调用http:
qingqibing版本:
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
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.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Description: http请求工具类
* @Author
* @Date 2023/8/21 15:25
* @Version 1.0
*/
public class HttpClientUtils{
private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
// 请求成功
public final static int OK = 200;
/**
* post请求
* @param url 请求url
* @param param 参数(使用&拼接好的参数)
* @return String 返回结果
*/
public static String sendPost(String url, String param) {
String result = "";
try {
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(
SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(scsf).build();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
StringEntity entity = new StringEntity(param, ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8));
httppost.setEntity(entity);
//构建超时等配置信息
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) //连接超时时间
.setConnectionRequestTimeout(1000) //从连接池中取的连接的最长时间
.setSocketTimeout(10 * 1000) //数据传输的超时时间
.build();
httppost.setConfig(config);
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
HttpEntity resEntity = response.getEntity();
//回复接收
result = EntityUtils.toString(resEntity, "UTF-8");
}
} catch (Exception e) {
logger.error("请求融云失败\nurl:{}\nparam:{}\nmessage:{}", url, param, e.getMessage(), e);
}
return result;
}
}
使用::::::::::::::::::
转换md5