http工具类 james

package com.shsnc.south.znpact.server.util;

import com.shsnc.south.znpact.server.system.constant.HttpConstants;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
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.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author HuangHJ
* @date 2020/3/9 17:18
*/
public class HttpClientUtil {

private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

/**
* 发送post请求
* @param url 请求地址
* @param json 请求参数
* @return
*/
public static JSONObject sendPost(String url, String json) throws JSONException {

//构建http客户端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Post请求
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(json, "UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
//设置请求报文
httpPost.setEntity(entity);
// 响应模型, 构建响应模型
CloseableHttpResponse response = null;
// 由客户端执行(发送)Post请求
try {
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
logger.info("url:{}, response code: {}", url, response.getStatusLine());
if (responseEntity != null) {
String responseBody = EntityUtils.toString(responseEntity);
logger.info("url:{}, response body: {}", url, responseBody);
return new JSONObject(responseBody);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

/**
* 根据传进来的Map参数转化成JSONObject的公共方法
* @param params
* @return
*/
public static JSONObject createRequestJson (Map<String,Object> params) {
JSONObject jsonObj = new JSONObject(params);
return jsonObj;
}

public static void main(String[] args) throws Exception{
JSONObject obj = new JSONObject();
obj.put("mockServerId", "test-baidu-7633");
JSONArray proxyingRules = new JSONArray();
JSONObject rule = new JSONObject();
rule.put("serverPort", 1081);
rule.put("proxyRemotePort", 433);
rule.put("proxyRemoteHost", "www.baidu.com");
rule.put("proxyRemoteScheme", "HTTP");
proxyingRules.put(rule);
obj.put("proxyingRules", proxyingRules);
String ip = "192.168.1.43";
Integer port = 27003;
String url = String.format(HttpConstants.MOCK_PROXY_START_URL, ip, port);
logger.info("url:{}, json:{}", url, obj.toString());
JSONObject res = sendPost(url, obj.toString());
logger.info("res:{}", res.toString());

}
}
posted @ 2021-11-14 21:39  james4515  阅读(58)  评论(0)    收藏  举报