HttpPost获取身份验证错误

时间:2014-02-05 01:31:49

标签: android curl http-post www-authenticate

我正在Authentication error: Unable to respond to any of these challenges: {}

这是来自服务器的响应

02-04 20:18:52.260: I/JSON(1306): <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
02-04 20:18:52.260: I/JSON(1306): <h4>A PHP Error was encountered</h4>
02-04 20:18:52.260: I/JSON(1306): <p>Severity: Notice</p>
02-04 20:18:52.260: I/JSON(1306): <p>Message:  Undefined index: nc</p>
02-04 20:18:52.260: I/JSON(1306): <p>Filename: libraries/REST_Controller.php</p>
02-04 20:18:52.260: I/JSON(1306): <p>Line Number: 754</p>
02-04 20:18:52.260: I/JSON(1306): </div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
02-04 20:18:52.260: I/JSON(1306): <h4>A PHP Error was encountered</h4>
02-04 20:18:52.260: I/JSON(1306): <p>Severity: Notice</p>
02-04 20:18:52.260: I/JSON(1306): <p>Message:  Undefined index: cnonce</p>
02-04 20:18:52.260: I/JSON(1306): <p>Filename: libraries/REST_Controller.php</p>
02-04 20:18:52.260: I/JSON(1306): <p>Line Number: 754</p>
02-04 20:18:52.260: I/JSON(1306): </div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
02-04 20:18:52.260: I/JSON(1306): <h4>A PHP Error was encountered</h4>
02-04 20:18:52.260: I/JSON(1306): <p>Severity: Notice</p>
02-04 20:18:52.260: I/JSON(1306): <p>Message:  Undefined index: qop</p>
02-04 20:18:52.260: I/JSON(1306): <p>Filename: libraries/REST_Controller.php</p>
02-04 20:18:52.260: I/JSON(1306): <p>Line Number: 754</p>
02-04 20:18:52.260: I/JSON(1306): </div>-

这是我的代码

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(surl);
httppost.setHeader(HTTP.CONTENT_TYPE, "application/json");
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Connection", "close");
httppost.setHeader("Vary", "Accept-Encoding");
httppost.setHeader("Vary", "User-Agent");

DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("algorithm", "MD5");
digestAuth.overrideParamter("realm", "REST API");
digestAuth.overrideParamter("nonce", Long.toString(new Random().nextLong(), 36));           
digestAuth.overrideParamter("qop", "auth");
digestAuth.overrideParamter("nc", "0");
digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce());
digestAuth.overrideParamter("opaque", "aba3d4b49c454e1974970e7b5514b001");

Header auth = digestAuth.authenticate(new
      UsernamePasswordCredentials("mypass", "mypassword"), httppost);
httppost.setHeader(auth);


// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

String result = EntityUtils.toString(response.getEntity());

Log.i("JSON", result+"-");

Header auth = digestAuth.authenticate(new UsernamePasswordCredentials("admin", "1234"), httppost);
httppost.setHeader(auth);


// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

String result = EntityUtils.toString(response.getEntity());

Log.i("JSON", result+"-");

这是我想在cURL中实现的

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_POST, 1); 

curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

我使用了web-sniffer

这是必需的HEADERS

WWW-Authenticate:摘要领域=“REST API”qop =“auth”nonce =“52f19001dc626”opaque =“aba3d4b49c454e1974970e7b5514b001”

内容长度: 41
不同:接受编码,用户代理
连接:关闭
内容类型: json

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

试试这个

httppost.setHeader("Authorization", "Basic " + Base64.encodeToString("admin:123".getBytes(), Base64.NO_WRAP));

而不是这个

Header auth = digestAuth.authenticate(new UsernamePasswordCredentials("admin", "1234"), httppost);
httppost.setHeader(auth);
相关问题