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
执行的代码如下:
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; }
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行~~
[解决办法]
很想知道你最后是怎么解决的我今天也遇到了同样的问题