代理ip刷网页停留10秒 java
以下是使用Java代码实现代理IP刷网页并停留10秒的示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
public class ProxyIPDemo {
public static void main(String[] args) {
String proxyHost = "代理IP地址";
int proxyPort = 代理端口号;
String targetUrl = "目标网页URL";
// 创建代理对象
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
try {
// 创建URL对象
URL url = new URL(targetUrl);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
// 设置请求方法
connection.setRequestMethod("GET");
// 设置超时时间
connection.setConnectTimeout(5000);
// 发起请求
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
// 读取响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 输出响应内容
System.out.println(response.toString());
} else {
System.out.println("请求失败,响应码:" + responseCode);
}
// 关闭连接
connection.disconnect();
// 停留10秒
Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
请注意将代码中的"代理IP地址"、"代理端口号"和"目标网页URL"替换为实际的值。此外,代码中使用的是HTTP代理,如果目标网页使用的是HTTPS协议,需要相应修改代码
原文地址: https://www.cveoy.top/t/topic/hJXY 著作权归作者所有。请勿转载和采集!