-1

ミステリアスな私に問題あり!

HttpClient() クラスを使用して Web ページのコンテンツを取得します。

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