Encountered org.apache.http.client.ClientProtocolException when httpclient request

Source: Internet
Author: User
Tags readline

Read the online solution, and later in a CSDN blog to find the answer, the following is my solution

Client.getparams (). Setparameter (clientpnames.allow_circular_redirects, false);

Httpurirequest request = new HttpGet (URL);

Httpentity httpentity = null;
Request.addheader ("Accept-charset", default_charset);
Request.addheader ("host", host);
Request.addheader ("Accept", Accept);
Request.addheader ("User-agent", user_agent);


The value of User_agent is: mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Qqdownload 1.7;. NET CLR 1.1.4322; CIBA. NET CLR 2.0.50727)

(Some sites will first determine whether the user's request is from the browser, if not, then return the incorrect text)

Here is someone else's blog text, quote:


Recently engaged in a project to understand the next httpclient, summed up here, and everyone to learn to share, I hope that friends to put forward valuable advice.

First, introduce the background of the project:
Goal: The domestic insurance Company's "Web one account Pass" to "WAP one account Pass".
Resource: The customer does not provide any webservice interfaces.

The third party component used in this project is the Apache HttpClient, a very powerful web crawler (it may not be good to catch this word), and here
Discuss some of the common uses of the httpclient and the places to be noted.

List of resources referenced in this article:

HttpClient Getting Started: http://www.ibm.com/developerworks/cn/opensource/os-httpclient/
HttpClient Certificate Import: http://www.blogjava.net/happytian/archive/2006/12/22/89447.html
HttpClient Advanced Knowledge: http://laohuang.iteye.com/blog/55613
HttpClient Official Document: http://hc.apache.org/httpcomponents-client/index.html
HttpClient Resource shutdown: http://www.iteye.com/topic/234759


The above article is very good, after reading also know how to use httpclient this very good tool, but here or add some more important things, is also the project
Problems that are often encountered.

The first thing to pay attention to are the following points:
1, httpclient connectivity after the issue of resource release is very important, and we use the database connection to release the same resources.
2, the HTTPS website uses the SSL encryption transmission, the certificate import should notice.
3, do such a project better understand the next HTTP semantics, such as 302,301,200,404 return code meaning (this is the most basic), cookie,session mechanism.
4. The redirect State of the httpclient is automatic by default, which greatly facilitates the developer (such as some authorization to obtain cookies), but sometimes it is managed manually, such as
Sometimes a circularredirectexception exception is encountered, which occurs because the location value in the returned header file points to the previously repeated (port number can be different) address, causing a possible death
Circular recursive redirection, which can be manually turned off: Method.setfollowredirects (false)
5, some sites will first determine whether the user's request is from the browser, if not, then return the incorrect text, so use httpclient crawl information in the head to add the following information:
Header.put ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1; Qqdownload 1.7;. NET CLR 1.1.4322; CIBA. NET CLR 2.0.50727) ");
6, when the POST request to submit data to change the default encoding, otherwise submitted to the data will appear garbled. The Setcontentcharset () method of rewriting Postmethod is OK:

