主要使用的httpClient的post方法:直接上代码
添加依赖:
<!--httpcomponents的包-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<!--JSONobject的包-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
工具类:
package com.upiweb.common.util;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/**
* 调用的是接口
*/
public class SendSignHttpsClient {
/**
* post请求以及参数是json
*
* @param url
* @param jsonParams
* @return
*/
public static JSONObject doPostForJson(String url, String jsonParams) {
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject jsonObject = null;
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().
setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
.setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "application/json");
try {
httpPost.setEntity(new StringEntity(jsonParams, ContentType.create("application/json", "utf-8")));
System.out.println("request parameters" + EntityUtils.toString(httpPost.getEntity()));
System.out.println("httpPost:" + httpPost);
HttpResponse response = httpClient.execute(httpPost);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
String result = EntityUtils.toString(response.getEntity());
System.out.println("result:" + result);
jsonObject = JSONObject.fromObject(result);
return jsonObject;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonObject;
}
}
}
测试代码:
public static void main(String []args)
{
try
{
SendSignHttpsClient restUtil = new SendSignHttpsClient();
JSONObject resultString = restUtil.doPostForJson("http://47.XX.XX.202:8080/ttsm/jkm/checkJkm","{\"idCard\":\"33102*******8470\",\"name\":\"张三\"}");
} catch (Exception e)
{ // TODO: handle exception System.out.print(e.getMessage());}
}
}
}
返回结果: