方式一:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.entity.StringEntity;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.invoke.MethodHandles;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class TestInfrom {
public static void main(String[] args) throws Exception {
String requestPath= "https://xxx:xxx/xxx";
TestReqBean testReqBean = new TestReqBean();
String json= GsonUtil.getInstance().toJson(testReqBean);
String res= doPost(requestPath, json,"application/json","utf8");
TestRespBean data=GsonUtil.getInstance().fromJson(res, TestRespBean.class);
System.out.println("inform_begin>>requestPath:" + requestPath+",data="+data);
}
public static String doPost(String url, String stringJsonPrama, String contentType, String encode) {
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
if (null == stringJsonPrama || null == url || null == encode) {
return "Parameters error";
}
try {
if (null == httpClient)
httpClient = HttpClients.createDefault();
if (null == httpPost)
httpPost = new HttpPost(url);
// 设置参数
StringEntity sen = new StringEntity(stringJsonPrama, encode);
sen.setContentEncoding(encode);
sen.setContentType(contentType);
httpPost.setEntity(sen);
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
try {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, encode);
}
} catch (Exception e) {
LOG.error("http pos throw Exception", e);
}
}
} catch (ConnectionPoolTimeoutException e) {
LOG.error("http pos throw ConnectionPoolTimeoutException(wait time out)", e.getMessage());
} catch (ConnectTimeoutException e) {
LOG.error("http pos throw ConnectTimeoutException", e.getMessage());
} catch (SocketTimeoutException e) {
LOG.error("http pos throw SocketTimeoutException", e.getMessage());
} catch (ClientProtocolException e) {
LOG.error("http pos throw ClientProtocolException", e.getMessage());
} catch (IOException e) {
LOG.error("http pos throw IOException", e.getMessage());
} catch (Exception e) {
LOG.error("http pos throw Exception", e.getMessage());
} finally {
if (null != httpClient) {
httpClient.getConnectionManager().shutdown();
}
}
return result;
}
}
方式二:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.entity.StringEntity;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.invoke.MethodHandles;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class TestInfrom{
public static void main(String[] args) throws Exception {
String url= "https://xxx:xxx";
TestReqBean testReqBean = new TestReqBean();//传参bean
String json= GsonUtil.getInstance().toJson(testReqBean);
JSONObject res= postHttps(url, JSON.parseObject(json));
System.out.println("inform_begin>>requestPath:" + requestPath+",res="+res);
}
public static JSONObject postHttps(String url, JSONObject parameters) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
// 创建参数队列
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Set<String> keySet = parameters.keySet();
for (String key : keySet) {
String value = parameters.get(key).toString();
if (value.equals("null") != true) {
nameValuePairs.add(new BasicNameValuePair(key, value));
}
}
return executeSendRequest(httpClient,httpPost,nameValuePairs);
}
private static JSONObject executeSendRequest(CloseableHttpClient httpclient, HttpPost httpPost, List<NameValuePair> nameValuePairs) throws Exception {
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
httpPost.setEntity(uefEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
return covertEntityToJSON(response.getEntity());
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private static JSONObject covertEntityToJSON(HttpEntity entity) throws Exception {
if (entity != null) {
String res = EntityUtils.toString(entity, "UTF-8");
JSONObject jsonObject = JSONObject.parseObject(res);
return jsonObject;
} else {
return null;
}
}
}