DynamicIPAccess.java

DynamicIPAccess.java_第1张图片

package webspider_20230929_paypal;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 动态IP访问
 *
 * @author ZengWenFeng
 * @date 2023.10.08
 * @email [email protected]
 * @mobile 13805029595
 */
public class DynamicIPAccess
{
	public static void main(String[] args)
	{
		try
		{
			// 设置要访问的URL
			URL url = new URL("http://www.baidu.com");

			// 打开连接
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();

			// 设置代理服务器的动态IP地址和端口
			String proxyIP = "192.168.0.1";
			int proxyPort = 8080;

			// 设置代理服务器
			System.setProperty("http.proxyHost", proxyIP);
			System.setProperty("http.proxyPort", String.valueOf(proxyPort));

			// 发送GET请求
			connection.setRequestMethod("GET");

			// 获取响应
			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());

			// 关闭连接
			connection.disconnect();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,java)