import com.sb.yldf.utils.releseUtil.ReleseUtil;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
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.BasicHeader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
-
用于模拟HTTP请求中GET/POST方式
*/
public class HttpClientPostJson {
protected static final Logger logger = LoggerFactory.getLogger(HttpClientPostJson.class);/**
-
Post请求方法(基于项目需求,想要实现Post消息推送,故采用HttpClient组件进行实现)
-
20221117增加RequestConfig中配置请求,接收超时时间
*/
public static String sendPost(String url, String input) {
//返回结果
String output = “”;
try {
//创建方法一
//@SuppressWarnings(“resource”)
//DefaultHttpClient httpClient = new DefaultHttpClient();//创建方法二 CloseableHttpClient httpClient = HttpClients.createDefault(); //设置RequestConfig接收超时时间 RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(60000)//请求获取数据的超时时间,单位毫秒。如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用 .setConnectTimeout(10000)//设置连接超时时间,单位毫秒 .setConnectionRequestTimeout(60000)//设置从connect Manger获取Connection超时时间,单位毫秒 .build(); HttpPost postRequest = new HttpPost(url); postRequest.setConfig(requestConfig);//增加属性 //构建消息实体 StringEntity entity = new
-