zoukankan      html  css  js  c++  java
  • HttpClient 发送 post 请求中文全是问号

    使用 HttpClient 发送 post 请求,但发出去的请求体里的英文正常,中文全是问号,需要设置以下

    HttpClient client = HttpClients.createDefault();
    
            // 构造 POST 参数
            ArrayList<NameValuePair> postData = new ArrayList<>();
            postData.add(new BasicNameValuePair("username", "name"));
            postData.add(new BasicNameValuePair("pwd", "pwd"));
    
            HttpPost post = new HttpPost("http://xxx.xx.com");
            // 首先Header部分需要设定字符集为:uft-8
            post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    		// 此处也需要设定
            post.setEntity(new UrlEncodedFormEntity(postData, "utf-8"));
            post.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            post.setHeader("Accept-Encoding", "gzip, deflate");
            post.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
            post.setHeader("Connection", "keep-alive");
            post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36");
    		// 执行请求
            HttpResponse response = client.execute(post);
    

    这样,发出去的中文参数便正常了

    没有修不好的电脑
  • 相关阅读:
    第二阶段个人冲刺总结01
    软件工程学习进度表13
    软件工程学习进度表12
    个人博客(09)
    个人博客(07)
    个人博客(08)
    poj1562 DFS入门
    poj3278 BFS入门
    数组单步运算
    十天冲刺
  • 原文地址:https://www.cnblogs.com/duniqb/p/12702427.html
Copyright © 2011-2022 走看看