HttpPost方式调用接口方式
第一步,添加依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</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");
post.setEntity(postingString);
HttpResponse response = httpClient.execute(post);
String content = EntityUtils.toString(response.getEntity());
System.out.println(content);
return content;
} catch (SocketTimeoutException e) {
LoggerUtil.error("调用Dat+"
+ ".aService接口超时,超时时间:" + 300
+ "秒,url:" + url + ",参数:" + requestParams, e);
return jb.toString();
} catch (Exception e) {
LoggerUtil.error("调用DataService接口失败,url:" + url + ",参数:" + requestParams,