zoukankan      html  css  js  c++  java
  • HttpClient无乱码

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
        public static String doPost(String url, String json) {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            String result = "";
            try {
                StringEntity s = new StringEntity(json,"utf-8");
                s.setContentEncoding("UTF-8");
                s.setContentType("application/json");//设置contentType
                post.addHeader("Content-Type", "application/json;charset=UTF-8");
                post.setEntity(s);
                HttpResponse res = httpclient.execute(post);
                if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    HttpEntity entity = res.getEntity();
                    result = EntityUtils.toString(res.getEntity());
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return result;
        }
  • 相关阅读:
    Stepping Number
    Replace String
    String Permutation
    Clock Angle
    Keypad Permutation
    Replace Words
    1、奉加微 PHY6202 Get started
    Python3 修改二进制文件
    Google Fast Pair
    python 校验 BLE resolvable private address
  • 原文地址:https://www.cnblogs.com/julian-chang/p/11883669.html
Copyright © 2011-2022 走看看