I am using OAuth2 on my android project. The idea is to use a singleton HttpClient used with a ThreadSafeClientConnManager.
For a normal request to the server we construct an Authorization header and send that. The header is constructed from values received from the server. This works fine. However every 15 minutes we must get new values from the server to construct the header. To Received these values I must set the credentials like so.
client.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(creds.clientId, creds.clientSecret));
In order for this to work I must set up and new DefaultHttpClient. If I use the original singleton httpclient I receive some errors. My question is.. is it possible to set the credentials to be used only on this one request?
I noticed that there is an AuthScope. The host and port would not be suitable for this but maybe the realm would? I can't find anything that tells me what a realm is or how to use it.
06-05 10:12:55.969: W/System.err(23843):
org.apache.http.NoHttpResponseException: The target server failed to
respond 06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:504)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
So After more testing I have found where the problem lies.
I want to configure a pooled connection manager like so
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(
new Scheme("https", PlainSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conManager = new ThreadSafeClientConnManager(new BasicHttpParams(), schemeRegistry);
DefaultHttpClient httpClient = new DefaultHttpClient();
But when configure like this, I get the error above. If I use the normal default httpclient like so
DefaultHttpClient httpClient = new DefaultHttpClient();
Then it works fine. Any ideas?