[Java] View Plain copy print? /*   * HttpRequestProxy.java   *   * created on november 3 , 2008, 9:53 am   */      package cn.com.mozat.net;      import java.io.bufferedreader;   import java.io.ioexception;   Import  java.io.InputStream;   import java.io.inputstreamreader;   import  java.util.hashmap;   import java.util.iterator;   import java.util.map;    import java.util.set;      import org.apache.commons.httpclient.header;    import org.apache.commons.httpclient.httpclient;   import  org.apache.commons.httpclient.httpexception;   import org.apache.commons.httpclient.httpmethod;    import org.apache.commons.httpclient.namevaluepair;   import  Org.apache.commons.httpcLient. simplehttpconnectionmanager;   import org.apache.commons.httpclient.methods.getmethod;    import org.apache.commons.httpclient.methods.postmethod;      import  cn.com.mozat.exception.customexception;     /**   *    * @ author bird  email:lihongfu-84@163.com   *   * 2008-11-4   09:49:48   */   public class httprequestproxy{        Timeout interval        private static int connectTimeOut = 60000;     //ConnectionManager to turn off connection        private when managing Httpclientconnection  static boolean alwaysClose = false;    //return Data encoding format         private String encoding =  "UTF-8";         &nBsp     private final httpclient client = new httpclient (New  simplehttpconnectionmanager (alwaysclose));           public  httpclient gethttpclient () {           return  client;       }                 /**       *  usage:       *  Httprequestproxy hrp = new httprequestproxy ();       *  Hrp.dorequest ("http://www.163.com", Null,null, "GBK");       *         *  @param  url   Requested resources url       * @ Param postdata  post the form's encapsulated data   No newsletters null  when requested      * @ ParamHeader information (header) included with  header   request request   No newsletters null       *   @param  encoding response return information encoding format   No newsletters null       * @ Return  response back text data        *  @throws  CustomException         */       public string dorequest ( string url,map postdata,map header,string encoding)  throws customexception{         String responseString = null;         //Header Request Information         Header[] headers = null;         if (header != null) {          set entryset = header.entryset ();             int datalength = entryset.size ();              headers= new Header[dataLength];             int i = 0;            for ( Iterator itor = entryset.iterator () Itor.hasnext ();) {              Map.Entry entry =  (Map.entry) itor.next ();              headers[i++] = new header (Entry.getKey (). ToString (), Entry.getvalue (). ToString ());                     }        //post Way            if (postdata!=null) {             Postmethod postrequest = new postmethod (Url.trim ());             if (headers != null) {              for (int i = 0;i < headers.length;i++) {               postrequest.setrequestheader (Headers[i]);              }             }            set entryset  = postdata.entryset ();            int  Datalength = entryset.size ();             namevaluepair[] params = new namevaluepair[datalength];             int i = 0;            for ( Iterator itor = entryset.iterator () Itor.hasnext ();) {              Map.Entry entry =  (Map.entry) itor.next ();              params[i++] = new namevaluepair (Entry.getKey (). ToString (), Entry.getvalue (). ToString ());                         postrequest.setrequestbody (params);             try {        Responsestring = this.executemethod (postrequest,encoding);      } catch   (customexception e)  {       throw e;       } finally{       postrequest.releaseconnection ();      }            }         / /get mode            if (postdata == null) {            getmethod getrequest = new getmethod (Url.trim ());            if (headers != null) {              for (int i = 0;i  < headers.length;i++) {               Getrequest.setrequestheader (Headers[i]);              }            }             try {       responsestring = this.executemethod (getrequest,encoding);      } catch  (customexception e)  {                     E.printstacktrace ();       throw e;      }finally{        getrequest.releaseconnection ();      }           }                return responseString;       }       private string executemethod (httpmethod request, string encoding)  throws  CustomException{     String responseContent = null;      inputstream responsestream = null;     BufferedReader rd = null;      try {      this.gethttpclient (). Executemethod (Request);       if (encoding != null) {       responsestream  = request.getresponsebodyasstream ();        rd = new  bufferedreader (New inputstreamreader, responsestream,                          encoding ));                 string  templine = rd.readline ();                  stringbuffer tempstr = new stringbuffer ();                  string crlf=system.getproperty ("Line.separator");                  while  ( Templine != null)                   {                      tempstr.append (templine);                      tempstr.append (CRLF);                      templine  = rd.readline ();                  }                  responsecontent&nbsP;= tempstr.tostring ();      }else        Responsecontent = request.getresponsebodyasstring ();                     Header locationHeader =  Request.getresponseheader ("location");      //return code is 302,301, the page is already redirected, The URL of the location is requested again, which is important in       //some login authorization to fetch cookies       if  ( Locationheader != null)  {                 string redirecturl = locationheader.getvalue ();                 this.dorequest (redirectUrl, null,  Null,null);            }     }  catch  (httpexception e) &NBSP;{&Nbsp;     throw new 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.