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;
        }
  • 相关阅读:
    某题3
    无向图Tarjan&&求树直径
    分布式消息中间件(一)设计
    大数据算法(一)亚线性算法
    SparkSQL
    MySQL索引
    Spark程序设计
    Java高并发网络编程(五)Netty应用
    Java高并发网络编程(四)Netty
    Java高并发网络编程(三)NIO
  • 原文地址:https://www.cnblogs.com/julian-chang/p/11883669.html
Copyright © 2011-2022 走看看