DefaultHttpClient - 避免多次尝试执行请求

时间:2013-03-30 10:38:01

标签: java apache http

以下是我用来向网络服务发送帖子请求的代码:

    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<String> responseHandler = new BasicResponseHandler();
    try{
        String httpResponse = client.execute(request,responseHandler);
        return httpResponse; 
    }

    catch(Exception e){
        //handle        
    }

    finally{
        client.getConnectionManager().shutdown();
    }

这是http日志:

DEBUG 2013-03-28 16:22:00,627 org.apache.http.impl.conn.BasicClientConnectionManager - Get connection for route {}->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 - >> "POST /Contactus/contactus/ HTTP/1.1[\r][\n]"
DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - >> "Debug: True[\r][\n]"
DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - >> "SiteId: 111[\r][\n]"
DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - >> "Content-Type: application/x-www-form-urlencoded[\r][\n]"
DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - >> "Content-Length: 692[\r][\n]"
DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - >> "Host: jboss-stage.framework.abc.com[\r][\n]"
DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - >> "Connection: Keep-Alive[\r][\n]"
DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - >> "User-Agent: Apache-HttpClient/4.2.3 (java 1.5)[\r][\n]"
DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - >> "[\r][\n]"
DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - >> POST /Contactus/contactus/ HTTP/1.1
DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - >> Debug: True
DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - >> SiteId: 111
DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - >> Content-Type: application/x-www-form-urlencoded
DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - >> Content-Length: 692
DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - >> Host: jboss-stage.framework.abc.com
DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - >> Connection: Keep-Alive
DEBUG 2013-03-28 16:22:01,173 org.apache.http.headers - >> User-Agent: Apache-HttpClient/4.2.3 (java 1.5)
DEBUG 2013-03-28 16:22:01,173 org.apache.http.wire - >> "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 - << "HTTP/1.1 401 Unauthorized[\r][\n]"
DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - << "Server: Apache-Coyote/1.1[\r][\n]"
DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - << "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 - << "Date: Thu, 28 Mar 2013 10:52:08 GMT[\r][\n]"
DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - << "WWW-Authenticate: Basic realm="Contactus"[\r][\n]"
DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - << "Accept-Ranges: bytes[\r][\n]"
DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - << "Server: Noelios-Restlet-Engine/1.1.5[\r][\n]"
DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - << "Content-Type: text/html;charset=ISO-8859-1[\r][\n]"
DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - << "Content-Length: 312[\r][\n]"
DEBUG 2013-03-28 16:22:02,327 org.apache.http.wire - << "[\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 - << HTTP/1.1 401 Unauthorized
DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - << Server: Apache-Coyote/1.1
DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - << 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 - << Date: Thu, 28 Mar 2013 10:52:08 GMT
DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - << WWW-Authenticate: Basic realm="Contactus"
DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - << Accept-Ranges: bytes
DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - << Server: Noelios-Restlet-Engine/1.1.5
DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - << Content-Type: text/html;charset=ISO-8859-1
DEBUG 2013-03-28 16:22:02,327 org.apache.http.headers - << 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 - << "<html>[\n]"
DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - << "<head>[\n]"
DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - << "   <title>Status page</title>[\n]"
DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - << "</head>[\n]"
DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - << "<body>[\n]"
DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - << "<h3>The request requires user authentication</h3><p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2">here</a>.<br>[\n]"
DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - << "Please continue your visit at our <a href="/">home page</a>.[\n]"
DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - << "</p>[\n]"
DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - << "</body>[\n]"
DEBUG 2013-03-28 16:22:02,343 org.apache.http.wire - << "</html>[\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 - >> "POST /Contactus/contactus/ HTTP/1.1[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "Debug: True[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "SiteId: 111[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "Content-Type: application/x-www-form-urlencoded[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "Content-Length: 692[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "Host: jboss-stage.framework.abc.com[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "Connection: Keep-Alive[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "User-Agent: Apache-HttpClient/4.2.3 (java 1.5)[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "Authorization: Basic ********************[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "[\r][\n]"
DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - >> POST /Contactus/contactus/ HTTP/1.1
DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - >> Debug: True
DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - >> SiteId: 111
DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - >> Content-Type: application/x-www-form-urlencoded
DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - >> Content-Length: 692
DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - >> Host: jboss-stage.framework.abc.com
DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - >> Connection: Keep-Alive
DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - >> User-Agent: Apache-HttpClient/4.2.3 (java 1.5)
DEBUG 2013-03-28 16:22:02,359 org.apache.http.headers - >> Authorization: Basic ****************
DEBUG 2013-03-28 16:22:02,359 org.apache.http.wire - >> "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 - << "HTTP/1.1 201 Created[\r][\n]"
DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - << "Server: Apache-Coyote/1.1[\r][\n]"
DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - << "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 - << "Date: Thu, 28 Mar 2013 10:52:10 GMT[\r][\n]"
DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - << "Accept-Ranges: bytes[\r][\n]"
DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - << "Server: Noelios-Restlet-Engine/1.1.5[\r][\n]"
DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - << "Content-Type: application/json;charset=UTF-8[\r][\n]"
DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - << "Content-Length: 114[\r][\n]"
DEBUG 2013-03-28 16:22:03,342 org.apache.http.wire - << "[\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 - << HTTP/1.1 201 Created
DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - << Server: Apache-Coyote/1.1
DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - << 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 - << Date: Thu, 28 Mar 2013 10:52:10 GMT
DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - << Accept-Ranges: bytes
DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - << Server: Noelios-Restlet-Engine/1.1.5
DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - << Content-Type: application/json;charset=UTF-8
DEBUG 2013-03-28 16:22:03,342 org.apache.http.headers - << 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 - << "{"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

在尝试1中,我得到回复HTTP/1.1 401 Unauthorized,而在尝试2中,授权成功,我获得成功回复HTTP/1.1 201 Created。如何避免不成功的尝试(1)?解决这个问题将极大地帮助表现。谢谢!

编辑:好的,我查看了其他答案,似乎有两种实现方法: 1.将抢先认证设置为true 2.在标题

中设置身份验证

哪种方式最好?

0 个答案:

没有答案