为了让HttpClient正确解码站点,我们需要确保以下几点:
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.example.com");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
// 获取响应内容的字符编码
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
// 根据字符编码解码响应内容
String content = EntityUtils.toString(entity, charset);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.example.com");
httpGet.addHeader("Accept-Encoding", "gzip, deflate");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
// 解码响应内容
InputStream inputStream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
inputStream = new GZIPInputStream(inputStream);
} else if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("deflate")) {
inputStream = new InflaterInputStream(inputStream);
}
// 读取解码后的内容
String content = IOUtils.toString(inputStream, charset);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.example.com");
HttpResponse response = httpClient.execute(httpGet);
// 检查是否为重定向响应
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_SEE_OTHER || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
// 获取重定向的URL
Header locationHeader = response.getFirstHeader("Location");
String redirectUrl = locationHeader.getValue();
// 创建新的请求并执行
httpGet = new HttpGet(redirectUrl);
response = httpClient.execute(httpGet);
}
// 获取响应内容
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity, charset);
需要注意的是,以上代码示例中使用的是Apache HttpClient库进行HTTP请求。另外,为了解码响应内容,我们需要添加Apache Commons IO库和Apache HttpComponents库的依赖。
以上是让HttpClient正确解码站点的基本步骤和代码示例。根据实际情况和需求,可能还需要进行其他操作,例如处理Cookie、设置代理等。在实际开发中,可以根据具体的站点和需求进行调整和优化。
领取专属 10元无门槛券
手把手带您无忧上云