-1

我有一个问题谁是神秘的!

我使用 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
4

1 回答 1

0

这是您的错误来源:

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

-property的文档说:http.protocol.allow-circular-redirects

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

因此它需要 a boolean,但你给了它一个字符串。因此,平台尝试将字符串对象转换为布尔对象并失败(请参阅您的异常)。

要么使用setBooleanParameter(string, boolean)- 方法,要么去掉引号:

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

另外,仅供参考:抛出异常,不返回。

于 2013-07-19T19:17:56.390 回答