HttpClient Get/Post请求调用(请求头 请求体设置)

Get请求:

     // 创建Httpclient对象

   CloseableHttpClient httpClient = HttpClients.createDefault();

String resultString = "";
CloseableHttpResponse response = null;
//参数Map
Map<String,String> param = new HashMap<>();
param.put("xx",xxxx);

//请求头
Map<String,String> headers= new HashMap<>();
headers.put("xx",xxxx);

//结果集转换
ObjectMapper mapper = new ObjectMapper();
try {
    // 创建uri
    URIBuilder builder = new URIBuilder("url");
   //传递参数
    if (param != null) {
        for (String key : param.keySet()) {
            builder.addParameter(key, param.get(key));
        }
    }
    URI uri = builder.build();

    // 创建http GET请求
    HttpGet httpGet = new HttpGet(uri);
   //传递请求头
   if (headers != null) {
      for (String  key : headers.keySet()) {
        httpGet.addHeader(key,headers.get(key));
      }
     }
     httpGet.addHeader("Accept-Charset", "utf-8");
     httpGet.addHeader("contentType", "utf-8");
     httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0)    Gecko/20100101 Firefox/37.0");
     httpGet.addHeader("Content-Type", "application/json");

    // 执行请求
    response = httpclient.execute(httpGet);
    // 判断返回状态是否为200
    if (response.getStatusLine().getStatusCode() == 200) {
        resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
        JsonNode rootNode = mapper.readTree(result);
        //根据需求取对应数据
        String xxx =   rootNode.get("xxxxx").asText();
    }
} catch (Exception e) {
    e.printStackTrace();
    log.error("", e);
} finally {
    try {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Post请求:

// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
//  参数Map  存入body体
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put(key  ,value)
String param =  objectNode.toString();

//请求头
Map<String,String> headers= new HashMap<>();

try {
    // 创建Http Post请求
    HttpPost httpPost = new HttpPost(url);

   //添加请求头
    if (headers != null) {
        for (String  key : headers.keySet()) {
            httpPost.addHeader(key,headers.get(key));
        }
    }
    httpPost.addHeader("Accept-Charset", "utf-8");
    httpPost.addHeader("contentType", "utf-8");
    httpPost.addHeader("Content-Type", "application/json");

   //添加请求体
    StringEntity entity = new StringEntity(param, ContentType.APPLICATION_JSON);
    httpPost.setEntity(entity);


    // 执行http请求
    response = httpClient.execute(httpPost);
    resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
              xxx
} finally {
              xxxx
}

    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值