1.服务器可以设置编码格式
2.nginx也可以设置编码格式
3.httpCilent工具(jar包),会先获取response中返回的Content-Type中包含的编码格式charset,如果没取到,则取httpClient工具中的default charset 为ISO-8859-1,httpclient中有给各种协议中传输数据的编码格式默认设置。如需要改变,需要在调用的时候去设置
params.setHttpElementCharset("US-ASCII");//创建httpHeader的字符集默认值
params.setContentCharset("ISO-8859-1");//创建contentBody的字符集默认值
public static final String CREDENTIAL_CHARSET = "http.protocol.credential-charset";
public static final String HTTP_ELEMENT_CHARSET = "http.protocol.element-charset";
public static final String HTTP_URI_CHARSET = "http.protocol.uri-charset";//http.protocol.uri-charset这个值get方法先取,取不到httpclient中设置为utf-8,初始化的时候是没值的,就会取到utf-8
public static final String HTTP_CONTENT_CHARSET = "http.protocol.content-charset";
4.String的默认编码格式和系统有关系,String 的处理是通过getByte("charset")来处理的,会根据括号里的字符格式来转换成对应的字节码,如果String当前默认编码是utf-8格式的,getbyte中用iso-8859-1会出转换成乱码。
// String responseStr = resBuffer.toString();
// byte[] responseBody = responseStr.getBytes(getMethod.getResponseCharSet());
public static String httpVisit(String url, String charset) {
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
httpClient.setConnectionTimeout(1500);
// httpClient.getParams().setParameter("http.protocol.content-charset", charset);
// 创建GET方法的实例
GetMethod getMethod = new GetMethod(url);
getMethod.addRequestHeader("Connection", "close");
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());
}
// 返回响应消息
InputStream resStream = getMethod.getResponseBodyAsStream();
long contentLength = getMethod.getResponseContentLength();
if (contentLength > 2147483647L) {
throw new IOException("Content too large to be buffered: " + contentLength + " bytes");
}
int limit = getMethod.getParams().getIntParameter("http.method.response.buffer.warnlimit", 1048576);
if ((contentLength == -1L) || (contentLength > limit)) {
System.out.println("Too long");
}
ByteArrayOutputStream outstream = new ByteArrayOutputStream(contentLength > 0L ? (int) contentLength : 4096);
byte[] buffer = new byte[4096];
int len;
while ((len = resStream.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
outstream.close();
String content = new String(outstream.toByteArray(), charset);
// InputStream resStream = getMethod.getResponseBodyAsStream();
// BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
// StringBuffer resBuffer = new StringBuffer();
// String resTemp = "";
// while ((resTemp = br.readLine()) != null) {
// resBuffer.append(resTemp);
// }
// String responseStr = resBuffer.toString();
// byte[] responseBody = responseStr.getBytes(getMethod.getResponseCharSet());
// String content = new String(responseBody, charset);
// byte[] responseBody1 = getMethod.getResponseBodyAsString().getBytes(getMethod.getResponseCharSet());
// String content1 = new String(responseBody1, charset);
// return content1;
return content;
} catch (HttpException e) {// 发生致命的异常,可能是协议不对或者返回的内容有问题
LogHelper.exceptionLog(e);
return "-1";
} catch (IOException e) {
// 发生网络异常
LogHelper.exceptionLog(e);
return "-1";
} finally {
try {
// 释放连接
getMethod.releaseConnection();
httpClient.getHttpConnectionManager().closeIdleConnections(1);
} catch (Exception e) {
LogHelper.exceptionLog(e);
}
}
}