22
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
ProxySelectorRoutePla