我有一个问题谁是神秘的!
我使用 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;
    }
这是外壳结果:
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