zoukankan      html  css  js  c++  java
  • java http/https post/get 请求 ,携带header参数

    import com.alibaba.fastjson.JSONObject;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    
    import java.nio.charset.Charset;
    
    public class HttpPostJsonUtil {
    
        public static JSONObject doPost(String url, JSONObject jsonObject) {
            HttpClient client = HttpClientBuilder.create().build();
            HttpPost post = new HttpPost(url);
            JSONObject response = null;
            post.addHeader("Content-type","application/json; charset=utf-8");
            post.setHeader("Accept", "application/json");
    
            try {
                post.setEntity(new StringEntity(jsonObject.toString(), Charset.forName("UTF-8")));
                HttpResponse res = client.execute(post);
                if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    HttpEntity entity = res.getEntity();
                    String result = EntityUtils.toString(entity);
                    response = JSONObject.parseObject(result);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return response;
        }
    
        public static JSONObject doGet(String url) {
            HttpClient client = HttpClientBuilder.create().build();
            HttpGet get = new HttpGet(url);
            JSONObject response = null;
            get.addHeader("Content-type","application/json; charset=utf-8");
            get.setHeader("Accept", "application/json");
    
            try {
                HttpResponse res = client.execute(get);
                if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    HttpEntity entity = res.getEntity();
                    String result = EntityUtils.toString(entity);
                    response = JSONObject.parseObject(result);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return response;
        }
    
    }
    

     方法中,jsonObject 参数可以换成 list 或者 map ,只要将 

    jsonObject.toString() 换成
    JSONObject.toJSONString(list) 或者
    JSONObject.toJSONString(map)
     
  • 相关阅读:
    C++11 二叉堆
    OpenCV --- 实现两幅图像并排合并(ROI)
    OpenCV --- 修改图像的对比度、亮度 、RGB转Gray图像、修改图像的尺寸
    Opencv --- 图像像素遍历的各种方法
    Ubuntu系统的安装(虚拟机) 并配置C/C++编译器
    在Ubuntu下编译安装nginx
    【OpenCV3】threshold()函数详解
    MFC 剪切板的使用、线程介绍
    C++基础知识 基类指针、虚函数、多态性、纯虚函数、虚析构
    【OpenCV3】cvRound()、cvFloor()、cvCeil()函数详解
  • 原文地址:https://www.cnblogs.com/remember-forget/p/14840145.html
Copyright © 2011-2022 走看看