找不到HttpClient Intermittent Page

时间:2013-04-02 20:19:08

标签: java apache-httpclient-4.x

我使用下面的代码并多次运行该方法,有几次我在GZIP中得到了一个响应,这是我所期望的,还有一些我得到完全不同的响应(未找到非GZIP页面)。但是,如果我使用Mozilla或IE多次下载相同的URL,我会始终获得相同的GZIP响应。

这是我尝试访问的服务器的错误,还是我需要设置任何参数才能获得一致的响应?

我尝试下载is the following的网址,您能告诉我吗?

public static byte[] dowloadURL(URL urlToDownload) {

    InputStream iStream = null;
    byte[] urlBytes = null;

    try {

        //HttpClient httpClient = new HttpClient();
        org.apache.http.client.
        HttpClient httpClient = new DefaultHttpClient();


        HttpGet httpget = new HttpGet(urlToDownload.toString());

        HttpResponse response  = httpClient.execute(httpget);

        iStream = response.getEntity().getContent();

        urlBytes = IOUtils.toByteArray(iStream);
        String responseString = new String(urlBytes);
        System.out.println(" >>> The response  string for " +urlToDownload.toString()+  " is "   +responseString);

    } catch (IOException e) {
        System.err.printf("Failed while reading bytes from %s: %s",
                urlToDownload.toExternalForm(), e.getMessage());
        e.printStackTrace();
        // Perform any other exception handling that's appropriate.
    } finally {
        if (iStream != null) {
            try {
                iStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return urlBytes;
}

0 个答案:

没有答案