首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

httpClient 错误 The server failed to respond with a valid HTTP response

2012-08-29 
httpClient 异常 The server failed to respond with a valid HTTP response使用 httpClient 从服务器端读

httpClient 异常 The server failed to respond with a valid HTTP response
使用 httpClient 从服务器端读取数据,出现异常 : 
Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
执行的代码如下: 

Java code
public static String getResultByHttpGet()            throws ClientProtocolException, IOException    {        String result = "";                String url="http://192.168.1.83/imonitorint.html?op=r&33032=0";        HttpGet httpGet = new HttpGet();                //执行这行代码时出错        HttpResponse response = new DefaultHttpClient().execute(httpGet);        if (response.getStatusLine().getStatusCode() == 200)        {            HttpEntity entity = response.getEntity();            result = EntityUtils.toString(entity, "GB2312");        }        return result;    }

全部的错误信息如下: 

org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:822)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at com.osee.yws.httpAgreement.HttpAgreement.getResultByHttpGet(HttpAgreement.java:50)
at com.osee.yws.bll.LCDOptionBll.readLcdOption(LCDOptionBll.java:104)
at com.osee.yws.bll.LCDOptionBll.readInput(LCDOptionBll.java:73)
at com.osee.yws.gui.SetDeviceOptionLCD$1.windowOpened(SetDeviceOptionLCD.java:74)
at java.awt.AWTEventMulticaster.windowOpened(Unknown Source)
at java.awt.AWTEventMulticaster.windowOpened(Unknown Source)
at java.awt.Window.processWindowEvent(Unknown Source)
at javax.swing.JDialog.processWindowEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:109)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:281)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)


at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:645)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
... 33 more


[解决办法]
HttpGet httpGet = new HttpGet();括号里应该有网址的,你把url放进去试试吧。
[解决办法]
89 @Override
90 protected HttpMessage parseHead(
91 final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
92 //read out the HTTP status string
93 int count = 0;
94 ParserCursor cursor = null;
95 do {
96 // clear the buffer
97 this.lineBuf.clear();
98 int i = sessionBuffer.readLine(this.lineBuf);
99 if (i == -1 && count == 0) {
100 // The server just dropped connection on us
101 throw new NoHttpResponseException("The target server failed to respond");
102 }
103 cursor = new ParserCursor(0, this.lineBuf.length());
104 if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
105 // Got one
106 break;
107 } else if (i == -1 || count >= this.maxGarbageLines) {
108 // Giving up
109 throw new ProtocolException("The server failed to respond with a " +
110 "valid HTTP response");
111 }
112 if (this.log.isDebugEnabled()) {
113 this.log.debug("Garbage in response: " + this.lineBuf.toString());
114 }
115 count++;
116 } while(true);
117 //create the status line from the status string
118 StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
119 return this.responseFactory.newHttpResponse(statusline, null);
120 }

看109行~~
[解决办法]
很想知道你最后是怎么解决的我今天也遇到了同样的问题

热点排行
Bad Request.