我使用以下代码并多次运行该方法,有几次我在 GZIP 中得到了我所期望的响应,而其他几次我得到了完全不同的响应(未找到非 GZIP 页面)。但是,如果我使用 Mozilla 或 IE 多次下载相同的 URL,我始终会得到相同的 GZIP 响应。
这是我尝试访问的服务器的错误,还是我需要设置任何参数以获得一致的响应?
我要下载的网址如下,你能告诉我吗?
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;
}