httpspost设置代理发送请求

httpspost设置代理发送请求
本⽂主要是对http和https 发送post请求所做⼯具类,⽅法中有两个参数:https(是否是https地址)和proxy(是否使⽤代理)。http和https主要使⽤apache的基础jar包,代理地址可从配置⽂件中获取。好了,废话不多说,直接上代码:
1. https请求类
public class HttpProxyPost {
private static Logger logger = Logger(HttpProxyPost.class);
private static PropertiesConfiguration config = Config();
private static CloseableHttpClient client = null;
public static String requestWithPost(String postUrl, JSONObject reqJson, Boolean https, Boolean proxy) {
HttpResponse rsp = null;
try {
client = HttpsClient(https);
} catch (Exception e) {
<("获取httpCliet失败");
}
HttpPost post = new HttpPost(postUrl);
if (proxy) {
科学发展观的基本内涵是什么String proxyIP = String("proxyIP");
int proxyPort = Integer.String("proxyPort"));
logger.info("使⽤代理发送请求:{}:{}", proxyIP, proxyPort);
HttpHost proxyHost = new HttpHost(proxyIP, proxyPort);
RequestConfig reqConfig = RequestConfig.custom().setProxy(proxyHost).build();
post.setConfig(reqConfig);
}
StringEntity params = null;
String rspStr = "";
try {
params = new String(), "utf-8");
params.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
params.setChunked(true);
post.addHeader("content-type", "application/json");
post.setEntity(params);
rsp = ute(post);
HttpEntity entity = Entity();
String rspString = String(entity, "utf-8");
JSONObject rspJson = JSONObject.parseObject(rspString);
logger.info("请求返回报⽂:{}", rspJson);
rspStr = String();双重阴谋
} catch (Exception e) {
logger.info("外调异常::{}", e.getMessage());
} finally {
}
return rspStr;
}
}
2. 创建https client
public class SSLClient {
private static HttpClientBuilder builder;
public static CloseableHttpClient getHttpsClient(Boolean https)
throws KeyManagementException, NoSuchAlgorithmException {
if (builder == null) {
builder = ate();
}
if (!https) {
return builder.build();
小额贷款公司改制设立村镇银行暂行规定} else {
X509TrustManager tm = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
哈贝马斯交往理论return null;
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
第十二届国家机构领导人
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
};无缝钢轨
SSLContext ctx = Instance("TLS");
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  builder.setSSLSocketFactory(ssf);
}
return builder.build();
}

本文发布于:2024-09-23 02:29:38,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/164631.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:代理   请求   发送   配置
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议