我正在尝试从共享点服务器获取数据。
以下是我的代码。
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
httpclient.getCredentialsProvider().setCredentials(new AuthScope("masconsult.eu", -1),
new NTCredentials(username, password, "", ""));
HttpGet httpGet = new HttpGet(webserviceUrl);
httpGet.addHeader("Content-type", "application/json");
httpGet.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
// HttpResponse response="";
String responseXML = "";
HttpResponse response = httpclient.execute(httpGet);
response.getStatusLine().getReasonPhrase();
responseXML = EntityUtils.toString(response.getEntity());
Toast.makeText(this, responseXML, Toast.LENGTH_LONG).show();
我收到响应HTTP / 1.1 401未经授权。甚至我都添加了所有正确的凭据。
在Chrome浏览器中,使用相同的凭据即可正常工作。
请建议我对代码进行任何更改。
最佳答案
现在可以正常工作了。
我用标头修改了代码。
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new NTCredentials(username, password, "", ""));
HttpGet httpGet = new HttpGet(webserviceUrl);
httpGet.addHeader("accept", "application/json;odata=verbose");
httpGet.addHeader("content-Type", "application/json;odata=verbose");
httpGet.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
HttpResponse response = httpclient.execute(httpGet);
System.out.println("Responseeee" + response.getStatusLine());
responseXML = EntityUtils.toString(response.getEntity());
new JSONObject(responseXML).toString();