weixin_33716154 2013-07-26 07:58 采纳率: 0%
浏览 33

AJAX认证?

Pls see image http://whatcodecraves.com/images/backbonejs-auth-http.png,

I have 2 AJAX requests:

  1. The first one to authenticate, server responded:

    Set-Cookie:JSESSIONID=06AB56403C08AD373BEDE33CBEFBE102; Path=/

  2. Another one to use the session. What should I do with this to send to server to use the same authentication session (a GET request)?

My code:

  1. First AJAX Request:

    $.ajax({
        url : "https://www.hafiz.gov.sa/HRDFWeb/AuthenticateDirectly",
        type : "POST",
        data : {
            "date" : "135854809",
            "json" : JSON.stringify({
                "nin" : "1089194383",
                "password" : "255793"
            })
        },
        success : function(data) {
            console.log(data);
        }
    });
    
  2. Second AJAX Request:

    $.ajax({
        url : 'https://www.hafiz.gov.sa/HRDFWeb/UpdateLastLogin?nin=1089194383&password=255793&date=1374661464&callback=?',
        type : "get",
        dataType: 'jsonp',
        success : function(data) {
            console.log(data);
        },
        error : function(data){
            console.log(data);
        }
    });
    

EDIT:

@w4rumy, Thanks for your answer, but I can't access that code. I'm writing a phonegap android app, this is the code that it uses to connect to server.

HttpPost httpost = new HttpPost("https://www.hafiz.gov.sa/HRDFWeb/AuthenticateDirectly");

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("date", System.currentTimeMillis() + ""));
nvps.add(new BasicNameValuePair("json", "{\"nin\":\""+idd+"\",\"password\":\""+passs+"\"}"));

try {
    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

try {
    response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
entity = response.getEntity();

Log.v("mytag", "Login form get: " + response.getStatusLine());

if (entity != null) {
    try {
        entity.consumeContent();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Log.v("mytag", "Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();

if (cookies.isEmpty()) {
    Log.v("mytag", "None");
} else {
    for (int i = 0; i < cookies.size(); i++) {
        Log.v("mytag", "- " + cookies.get(i).toString());
    }
}

httpget = new HttpGet("https://www.hafiz.gov.sa/HRDFWeb/UpdateLastLogin?nin=1089194383&password=255793&date=1358448809");
try {
    response = httpclient.execute(httpget);
} catch (ClientProtocolException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}

try {
    content = response.getEntity().getContent();
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 如何实验stm32主通道和互补通道独立输出
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题