Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “错误”;HTTP/1.1 401“未经授权”;在android中使用基本身份验证-EWS 2010_Java_Android_Basic Authentication_Exchangewebservices - Fatal编程技术网

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

Java “错误”;HTTP/1.1 401“未经授权”;在android中使用基本身份验证-EWS 2010,java,android,basic-authentication,exchangewebservices,Java,Android,Basic Authentication,Exchangewebservices,我指的是对服务器的请求。问题是有时(不总是,大约20%-30%,意味着有时我可以获得成功的响应),我得到了401错误和服务器响应基本授权挑战,但没有找到 这是我的密码: HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) thr

我指的是对服务器的请求。问题是有时(不总是,大约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");
所以,请告诉我哪里是我的问题。提前感谢。
更新:目标服务器是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

cookie是我的问题,不是吗?

上周我在与Android HTTP basic进行斗争。我尝试过3到4种不同的方法,但总是发现一些问题。现在我唯一能让它工作的方法就是最简单的方法

 UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass);
 Header header = new BasicScheme().authenticate(credentials, mHttpRequest);
 mHttpRequest.addHeader(header);
这对我和AndroidHttpClient都有效

编辑:

这是我的全部源代码

这是基于

使用Wireshark确保发送的是您期望的内容。谢谢,我使用SoapUI发出请求,请求总是有效的,但我不知道为什么仍然存在错误。在您的应用程序(如果在emulator上运行,则指运行emulator的实际计算机)和目标服务器之间是否存在代理服务器?是,但我也在真实的设备连接到服务器中测试了它,仍然存在此错误。请在每个
else
中放入一些日志语句,如果
中,请检查在什么情况下
authState.getAuthScheme()
creds
为空。您建议NTLM身份验证吗?因为我得到了这个错误:“身份验证方案ntlm不受支持”身份验证错误:无法响应这些挑战:{ntlm=WWW-Authenticate:ntlm,negotiate=WWW-Authenticate:negotiate}我不知道ntlm我从未与IIS合作过。关于这一点,请看这篇文章,问题是我试图请求EWS服务器,它需要基本的身份验证。我不知道可能是什么,但是在Apache上使用抢占式基本身份验证进行日志记录可以与我之前发布的代码兼容。有了EWS,我帮不了你,抱歉。
 UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass);
 Header header = new BasicScheme().authenticate(credentials, mHttpRequest);
 mHttpRequest.addHeader(header);