第一种:需要httpclient的Maven依赖包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
代码:
public static String callBgrsjk(String requestParams) {
String url = null;
JSONObject jb=new JSONObject();
jb.put("code",0);
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(300 * 1000)
.setConnectTimeout(300 * 1000)
.build();
url = "http://URL:Port/地址";
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
post.setHeader("Content-Type","application/json;charset=utf-8");
StringEntity postingString = new StringEntity(requestParams,
"utf-8");