错误" HTTP / 1.1 401未经授权"在Android的基本身份验证 - EWS 2010 [英] Error "HTTP/1.1 401 Unauthorized" with basic authentication in android - EWS 2010
问题描述
我指的是此链接请求到服务器。问题是有时(并不总是,约20% - 30%,这意味着有时我能得到成功响应),我得到了401错误和服务器响应的基本授权挑战预期,但没有找到的。 P>
下面是我的code:
的Htt prequestInterceptor preemptiveAuth =新的Htt prequestInterceptor()
{
公共无效过程(最终的Htt prequest的要求,最终HttpContext的背景下)
抛出HttpException,IOException异常
{
AuthState authState =(AuthState)上下文
.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider =(CredentialsProvider)上下文
.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost =(HttpHost)上下文
.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
如果(authState.getAuthScheme()== NULL)
{
AuthScope authScope =新AuthScope(targetHost.getHostName()
targetHost.getPort());
凭据creds = credsProvider.getCredentials(authScope);
如果(creds!= NULL)
{
authState.setAuthScheme(新BasicScheme());
authState.setCredentials(creds);
}
}
}
};
下面是我的身份验证:
HttpPost httpPost =新HttpPost(SERVER_AUTH_URL);
httpPost.setHeader(内容型,为text / xml;字符集= UTF-8);
httpPost.setHeader(保持活跃,300);
httpPost.setHeader(连接,保持活动);
StringEntity本身=新StringEntity(myRequest,UTF-8);
httpPost.setEntity(SE);
se.setContentType(为text / xml);
se.setContentEncoding(gzip压缩,放气);
//增加preemptiveAuth
client.addRequestInterceptor(preemptiveAuth,0);
//设置代理
ProxySelectorRoutePlanner routePlanner =新ProxySelectorRoutePlanner(
client.getConnectionManager()。getSchemeRegistry(),
ProxySelector.getDefault());
client.setRoutePlanner(routePlanner);
名单<字符串> AUTH preFS =新的ArrayList<字符串>();
AUTH prefs.add(AuthPolicy.BASIC);
AUTH prefs.add(AuthPolicy.NTLM);
AUTH prefs.add(AuthPolicy.DIGEST);
client.getParams()的setParameter(http.auth.scheme优先,权威性preFS)。
CredentialsProvider credProvider =新BasicCredentialsProvider();
// client.getParams()的setParameter(http.auth.scheme优先,
// AUTH preFS);
credProvider.setCredentials(AuthScope.ANY,
新NTCredentials(getUsername(),getPassword来(),
,getDomain()));
Log.d(TAG,修EXCUTE);
client.setCredentialsProvider(credProvider);
HTT presponse响应= client.execute(httpPost);
Log.d(TAG,有EXCUTE);
在注释code,你可以看到我尝试过很多办法,但错误没有消失。这是logcat的说:
十月五日至2日:28:21.724:D / dalvikvm(1169):GC_FOR_MALLOC释放11259对象/ 457352字节43ms
十月五号至二号:28:22.384:D / MainActivity(1169):修复EXCUTE
十月五号至二号:28:22.744:D / dalvikvm(1169):GC_FOR_MALLOC释放11481对象/ 447264字节67ms
十月五号至二号:28:22.894:D / dalvikvm(1169):GC_FOR_MALLOC释放1078对象/ 83928字节76ms
十月五号至二号:28:22.894:I / dalvikvm堆(1169):成长堆(破片的情况下),以3.833MB为87396字节的分配
十月五号至二号:28:23.034:D / dalvikvm(1169):GC_FOR_MALLOC释放58的对象/ 3336字节143ms
十月五号至二号:28:23.124:W / DefaultRequestDirector(1169):验证错误:预期基本授权的挑战,但没有发现
十月五号至二号:28:23.124:D / MainActivity(1169):已EXCUTE
十月五号至二号:28:23.124:E / MainActivity(1169):响应错误:HTTP / 1.1 401未经授权
那么,请告诉我,是我的问题。在此先感谢。
更新:目标服务器的Exchange Web服务的2010年。出于某种原因,我不希望使用EWS API的,我做我自己的XML请求连接到服务器(此XML请求正常工作) 。
这里是从服务器的响应头
当请求失败:
服务器Microsoft-IIS / 7.5
WWW身份验证协商
WWW身份验证NTLM
的X技术支持,通过ASP.NET
日期星期五2012年三时47分57秒格林尼治标准时间5月25日
内容长度0
在申请成功的第一次:
缓存控制私人
传输编码分块
内容类型为text / xml;字符集= UTF-8
服务器Microsoft-IIS / 7.5
设置Cookie exchangecookie = 1d9d9b1d21064035ad375c8aecde2168;到期=星期六,25日,2013四点00分46秒格林尼治标准时间;路径= /;仅Http
的X ASPNET-版本2.0.50727
的X技术支持,通过ASP.NET
日期星期五,2012 4时00分45秒格林尼治标准时间5月25日
和第二次:
缓存控制私人
传输编码分块
内容类型为text / xml;字符集= UTF-8
服务器Microsoft-IIS / 7.5
的X EwsPerformanceData RPCC = 4; RPCL = 0; LdapC = 0; LdapL = 0;
的X ASPNET-版本2.0.50727
的X技术支持,通过ASP.NET
日期星期五,2012四时00分47秒格林尼治标准时间5月25日
该cookie是我的问题,不是吗?
我基本上周挣扎与Android HTTP。我试过,也许3或4个不同的做这件事的方式,总是我发现了一些问题。现在唯一的办法我设法使它工作是最简单的方法。
UsernamePasswordCredentials凭据=新UsernamePasswordCredentials(user和pass);
标题标题=新BasicScheme()认证(证书,mHtt prequest)。
mHtt prequest.addHeader(头);
这对我来说曾与AndroidHttpClient。
编辑:
这是我的完整源$ C $ C
https://gist.github.com/2642692
这是基于 <一href="http://luken$c$c.com/2010/04/27/calling-web-services-in-android-using-httpclient/">http://luken$c$c.com/2010/04/27/calling-web-services-in-android-using-httpclient/
I refer to this link to request to the server. The problem is sometime (not always, about 20% - 30%, means sometime I can get successful response), I got the 401 error and server response basic authorization challenge expected, but not found.
Here is my code:
HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor()
{
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException
{
AuthState authState = (AuthState) context
.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider) context
.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context
.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (authState.getAuthScheme() == null)
{
AuthScope authScope = new AuthScope(targetHost.getHostName(),
targetHost.getPort());
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null)
{
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
};
Here is my authentication:
HttpPost httpPost = new HttpPost(SERVER_AUTH_URL);
httpPost.setHeader("Content-type", "text/xml; charset=utf-8");
httpPost.setHeader("Keep-Alive", "300");
httpPost.setHeader("Connection", "Keep-Alive");
StringEntity se = new StringEntity(myRequest, "UTF-8");
httpPost.setEntity(se);
se.setContentType("text/xml");
se.setContentEncoding("gzip,deflate");
//add preemptiveAuth
client.addRequestInterceptor(preemptiveAuth, 0);
//Set the proxy
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
client.getConnectionManager().getSchemeRegistry(),
ProxySelector.getDefault());
client.setRoutePlanner(routePlanner);
List<String> authPrefs = new ArrayList<String>();
authPrefs.add(AuthPolicy.BASIC);
authPrefs.add(AuthPolicy.NTLM);
authPrefs.add(AuthPolicy.DIGEST);
client.getParams().setParameter("http.auth.scheme-priority", authPrefs);
CredentialsProvider credProvider = new BasicCredentialsProvider();
// client.getParams().setParameter("http.auth.scheme-priority",
// authPrefs);
credProvider.setCredentials(AuthScope.ANY,
new NTCredentials(getUsername(), getPassword(),
"", getDomain()));
Log.d(TAG, "repair excute");
client.setCredentialsProvider(credProvider);
HttpResponse response = client.execute(httpPost);
Log.d(TAG, "has excute");
From the comment code, you can see I tried many ways, but the error wasn't disappear. Here is logcat say:
05-02 10:28:21.724: D/dalvikvm(1169): GC_FOR_MALLOC freed 11259 objects / 457352 bytes in 43ms
05-02 10:28:22.384: D/MainActivity(1169): repair excute
05-02 10:28:22.744: D/dalvikvm(1169): GC_FOR_MALLOC freed 11481 objects / 447264 bytes in 67ms
05-02 10:28:22.894: D/dalvikvm(1169): GC_FOR_MALLOC freed 1078 objects / 83928 bytes in 76ms
05-02 10:28:22.894: I/dalvikvm-heap(1169): Grow heap (frag case) to 3.833MB for 87396-byte allocation
05-02 10:28:23.034: D/dalvikvm(1169): GC_FOR_MALLOC freed 58 objects / 3336 bytes in 143ms
05-02 10:28:23.124: W/DefaultRequestDirector(1169): Authentication error: basic authorization challenge expected, but not found
05-02 10:28:23.124: D/MainActivity(1169): has excute
05-02 10:28:23.124: E/MainActivity(1169): response error: HTTP/1.1 401 Unauthorized
So, please tell me where is my problem. Thanks in advance.
Update: the target server is Exchange Web Service 2010. For some reason, I don't want to use EWS API, I made my own xml request to connect to the server (this xml request works properly).
And here is the response header from server
When request failed:
Server Microsoft-IIS/7.5
WWW-Authenticate Negotiate
WWW-Authenticate NTLM
X-Powered-By ASP.NET
Date Fri, 25 May 2012 03:47:57 GMT
Content-Length 0
When request success the first time:
Cache-Control private
Transfer-Encoding chunked
Content-Type text/xml; charset=utf-8
Server Microsoft-IIS/7.5
Set-Cookie exchangecookie=1d9d9b1d21064035ad375c8aecde2168; expires=Sat, 25-May-2013 04:00:46 GMT; path=/; HttpOnly
X-AspNet-Version 2.0.50727
X-Powered-By ASP.NET
Date Fri, 25 May 2012 04:00:45 GMT
and the second time:
Cache-Control private
Transfer-Encoding chunked
Content-Type text/xml; charset=utf-8
Server Microsoft-IIS/7.5
X-EwsPerformanceData RpcC=4;RpcL=0;LdapC=0;LdapL=0;
X-AspNet-Version 2.0.50727
X-Powered-By ASP.NET
Date Fri, 25 May 2012 04:00:47 GMT
The cookie is my problem, isn't it?
I was struggling with Android HTTP basic last week. I have tried maybe 3 or 4 different ways of doing it and always I found some problem. Right now the only way I managed to make it work was the simplest way.
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass);
Header header = new BasicScheme().authenticate(credentials, mHttpRequest);
mHttpRequest.addHeader(header);
That worked for me with AndroidHttpClient.
Edit:
This is my full source code
https://gist.github.com/2642692
It's based on http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/
这篇关于错误&QUOT; HTTP / 1.1 401未经授权&QUOT;在Android的基本身份验证 - EWS 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!