HttpGet 401状态码,后跟200个状态码 [英] HttpGet 401 status code followed by 200 status code

查看:613
本文介绍了HttpGet 401状态码,后跟200个状态码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有权访问服务器日志,当我尝试连接到服务器时并执行httpGet命令,我可以在日志中首先看到401状态(http Unauthorized),然后是200(http OK)。



2次尝试发生在httpClient.execute(httpGet)期间



以避免这种行为。任何想法?



以下是我目前使用的代码:

  HttpGet httpGet = new HttpGet(this.url + request); 

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,3000);
HttpConnectionParams.setSoTimeout(httpParameters,5000);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
凭证creds =新的UsernamePasswordCredentials(登录名,密码);
httpClient.getCredentialsProvider()。setCredentials(new AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT),creds);

HttpResponse响应= httpClient.execute(httpGet);
int status = response.getStatusLine()。getStatusCode();
Log.v(这是Response code status:+ status); //代码状态= 200(即使401和200在服务器日志中可见)。

有关信息,我使用的是Android应用程序的代码。


为了解决我的问题,我使用了一个与我的问题相关的另一个问题的答案(Adam Batkin):使用Apache HttpClient 4进行抢先式基本身份验证(感谢Bret Okken提供的链接)。



我终于有了以下几行:

  httpGet.addHeader(BasicScheme.authenticate(creds,UTF8,假)); 

获得如下代码:

  HttpGet httpGet = new HttpGet(this.url + request); 

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,3000);
HttpConnectionParams.setSoTimeout(httpParameters,5000);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
凭证creds =新的UsernamePasswordCredentials(登录名,密码);
httpClient.getCredentialsProvider()。setCredentials(new AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT),creds);

httpGet.addHeader(BasicScheme.authenticate(creds,UTF8,false));

HttpResponse响应= httpClient.execute(httpGet);
int status = response.getStatusLine()。getStatusCode();
Log.v(这是Response code status:+ status);

感谢Thomas Stets提供了有趣的答案。


I am facing a strange behaviour using the Apachage HttpComponent to access a webservice.

I have access to the server log and when I am trying to connect to the server and execute the httpGet command I can see in the log at first a 401 status (http Unauthorized) and then a 200 (http OK).

The 2 attempts take place during the "httpClient.execute(httpGet)"

So I am searching how to avoid this behaviour. Any ideas ?

Here is the following code I am currently using :

HttpGet httpGet = new HttpGet(this.url + request);

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,3000);
HttpConnectionParams.setSoTimeout(httpParameters,5000);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
Credentials creds = new UsernamePasswordCredentials(login, password);
httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);

HttpResponse response = httpClient.execute(httpGet);
int status = response.getStatusLine().getStatusCode();
Log.v(this, "Response code status: " + status); // code status = 200 (even if a 401 and then a 200 are visible in the server log).

For information, I am using this code for an Android application.

解决方案

To solve my problem, I used an answer (Adam Batkin) from another question related to my problem : Preemptive Basic authentication with Apache HttpClient 4 (Thanks Bret Okken for the link).

I finally had the following line :

httpGet.addHeader(BasicScheme.authenticate(creds, "UTF8", false));

To get a code like this :

HttpGet httpGet = new HttpGet(this.url + request);

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,3000);
HttpConnectionParams.setSoTimeout(httpParameters,5000);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
Credentials creds = new UsernamePasswordCredentials(login, password);
httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);

httpGet.addHeader(BasicScheme.authenticate(creds, "UTF8", false));

HttpResponse response = httpClient.execute(httpGet);
int status = response.getStatusLine().getStatusCode();
Log.v(this, "Response code status: " + status);

Thanks Thomas Stets for the interesting answer.

这篇关于HttpGet 401状态码,后跟200个状态码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