HttpClient How to receive malformed response header information

Source: Internet
Author: User

Exception in thread ' main ' org.apache.commons.httpclient.ProtocolException:Unable to parse header:share memory not exist , need createNewShare memory!At org.apache.commons.httpclient.HttpParser.parseHeaders (Httpparser.java:202) at Org.apache.commons.httpclient.HttpMethodBase.readResponseHeaders (Httpmethodbase.java:1935) at Org.apache.commons.httpclient.HttpMethodBase.readResponse (Httpmethodbase.java:1737) at Org.apache.commons.httpclient.HttpMethodBase.execute (Httpmethodbase.java:1098) at Org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry (Httpmethoddirector.java:398) at Org.apache.commons.httpclient.HttpMethodDirector.executeMethod (Httpmethoddirector.java:171) at Org.apache.commons.httpclient.HttpClient.executeMethod (Httpclient.java:397) at Org.apache.commons.httpclient.HttpClient.executeMethod (Httpclient.java:323)

When doing web crawler, simulate GET request, access URL, display the above error exception. Asked 3 seniors, all said not met, this can be a headache.

Mainly do not know what is the problem, some people say it may be buffer, set the size of large points. spent an evening checking online information, about finally a little bit, see Reference site: http://bbs.csdn.net/topics/390178589

/** *  */ Packagecom.http;Importjava.io.IOException;ImportOrg.apache.http.Header;Importorg.apache.http.HttpException;ImportOrg.apache.http.HttpResponse;Importorg.apache.http.HttpResponseFactory;Importorg.apache.http.HttpVersion;ImportOrg.apache.http.conn.ClientConnectionOperator;Importorg.apache.http.conn.OperatedClientConnection;ImportOrg.apache.http.conn.scheme.SchemeRegistry;ImportOrg.apache.http.impl.conn.BasicClientConnectionManager;Importorg.apache.http.impl.conn.DefaultClientConnection;ImportOrg.apache.http.impl.conn.DefaultClientConnectionOperator;ImportOrg.apache.http.impl.conn.DefaultHttpResponseParser;ImportOrg.apache.http.io.HttpMessageParser;ImportOrg.apache.http.io.SessionInputBuffer;ImportOrg.apache.http.message.BasicHeader;ImportOrg.apache.http.message.BasicHttpResponse;ImportOrg.apache.http.message.BasicLineParser;ImportOrg.apache.http.message.BasicStatusLine;ImportOrg.apache.http.message.LineParser;ImportOrg.apache.http.params.HttpParams;ImportOrg.apache.http.util.CharArrayBuffer;/** * @authorYingzi **/ Public classMybasicclientconnectionmanagerextendsBasicclientconnectionmanager { PublicMybasicclientconnectionmanager () {Super(); } @OverrideprotectedClientconnectionoperator Createconnectionoperator (FinalSchemeregistry SR) {        return NewMyclientconnectionoperator (SR); }            classMyclientconnectionextendsdefaultclientconnection {@OverrideprotectedHttpmessageparser Createresponseparser (FinalSessioninputbuffer Buffer,Finalhttpresponsefactory Responsefactory,Finalhttpparams params) {            return NewMydefaulthttpresponseparser (Buffer,NewMylineparser (), responsefactory, params); }    }        classMydefaulthttpresponseparserextendsDefaulthttpresponseparser { PublicMydefaulthttpresponseparser (sessioninputbuffer buffer, Lineparser parser, httpresponsefactory response Factory, Httpparams params) {Super(buffer, parser, responsefactory, params); } @OverrideprotectedHttpResponse Parsehead (FinalSessioninputbuffer Sessionbuffer)throwsIOException, HttpException {Try {                return Super. Parsehead (Sessionbuffer); } Catch(Exception ex) {//suppressing parseexception anomalies                return NewBasichttpresponse (NewBasicstatusline (Httpversion.http_1_1, 200, ""))); }        }    }        classMyclientconnectionoperatorextendsDefaultclientconnectionoperator { PublicMyclientconnectionoperator (FinalSchemeregistry SR) {            Super(SR); } @Override Publicoperatedclientconnection createconnection () {return Newmyclientconnection (); }    }        classMylineparserextendsBasiclineparser {@Override PublicHeader Parseheader (FinalChararraybuffer Buffer) {            Try {                return Super. Parseheader (buffer); } Catch(Exception ex) {//suppressing parseexception anomalies                return NewBasicheader ("Invalid", buffer.tostring ()); }        }    }}
Mybasicclientconnectionmanager

And I use is multithreadedhttpconnectionmanager, do not try, so I look at the log of abnormal Trac, see the httpclient of the specific Excute method in the code, the blind dick guess the Next, I think we need to get rid of the original GetMethod.

/** *  */ Packagecom.http;Importjava.io.IOException;ImportOrg.apache.commons.httpclient.Header;Importorg.apache.commons.httpclient.HttpConnection;Importorg.apache.commons.httpclient.HttpException;ImportOrg.apache.commons.httpclient.HttpParser;Importorg.apache.commons.httpclient.HttpState;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;/** * @authorYingzi **/ Public classMyhttpgetmethodextendsOrg.apache.commons.httpclient.methods.GetMethod {Private Static FinalLogger log = Loggerfactory.getlogger (Myhttpgetmethod.class );  PublicMyhttpgetmethod () {Super(); }         Publicmyhttpgetmethod (String url) {Super(URL); } @Overrideprotected voidReadresponseheaders (httpstate State, httpconnection conn)throwsIOException, HttpException {getresponseheadergroup (). Clear (); Header[] Headers= {NewHeader ("Connection", "keep-alive"),NewHeader ("Content-type", "text/html; charset=gb18030"),NewHeader ("Keep-alive", "timeout=20"),NewHeader ("Cache-control", "max-age=3600")}; Try{Headers=httpparser.parseheaders (Conn.getresponseinputstream (), Getparams (). Gethttpelementcharset                ()); } Catch(Exception ex) {//suppressing parseexception anomaliesLog.warn ("Response header has some error info, can not parse normally."); }                          //Wire logging moved to HttpparserGetresponseheadergroup (). setheaders (headers); }    }

HttpClient How to receive malformed response header information

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.