71 lines
2.5 KiB
Java
71 lines
2.5 KiB
Java
|
/*
|
||
|
* @Author: Kane
|
||
|
* @Date: 2022-04-18 11:37:33
|
||
|
* @LastEditors: Kane
|
||
|
* @LastEditTime: 2022-04-18 11:41:09
|
||
|
* @FilePath: \undefinedd:\develop\产险厦门分公司项目\天气灾害预警\文档\example.java
|
||
|
* @Description:
|
||
|
*
|
||
|
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||
|
*/
|
||
|
import org.apache.commons.httpclient.HttpClient;
|
||
|
import org.apache.commons.httpclient.HttpStatus;
|
||
|
import org.apache.commons.httpclient.methods.PostMethod;
|
||
|
import org.apache.commons.httpclient.methods.StringRequestEntity;
|
||
|
|
||
|
public class HttpUtils {
|
||
|
public static String sendPostWithJson(String url, String jsonStr, HashMap<String,String> headers) {
|
||
|
// 返回的结果
|
||
|
String jsonResult = "";
|
||
|
|
||
|
try {
|
||
|
HttpClient client = new HttpClient();
|
||
|
// 连接超时
|
||
|
client.getHttpConnectionManager().getParams().setConnectionTimeout(3*1000);
|
||
|
// 读取数据超时
|
||
|
client.getHttpConnectionManager().getParams().setSoTimeout(3*60*1000);
|
||
|
client.getParams().setContentCharset("UTF-8");
|
||
|
|
||
|
PostMethod postMethod = new PostMethod(url);
|
||
|
|
||
|
postMethod.setRequestHeader("content-type", headers.get("content-type"));
|
||
|
|
||
|
// 非空
|
||
|
if (null != jsonStr && !"".equals(jsonStr))
|
||
|
{
|
||
|
StringRequestEntity requestEntity = new StringRequestEntity(jsonStr, headers.get("content-type"), "UTF-8");
|
||
|
postMethod.setRequestEntity(requestEntity);
|
||
|
}
|
||
|
|
||
|
int status = client.executeMethod(postMethod);
|
||
|
|
||
|
if (status == HttpStatus.SC_OK) {
|
||
|
jsonResult = postMethod.getResponseBodyAsString();
|
||
|
} else {
|
||
|
throw new RuntimeException("接口连接失败!");
|
||
|
}
|
||
|
|
||
|
} catch (Exception e) {
|
||
|
throw new RuntimeException("接口连接失败!");
|
||
|
}
|
||
|
return jsonResult;
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args)
|
||
|
{
|
||
|
String requestUrl = "http://localhost:8070/test/rz/server/rzxx/at_VaildToken.do";
|
||
|
String jsonStr = "{\"name\":\"张三\"}";
|
||
|
|
||
|
HashMap<String, String> headers = new HashMap<>(3);
|
||
|
headers.put("content-type", "application/json");
|
||
|
|
||
|
// 发送post请求
|
||
|
String resultData = HttpUtils.sendPostWithJson(requestUrl, jsonStr,headers);
|
||
|
|
||
|
// 并接收返回结果
|
||
|
System.out.println(resultData);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|