package com.sinops.idap.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NamevaluePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNamevaluePair;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;
public class HttpClientUtils {
private static RequestConfig requestConfig = null;
private static String charset = “utf-8”;
public static void main(String[] args) {
JSonObject tt = new JSonObject();
tt.put(“lib_id”, “”);
tt.put(“face_id”, “”);
JSonObject res = httpPost(“http://ip:port/xxx”, tt);
System.out.println(“返回信息:” + res);
}
static {
// 设置请求连接和传输超时时间
requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
}
public static JSonObject httpPost(String url, Object obj) {
// post请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
JSonObject jsonResult = null;
HttpPost httpPost = new HttpPost(url);
// 设置请求和传输超时时间
httpPost.setConfig(requestConfig);
try {
if (null != obj) {
StringEntity entity = null;
if (obj instanceof String) {
entity = new StringEntity(obj.toString(), charset);
} else {
entity = new StringEntity(JSON.toJSonString(obj), charset);
}
entity.setContentEncoding(charset);
entity.setContentType(“application/json;charset=UTF-8”);
httpPost.setHeader(“Accept”,“application/json”);
httpPost.setEntity(entity);
}
CloseableHttpResponse response = httpClient.execute(httpPost);
return convertResponse(response);
} catch (Exception e) {
System.out.printf(“error HttpClientUtils {} - {} - {}” + url, obj, e.getMessage());
} finally {
httpPost.releaseConnection();
}
return jsonResult;
}
} catch (Exception e) { private static JSonObject convertResponse(CloseableHttpResponse response) throws IOException, ParseException { }
public static JSonObject httpPostForm(String url, Map
// post请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
JSonObject jsonResult = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
try {
if (null != params) {
//组织请求参数
List paramList = new ArrayList();
if (params != null && params.size() > 0) {
Set keySet = params.keySet();
for (String key : keySet) {
paramList.add(new BasicNamevaluePair(key, params.get(key)));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(paramList, charset));
}
CloseableHttpResponse response = httpClient.execute(httpPost);
return convertResponse(response);
} catch (IOException e) {
System.out.printf(“post请求提交失败:” + url, e);
} finally {
httpPost.releaseConnection();
}
return jsonResult;
}
public static JSonObject httpGet(String url) {
// get请求返回结果
JSonObject jsonResult = null;
CloseableHttpClient client = HttpClients.createDefault();
// 发送get请求
HttpGet request = new HttpGet(url);
request.setConfig(requestConfig);
try {
CloseableHttpResponse response = client.execute(request); jsonResult = convertResponse(response);
System.out.printf(“get请求提交失败:” + url, e);
} finally {
request.releaseConnection();
}
return jsonResult;
}
// 请求发送成功,并得到响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity, “utf-8”);
// 把json字符串转换成json对象
JSonObject resJson = JSONObject.parseObject(strResult);
return JSONObject.parseObject(strResult);
} else {
System.out.printf(" {} ", response);
}
return null;
}



