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行~~
这个帖子讨论过,但是没有说解决办法。
这个url在浏览器中直接打开是有返回结果的,但是在代码中执行就会出错
throws ClientProtocolException, IOException
{
String result = "";
url="http://192.168.1.83/imonitorint.html?op=r&33032=0";
HttpGet httpGet = new HttpGet(url);
HttpResponse response = new DefaultHttpClient().execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "GB2312");
}
return result;
}
DefaultHttpClient client = new DefaultHttpClient()
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
HttpResponse response = client.execute(httpGet);
试试看?
服务器那端是你们自己写的吗?
能debug下那里看看,httpclient过来和浏览器过来有什么不一样吗?
你们那个url是输出xml? 下载文件?
请检查服务器端contentType 和header内容等。
你装个httpwatch,一个ie的插件。
可以看服务器端返回的response的全部内容。
比如httpheader。猜猜看那里比较奇怪造成 httpclient解析header失败了。(DefaultResponseParser.parseHead)
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行~~
我看你第一张图的时候发的贴
果然没有收到的header。
我觉得是服务器端程序有问题的吧。应该你们这里改得话,你只能去改httpclient的库吧。
或者有什么设置可以让httpclient不要去做parseHead
这里有片文章介绍,但是介绍的不够详细。
http://www.cnblogs.com/loveyakamoz/archive/2011/07/21/2113251.html