gpt4 book ai didi

java - 使用 httpclient 有没有办法通过 HEAD 请求获取页面的字符集?

转载 作者:行者123 更新时间:2023-12-02 05:58:27 28 4
gpt4 key购买 nike

我正在使用 httpclient 库执行基本的 HEAD 请求。我很好奇如何获得 apache 返回的字符集,例如:utf-8、iso-8859-1 等...谢谢!

  HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 2000);
HttpConnectionParams.setSoTimeout(httpParams, 2000);

DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
httpclient.getParams().setParameter("http.useragent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

HttpContext localContext = new BasicHttpContext();
httpget = new HttpHead(url);

HttpResponse response = httpclient.execute(httpget, localContext);

this.sparrowResult.statusCode = response.getStatusLine().getStatusCode();

工作结果已更新

Header contentType = response.getFirstHeader("Content-Type");
String charset= contentType.getValue();

最佳答案

如果使用 HttpClient 4.2

import java.nio.charset.Charset;
import org.apache.http.entity.ContentType;

ContentType contentType = ContentType.getOrDefault(entity);
Charset charSet = contentType.getCharset();

关于java - 使用 httpclient 有没有办法通过 HEAD 请求获取页面的字符集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3216730/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com