错误“HTTP/1.1 401 未经授权”,在 android 中使用基本身份验证 - EWS 2010

我参考此链接来请求服务器。问题是有时(并不总是,大约20% - 30%,意味着有时我可以得到成功的响应),我得到了401错误和服务器响应基本授权挑战预期,但没有找到

这是我的代码:

 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);
            }
        }
    }
};

这是我的身份验证:

    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");

从注释代码中,您可以看到我尝试了许多方法,但错误并没有消失。这里是logcat说的:

 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

所以,请告诉我我的问题在哪里。提前致谢。
更新:目标服务器是 Exchange Web Service 2010。出于某种原因,我不想使用EWS API,我提出了自己的xml请求来连接到服务器(这个xml请求工作正常)。
这是请求失败时来自服务器的响应标头:

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

首次请求成功时:

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

和第二次:

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

饼干是我的问题,不是吗?


答案 1

上周我正在与Android HTTP basic作斗争。我已经尝试了3或4种不同的方法,但总是发现一些问题。现在,我设法使它工作的唯一方法是最简单的方法。

 UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass);
 Header header = new BasicScheme().authenticate(credentials, mHttpRequest);
 mHttpRequest.addHeader(header);

这对我使用AndroidHttpClient有用。

编辑:

这是我的完整源代码

https://gist.github.com/2642692

它基于 http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/


答案 2

推荐