2013-07-19 88 views
-1

我有一个神秘的问题!HTTPClient.executeMethod()返回异常

我使用HttpClient()类来获取网页内容。

client = new HttpClient(); 
     client.getParams().setParameter(HttpMethodParams.USER_AGENT, useragent); 
     client.getParams().setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); 
     client.getParams().setParameter("http.protocol.allow-circular-redirects", "true"); 
     client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); 

[...] 

String html = getPage(client, new GetMethod(url)); 

和GETPAGE()方法:

public String getPage(HttpClient myClient, GetMethod myMethod) { 

     BufferedReader br = null; 
     String retval = new String(); 

     try { 

      int returnCode = myClient.executeMethod(myMethod); 

      if (returnCode != HttpStatus.SC_OK) { 
       System.err.println("Method failed: " + myMethod.getStatusLine()); 
      } else { 

       System.out.println("buffer : "); 
       br = new BufferedReader(new InputStreamReader(myMethod.getResponseBodyAsStream())); 
       String readLine; 
       while (((readLine = br.readLine()) != null)) { 
        //System.err.println(readLine); 
        retval = retval.concat(readLine); 
       } 

      } 
     } catch (Exception e) { 
      System.out.println("Exception e : "); 
      System.err.println(e); 
      System.out.println("END Exception e : "); 
     } finally { 
      myMethod.releaseConnection(); 
      if (br != null) { 
       try { 
        br.close(); 
       } catch (Exception fe) { 
        System.out.println("Exception fe : "); 
        System.err.println(fe); 
        System.out.println("END Exception fe : "); 
       } 
      } 
     } 
     return retval; 

    } 

这是shell结果:

Exception e : 
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean 
END Exception e : 

当我经过自己的代码加入Apache的代码示例(https://hc.apache.org/httpclient-3.x/tutorial.html),我取回成功html页面:

public String getPage(HttpClient myClient, GetMethod myMethod) { 

     BufferedReader br = null; 
     String retval = new String(); 

     try { 

      int returnCode = myClient.executeMethod(myMethod); 

      if (returnCode != HttpStatus.SC_OK) { 
       System.err.println("Method failed: " + myMethod.getStatusLine()); 
      } else { 

       System.out.println("buffer : "); 
       br = new BufferedReader(new InputStreamReader(myMethod.getResponseBodyAsStream())); 
       String readLine; 
       while (((readLine = br.readLine()) != null)) { 
       //System.err.println(readLine); 
       retval = retval.concat(readLine); 
       } 

      } 
     } catch (Exception e) { 
      System.out.println("Exception e : "); 
      System.err.println(e); 
      System.out.println("END Exception e : "); 
     } finally { 
      myMethod.releaseConnection(); 
      if (br != null) { 
       try { 
        br.close(); 
       } catch (Exception fe) { 
       } 
      } 
     } 

     try { 
      // Execute the method. 
      int statusCode = myClient.executeMethod(myMethod); 

      if (statusCode != HttpStatus.SC_OK) { 
       System.err.println("Method failed: " + myMethod.getStatusLine()); 
      } 

      // Read the response body. 
      byte[] responseBody = myMethod.getResponseBody(); 

      // Deal with the response. 
      // Use caution: ensure correct character encoding and is not binary data 
      System.out.println("here : " +new String(responseBody) +"\n enddd"); 

     } catch (HttpException e) { 
      System.err.println("Fatal protocol violation: " + e.getMessage()); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      System.err.println("Fatal transport error: " + e.getMessage()); 
      e.printStackTrace(); 
     } finally { 
      // Release the connection. 
      myMethod.releaseConnection(); 
     } 

     return retval; 

    } 

和外壳结果:

Exception e : 
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean 
END Exception e : 
juil. 19, 2013 6:53:26 PM org.apache.commons.httpclient.HttpMethodBase getResponseBody 
WARNING: Going to buffer response body of large or unknown size. Using getResponseAsStream instead is recommended. 
here : <!DOCTYPE html> 

[...] 

</html> 

enddd 
+0

我的括号太多了吗? – TheorY

+0

Lukas已经为您提供了解决方案 – 2013-07-19 19:20:45

回答

0

这是你的错误,产地:

client.getParams().setParameter("http.protocol.allow-circular-redirects", "true"); 

documentationhttp.protocol.allow-circular-redirects -property说:

名称http.protocol.allow-circular-redirects类型 :布尔型

因此它期望boolean,但你给它一个字符串。因此,平台尝试将字符串对象转换为布尔对象并失败(请参阅您的例外情况)。

要么使用setBooleanParameter(string, boolean)-method或摆脱报价:

client.getParams().setParameter("http.protocol.allow-circular-redirects", true); 

此外,供参考:一个例外是抛出,不予退换。

+0

感谢您的回复, 现在它工作得很好:) – TheorY