原文地址:http://blog.csdn.net/zelda002/article/details/8563222
httpClient java.net.URLEncoder.encode二次转码问题
我在servlet中调用java.net.URLDecoder.decode(request.getParameter("name"), "UTF-8")方法和调用java.net.URLDecoder.decode(request.getQueryString(), "UTF-8")所得到的结果是不一样的,就是由于在request.getParameter("name")之前会自动做一次解码的工作,而且是默认的ISO-8859-1。
所以,在使用java.net.URLEncoder.decode()和java.net.URLDecoder.decode(),的时候需要在前端页面中使用两次java.net.URLDecoder.decode()方法。
使用两次编码的过程相当于如下代码:
String name=java.net.URLEncoder.encode("测试", "UTF-8");
System.out.println(name);
name=java.net.URLEncoder.encode(name,"UTF-8");
System.out.println(name);
name=java.net.URLDecoder.decode(name, "UTF-8");
System.out.println(name);
System.out.println(java.net.URLDecoder.decode(name, "UTF-8"));
输出为:
%E6%B5%8B%E8%AF%95
%25E6%25B5%258B%25E8%25AF%2595
%E6%B5%8B%E8%AF%95
测试
第一次编码后将汉字编码为%和字母数字的格式,而第二次编码的时候是对%字母数字进行编码,虽然解码的时候使用的是ISO-8859-1,但是对于%和字母数字而言用ISO-8859-1和UTF-8解码出来的是一样的,此时就回到了汉字被编码过一次的字符串了,当再次进行解码的时候使用UTF-8就回将它转会汉字;
具体实现:
new Thread(new Runnable() {
@Override
public void run() {
httpClient会二次把%转码
/*HttpMethod httpMethod = new GetMethod();
String httpurl = Key.LOGGER_URL + "?category=server&permanent=" + cookies + "&user_hash=" + tempUid
+ "&product=wap" + "&version=1.2&view=register&action=" + promptEn + "&channel=" + sid
+ "&source=&referer=" + referer1
+ "&user_agent=" + java.net.URLEncoder.encode(request1.getHeader("user-agent"))
+ "&created=" + System.currentTimeMillis()
+ "&client_ip=&url=" + url1;
try {
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
httpMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 30000);
httpMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
httpMethod.getParams().setParameter("user-agent", "wap.zch168.com");
// System.out.println("httpurl:::" + httpurl);
httpMethod.setURI(new URI(httpurl, false, "utf-8"));
httpClient.executeMethod(httpMethod);
} catch (Exception e) {
e.printStackTrace();
}
httpMethod.releaseConnection();*/
//后改用:
SafelotteryHttp http = new SafelotteryHttp();
try{
String httpurl = Key.LOGGER_URL + "?category=server&permanent=" + cookies + "&user_hash=" + tempUid
+ "&product=wap" + "&version=1.2&view=register&action=" + promptEn + "&channel=" + sid
+ "&source=&referer=" + referer1
+ "&user_agent=" + user_agent
+ "&created=" + System.currentTimeMillis()
+ "&client_ip=&url=" + url1;
http.doGet(httpurl, null, "UTF-8", true);
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
public static String doGet(String url, String queryString, String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
try {
if (StringUtils.isNotBlank(queryString))
//对get请求参数做了http请求默认编码,好像没有任何问题,汉字编码后,就成为%式样的字符串
method.setQueryString(URIUtil.encodeQuery(queryString));
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
String line;
while ((line = reader.readLine()) != null) {
if (pretty)
response.append(line).append(System.getProperty("line.separator"));
else
response.append(line);
}
reader.close();
}
} catch (URIException e) {
logger.error("执行HTTP Get请求时,编码查询字符串“" + queryString + "”发生异常!", e);
} catch (IOException e) {
logger.error("执行HTTP Get请求" + url + "时,发生异常!", e);
} finally {
method.releaseConnection();
}
return response.toString();
}