Tôi tham khảo this link để yêu cầu máy chủ. Vấn đề là đôi khi (không phải lúc nào, khoảng 20% - 30%, có nghĩa là đôi khi tôi có thể nhận được phản hồi thành công), tôi nhận được lỗi 401 và phản hồi máy chủ thách thức ủy quyền cơ bản dự kiến, nhưng không tìm thấy.Lỗi "HTTP/1.1 401 trái phép" với xác thực cơ bản trong android - EWS 2010
Đây là mã của tôi:
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);
}
}
}
};
Đây là xác thực của tôi:
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");
Từ mã bình luận, bạn có thể thấy tôi thử nhiều cách khác nhau, nhưng các lỗi đã không biến mất. Đây là logcat nói:
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
Vì vậy, hãy cho tôi biết vấn đề của tôi ở đâu. Cảm ơn trước.
Cập nhật: máy chủ đích là Exchange Web Service 2010. Vì lý do nào đó, tôi không muốn sử dụng EWS API, tôi đã thực hiện yêu cầu xml của riêng mình để kết nối với máy chủ (yêu cầu xml này hoạt động bình thường).
Và đây là tiêu đề phản hồi từ máy chủ Khi yêu cầu không thành:
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
Khi yêu cầu thành công lần đầu tiên:
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
và lần thứ hai:
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
Các cookie là vấn đề của tôi, phải không?
Sử dụng Wireshark để đảm bảo nó gửi những gì bạn mong đợi. –
Cảm ơn, tôi sử dụng SoapUI để thực hiện yêu cầu, nó luôn hoạt động, nhưng tôi không biết tại sao lỗi này vẫn có lỗi – R4j
Có máy chủ proxy nào nằm giữa ứng dụng của bạn không (nếu bạn chạy nó trên trình mô phỏng chạy trình giả lập) và máy chủ đích? – yorkw