javaget请求跳过https
package com.wanwei.dataget.pdata.util;
import com.wanwei.dataget.pdata.entity.ProIp;
import com.wanwei.dataget.pdata.service.ProxyIpService;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
@RequiredArgsConstructor
public class HttpUtil {
private final ProxyIpService proxyIpService;
public static CloseableHttpClient httpClient;
{
SSLContext sslcontext = null;
try {
sslcontext = createIgnoreVerifySSL();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
//设置协议http和https对应的处理socket链接工厂的对象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslcontext))
.build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
// 共享连接池
httpClient = HttpClients.custom().setConnectionManager(connManager).setConnectionManagerShared(true).build();
}
public String sendPost(String url, Map<String, Object> param) throws URISyntaxException, IOException {
// CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(url);
List<NameValuePair> list = new ArrayList<>();
if (param != null) {
NameValuePair paramOne;
for (String key : param.keySet()) {
paramOne = new BasicNameValuePair(key, param.get(key).toString());
list.add(paramOne);
}
}
HttpPost httpPost = new HttpPost(uriBuilder.build());
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
httpPost.addHeader("Upgrade-Insecure-Requests", "1");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new UrlEncodedFormEntity(list, "utf8"));
//3.按回车,发起请求,响应
CloseableHttpResponse response = null;
try {
ProIp proIp = proxyIpService.useIpPort();
//设置代理IP、端口
HttpHost proxy = new HttpHost(proIp.getIp(), proIp.getPort(), "http");
RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
httpPost.setConfig(requestConfig);
response = httpClient.execute(httpPost);
//4.解析响应,打印数据
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "utf8");
return content;
} else {
System.out.println("请求结果 : " + response.getStatusLine().getStatusCode());
}
} finally {
//关闭资源
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public String sendGet(String url) throws URISyntaxException {
URIBuilder uriBuilder = new URIBuilder(url);
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
httpGet.addHeader("Upgrade-Insecure-Requests", "1");
httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
//3.按回车,发起请求,响应
CloseableHttpResponse response = null;
try {
ProIp proIp = proxyIpService.useIpPort();
//设置代理IP、端口
HttpHost proxy = new HttpHost(proIp.getIp(), proIp.getPort(), "http");
RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
httpGet.setConfig(requestConfig);
response = httpClient.execute(httpGet);
//4.解析响应,打印数据
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "utf8");
return content;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static String sendGetStatic(String url) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
RequestConfig requestConfig = RequestConfig.custom().build();
httpGet.setConfig(requestConfig);
// 执行请求
response = httpclient.execute(httpGet);
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() != 200) {
}
} catch (Exception e) {
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
}
}
return resultString;
}
public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
SSLContext sc = SSLContext.getInstance("SSLv3");
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sc.init(null, new TrustManager[]{trustManager}, null);
return sc;
}
}