Note that there are some explanatory texts on larger screens.

plurals
  1. PODefaultHttpClient - Avoiding multiple Attempts to execute request
    primarykey
    data
    text
    <p>Here's the code I am using to send a post request to a web service : </p> <pre><code> HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters,15000); HttpConnectionParams.setSoTimeout(httpParameters, 15000); DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC), new UsernamePasswordCredentials(getUsernamePassword())); //getUserNamePassword is helper method i have defined ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler(); try{ String httpResponse = client.execute(request,responseHandler); return httpResponse; } catch(Exception e){ //handle } finally{ client.getConnectionManager().shutdown(); } </code></pre> <p>And this is the http log:</p> <pre><code>DEBUG 2013-03-28 16:22:00,627 org.apache.http.impl.conn.BasicClientConnectionManager - Get connection for route {}-&gt;http://jboss-stage.framework.abc.com DEBUG 2013-03-28 16:22:00,658 org.apache.http.impl.conn.DefaultClientConnectionOperator - Connecting to jboss-stage.framework.abc.com:80 DEBUG 2013-03-28 16:22:01,157 org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: best-match DEBUG 2013-03-28 16:22:01,173 org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context DEBUG 2013-03-28 16:22:01,173 org.apache.http.client.protocol.RequestTargetAuthentication - Target auth state: UNCHALLENGED DEBUG 2013-03-28 16:22:01,173 org.apache.http.client.protocol.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED DEBUG 2013-03-28 16:22:01,173 org.apache.http.impl.client.DefaultHttpClient - Attempt 1 to execute request DEBUG 2013-03-28 16:22:01,173 org.apache.http.impl.conn.DefaultClientConnection - Sending request: POST /Contactus/contactus/ HTTP/1.1 DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "POST /Contactus/contactus/ HTTP/1.1[\r][\n]" DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "Debug: True[\r][\n]" DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "SiteId: 111[\r][\n]" DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "Content-Type: application/x-www-form-urlencoded[\r][\n]" DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "Content-Length: 692[\r][\n]" DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "Host: jboss-stage.framework.abc.com[\r][\n]" DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "Connection: Keep-Alive[\r][\n]" DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "User-Agent: Apache-HttpClient/4.2.3 (java 1.5)[\r][\n]" DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "[\r][\n]" DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - &gt;&gt; POST /Contactus/contactus/ HTTP/1.1 DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - &gt;&gt; Debug: True DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - &gt;&gt; SiteId: 111 DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - &gt;&gt; Content-Type: application/x-www-form-urlencoded DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - &gt;&gt; Content-Length: 692 DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - &gt;&gt; Host: jboss-stage.framework.abc.com DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - &gt;&gt; Connection: Keep-Alive DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - &gt;&gt; User-Agent: Apache-HttpClient/4.2.3 (java 1.5) DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - &gt;&gt; "ServiceReq={"ContactUs":{"ContactInfo":[{"FieldName":"gender","FieldValue":"F","IsEncrypted":false,"EncryptedValue":false}],"StatusCode":"New","Subject_id":"123"}}" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "HTTP/1.1 401 Unauthorized[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "Server: Apache-Coyote/1.1[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "Date: Thu, 28 Mar 2013 10:52:08 GMT[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "WWW-Authenticate: Basic realm="Contactus"[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "Accept-Ranges: bytes[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "Server: Noelios-Restlet-Engine/1.1.5[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "Content-Type: text/html;charset=ISO-8859-1[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "Content-Length: 312[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - &lt;&lt; "[\r][\n]" DEBUG 2013-03-28 16:22:02,327 org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 401 Unauthorized DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - &lt;&lt; HTTP/1.1 401 Unauthorized DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - &lt;&lt; Server: Apache-Coyote/1.1 DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - &lt;&lt; X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0 DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - &lt;&lt; Date: Thu, 28 Mar 2013 10:52:08 GMT DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - &lt;&lt; WWW-Authenticate: Basic realm="Contactus" DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - &lt;&lt; Accept-Ranges: bytes DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - &lt;&lt; Server: Noelios-Restlet-Engine/1.1.5 DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - &lt;&lt; Content-Type: text/html;charset=ISO-8859-1 DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - &lt;&lt; Content-Length: 312 DEBUG 2013-03-28 16:22:02,343 org.apache.http.impl.client.DefaultHttpClient - Connection can be kept alive indefinitely DEBUG 2013-03-28 16:22:02,343 org.apache.http.impl.client.DefaultHttpClient - Authentication required DEBUG 2013-03-28 16:22:02,343 org.apache.http.impl.client.DefaultHttpClient - jboss-stage.framework.abc.com:80 requested authentication DEBUG 2013-03-28 16:22:02,343 org.apache.http.impl.client.TargetAuthenticationStrategy - Authentication schemes in the order of preference: [negotiate, Kerberos, NTLM, Digest, Basic] DEBUG 2013-03-28 16:22:02,343 org.apache.http.impl.client.TargetAuthenticationStrategy - Challenge for negotiate authentication scheme not available DEBUG 2013-03-28 16:22:02,343 org.apache.http.impl.client.TargetAuthenticationStrategy - Challenge for Kerberos authentication scheme not available DEBUG 2013-03-28 16:22:02,343 org.apache.http.impl.client.TargetAuthenticationStrategy - Challenge for NTLM authentication scheme not available DEBUG 2013-03-28 16:22:02,343 org.apache.http.impl.client.TargetAuthenticationStrategy - Challenge for Digest authentication scheme not available DEBUG 2013-03-28 16:22:02,343 org.apache.http.impl.client.DefaultHttpClient - Selected authentication options: [BASIC] DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; "&lt;html&gt;[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; "&lt;head&gt;[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; " &lt;title&gt;Status page&lt;/title&gt;[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; "&lt;/head&gt;[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; "&lt;body&gt;[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; "&lt;h3&gt;The request requires user authentication&lt;/h3&gt;&lt;p&gt;You can get technical details &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2"&gt;here&lt;/a&gt;.&lt;br&gt;[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; "Please continue your visit at our &lt;a href="/"&gt;home page&lt;/a&gt;.[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; "&lt;/p&gt;[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; "&lt;/body&gt;[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - &lt;&lt; "&lt;/html&gt;[\n]" DEBUG 2013-03-28 16:22:02,343 org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: best-match DEBUG 2013-03-28 16:22:02,343 org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context DEBUG 2013-03-28 16:22:02,343 org.apache.http.client.protocol.RequestTargetAuthentication - Target auth state: CHALLENGED DEBUG 2013-03-28 16:22:02,343 org.apache.http.client.protocol.RequestTargetAuthentication - Generating response to an authentication challenge using basic scheme DEBUG 2013-03-28 16:22:02,359 org.apache.http.client.protocol.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED DEBUG 2013-03-28 16:22:02,359 org.apache.http.impl.client.DefaultHttpClient - Attempt 2 to execute request DEBUG 2013-03-28 16:22:02,359 org.apache.http.impl.conn.DefaultClientConnection - Sending request: POST /Contactus/contactus/ HTTP/1.1 DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "POST /Contactus/contactus/ HTTP/1.1[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "Debug: True[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "SiteId: 111[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "Content-Type: application/x-www-form-urlencoded[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "Content-Length: 692[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "Host: jboss-stage.framework.abc.com[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "Connection: Keep-Alive[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "User-Agent: Apache-HttpClient/4.2.3 (java 1.5)[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "Authorization: Basic ********************[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "[\r][\n]" DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - &gt;&gt; POST /Contactus/contactus/ HTTP/1.1 DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - &gt;&gt; Debug: True DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - &gt;&gt; SiteId: 111 DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - &gt;&gt; Content-Type: application/x-www-form-urlencoded DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - &gt;&gt; Content-Length: 692 DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - &gt;&gt; Host: jboss-stage.framework.abc.com DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - &gt;&gt; Connection: Keep-Alive DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - &gt;&gt; User-Agent: Apache-HttpClient/4.2.3 (java 1.5) DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - &gt;&gt; Authorization: Basic **************** DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - &gt;&gt; "ServiceReq={"ContactUs":{"ContactInfo":[{"FieldName":"gender","FieldValue":"F","IsEncrypted":false,"EncryptedValue":false}],"StatusCode":"New","Subject_id":"123"}}" DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "HTTP/1.1 201 Created[\r][\n]" DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "Server: Apache-Coyote/1.1[\r][\n]" DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0[\r][\n]" DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "Date: Thu, 28 Mar 2013 10:52:10 GMT[\r][\n]" DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "Accept-Ranges: bytes[\r][\n]" DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "Server: Noelios-Restlet-Engine/1.1.5[\r][\n]" DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "Content-Type: application/json;charset=UTF-8[\r][\n]" DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "Content-Length: 114[\r][\n]" DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "[\r][\n]" DEBUG 2013-03-28 16:22:03,342 org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 201 Created DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - &lt;&lt; HTTP/1.1 201 Created DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - &lt;&lt; Server: Apache-Coyote/1.1 DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - &lt;&lt; X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0 DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - &lt;&lt; Date: Thu, 28 Mar 2013 10:52:10 GMT DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - &lt;&lt; Accept-Ranges: bytes DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - &lt;&lt; Server: Noelios-Restlet-Engine/1.1.5 DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - &lt;&lt; Content-Type: application/json;charset=UTF-8 DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - &lt;&lt; Content-Length: 114 DEBUG 2013-03-28 16:22:03,342 org.apache.http.impl.client.DefaultHttpClient - Connection can be kept alive indefinitely DEBUG 2013-03-28 16:22:03,342 org.apache.http.impl.client.DefaultHttpClient - Authentication succeeded DEBUG 2013-03-28 16:22:03,342 org.apache.http.impl.client.TargetAuthenticationStrategy - Caching 'basic' auth scheme for http://jboss-stage.framework.abc.com:80 DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - &lt;&lt; "{"StatusCode":"00","contactusId":2025958,"Message":"Success","AutoResponse":"Text","AutoResponseText":"Thank you"}" DEBUG 2013-03-28 16:22:03,342 org.apache.http.impl.conn.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@1798928 DEBUG 2013-03-28 16:22:03,342 org.apache.http.impl.conn.BasicClientConnectionManager - Connection can be kept alive indefinitely </code></pre> <p>In attempt 1, i get the response <code>HTTP/1.1 401 Unauthorized</code> whereas in attempt 2, authorization is successful and i get a success response <code>HTTP/1.1 201 Created</code>. How can I avoid the unsuccessful attempt (1)? Solving this will greatly help performance.Thanks!</p> <p><strong>EDIT</strong>: Ok, i looked at other answers and there seems to be 2 ways of achieving this : 1. setting preemptive authentication to true 2. Setting the authentication in header</p> <p>Which is the best way though?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload