文章目录
- 前言
- (java)(python)以代理IP的方式进行请求数据
- 1. python
- 2. java
前言
如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!
(java)(python)以代理IP的方式进行请求数据
1. python
这里先写下python 的实现方式
1、实现
2、将获取的数据存入文件中
下面是一个get的实现方式
import requests
"""
GET /path HTTP/1.1 请求首行
1、请求头
2、空行
3、请求体
"""
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/94.0.4606.54 Safari/537.36 '
} # 添加请求头,伪装为浏览器
proxy = {'http': 'xxx.xx.xx.xxx:yyyy', 'https':'xxx.xx.xx.xxx:yyyy'}
ur2 = 'https://www.hahaha.org.cn/v1/yzy.do?pageNum=1&pageSize=20'
res = requests.get(url=ur2, proxies=proxy, headers=header)
print(f'状态码:{res.status_code}')
print(f'请求头:{res.headers}')
content = res.content # 获取响应的二进制内容
decoded_content = content.decode('UTF-8')
print(f'body:{decoded_content}')
# 文件写操作
with open("D:\\pathonFile\\test.html", "w", encoding="utf-8") as f:
f.write(decoded_content)
2. java
用java 的方式来实现
这里面要注意的是,对于https的请求会有个ssl验证,这里要注意把这地方绕过去
下面是一个get的实现方式。
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import javax.annotation.Resource;
@Resource
private ObjectMapper objectMapper;
YzyVo yzyVo = objectMapper.readValue(getFileStream(reRule.getUrlValue()), new TypeReference<ZdContentListVo>() {
});
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.security.cert.X509Certificate;
public String getFileStream(String fileUrl,String callProxyIp, int callProxyPort) throws IOException {
log.info("url get:{}",fileUrl);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(callProxyIp, callProxyPort));
InputStream inputStream;
URL url = new URL(fileUrl);
//绕过ssl验证
trustAllCertificates();
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(proxy);
// 设置连接超时时间(单位:毫秒)
httpConn.setConnectTimeout(10000); // 10秒
// 设置读取超时时间(单位:毫秒)
httpConn.setReadTimeout(10000); // 10秒
inputStream = httpConn.getInputStream();
return inputStreamToString(inputStream);
}
private void trustAllCertificates() {
try {
// 创建一个信任管理器对象
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// 获取一个SSLContext对象
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
log.error("创建ssl失败",e);
}
}
String inputStreamToString(InputStream inputStream) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
}