不同浏览器在地址栏中直接输入中文参数的结果

IE8:(至少在WinXP SP3上)会对直接输入的url进行GBK编码
Chrome:UTF-8编码
Firefox:UTF-8编码


需求:在服务器后台打印出每个URL参数及其值,并回送到浏览器。


代码:

需要注意IE8(真NMD风骚 -_-#)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html;charset=utf-8");
  PrintWriter out = response.getWriter(); 

  String query =request.getQueryString();
  if(query == null || query.isEmpty()){
    return;
  }

  if(!isIE8(request.getHeader("User-Agent"))){
    query = URLDecoder.decode(query, "utf-8");
  }else{
    //IE8的编码真尼玛的一个骚货!!!FUCK OFF
    query = new String(query.getBytes("iso-8859-1"), "gbk");
    query = URLDecoder.decode(query, "gbk"); 
    System.out.println(query);
  }

  String params[] = query.split("&"); 
  for(String s : params){
    System.out.println(s);
    out.println(s+"<br/>");
  }
}
​
​
private boolean isIE8(String userAgent){
  if(userAgent==null || userAgent.isEmpty()){
    return false;
  }

  if(userAgent.contains("Trident/4.0")){
    return true;
  }

  return false;
}

 


 

posted @ 2020-09-25 11:20  少司命  阅读(271)  评论(0编辑  收藏  举报