http请求之编码问题

来源:互联网 发布:智能中医处方软件 编辑:程序博客网 时间:2024/05/30 04:20

最近恶搞朋友网站的时候,提交http请求的时候出了一个乱码问题。于是做了个编码请求测试,

首先,测试的servlet的doPost方法如下

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
System.out.println("方法:"+request.getMethod());
System.out.println("name:"+request.getParameter("name"));
}

然后用httpClient发送post请求

public static void main(String[] args) throws Exception {
List<NameValuePair> l = new ArrayList<NameValuePair>();
NameValuePair pair1 = new BasicNameValuePair("name","小怪");
l.add(pair1);
SendParamtersByPost.Send("http://localhost:8080/ceshi/Ceshi", l);
}

发现结果如下

是,在浏览器中用form表单提交

结果为

猜测,是编码问题。于是乎我将字符串转成了ISO-8859-1编码后再发请求 


结果为正常:

因此推测,浏览器是先将各种编码全部转成了ISO-8859-1编码进行发送,不管你设置的是不是utf-8编码

0 0
原创粉丝点击