(Question also asked on stackoverflow forum without success :
http://stackoverflow.com/questions/4681539/android-multiple-post-on-the-same-connection)
Hello guys,
I've a problem doing multiple POST request on the same connection with
HTTP 1.1 standard. The first request goes to the server, but the
second is blocked on "client.execute(httpGet, httpContext);".
This works fine for multiple HTTP GET requests.
If I reinitialize the connection between each POST request, it works,
but much slower due to the connection initialization.
public void initializeHttpConnection(String login, String password)
{
schemeRegistry.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));// http scheme
schemeRegistry.register(new Scheme("https", new
EasySSLSocketFactory(), 443));// https scheme
HttpParams httpParameters = new BasicHttpParams();
httpParameters.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS,
1);
httpParameters.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE,
new ConnPerRouteBean(1));
httpParameters.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
false);
httpParameters.setParameter(HttpConnectionParams.CONNECTION_TIMEOUT,
10000);// Set the timeout in milliseconds until a connection is
established.
httpParameters.setParameter(HttpConnectionParams.SO_TIMEOUT,
60000);// in milliseconds which is the timeout for waiting for data.
HttpProtocolParams.setVersion(httpParameters,
HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParameters, "utf8");
ClientConnectionManager clientConnectionManager = new
ThreadSafeClientConnManager(httpParameters, schemeRegistry);
httpContext = new BasicHttpContext();
BasicScheme basicAuth = new BasicScheme();
httpContext.setAttribute("preemptive-auth", basicAuth);
//connection (client has to be created for every new connection)
client = new DefaultHttpClient(clientConnectionManager,
httpParameters);
client.getCredentialsProvider().setCredentials(new
AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(login,
password));
client.addRequestInterceptor(new PreemptiveAuth(), 0);
}
static class PreemptiveAuth implements HttpRequestInterceptor
{
public void process(final HttpRequest request, final HttpContext
context) throws HttpException, IOException
{
AuthState authState = (AuthState)
context.getAttribute(ClientContext.TARGET_AUTH_STATE);
// If no auth scheme avaialble yet, try to initialize it
preemptively
if (authState.getAuthScheme() == null)
{
AuthScheme authScheme = (AuthScheme)
context.getAttribute("preemptive-auth");
CredentialsProvider credsProvider = (CredentialsProvider)
context.getAttribute(ClientContext.CREDS_PROVIDER);
if (authScheme != null)
{
Credentials creds = credsProvider.getCredentials(new
AuthScope(AuthScope.ANY));
if (creds == null)
throw new HttpException("No credentials for
preemptive authentication");
authState.setAuthScheme(authScheme);
authState.setCredentials(creds);
}
}
}
}
private HttpResponse postHttpResponse(String sync_interface, String
data) throws Exception
{
HttpPost httpPost = new HttpPost(PROTOCOL + HOST +
sync_interface);
httpPost.setHeader("Host", HOST);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("User-Agent", getUserAgent());
StringEntity stringEntity = null;
try
{
stringEntity = new StringEntity(data, "UTF-8");
stringEntity.setContentType("application/json");
}
catch (UnsupportedEncodingException e)
{
Log.e("CM", "UnsupportedEncodingException : "+e);
}
httpPost.setEntity(stringEntity);
return client.execute(httpPost, httpContext);
}
I use HTTP Basic Authentication, the web server is running on rails
3.0.
My call:
initializeHttpConnection("login", "password");
postHttpResponse("url", "data");
Thanks in advance for your help
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en