日期:2014-05-19  浏览次数:20825 次

httpClient如何接收格式错误的响应头部信息
使用 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(url);
                //执行这行代码时出错
        HttpResponse response = new DefaultHttpClient().execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200)
        {
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "GB2312");
        }
        return result;
    }

 
String url="http://192.168.1.83/imonitorint.html?op=r&33032=0";
这个url在浏览器中直接打开是有返回结果的,但是在代码中执行就会出错
通过ie浏览器插件httpwatch,查看服务器端返回的response的全部内容,发现返回的header为空。
如下图 :

如何使HttpClient接受格式错误的响应头部信息,博客网上有人介绍,但是不够详细: 
http://www.cnblogs.com/loveyakamoz/archive/2011/07/21/2113251.html
之前发帖讨论过这个问题,但是没有解决: 
http://topic.csdn.net/u/20120816/16/f1092599-6b75-4216-aa24-b5f3f95fde6d.html?seed=814375011&r=79432055#r_79432055

全部的错误信息 :
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.