java 代理ip

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;


public class Test1 {

    /**
     * @param args
     * @throws UnsupportedEncodingException
     */
    public static void main(String[] args) {
        //http://www.xicidaili.com/nn/1                  代理ip获取网址
        SocketAddress addr = new InetSocketAddress("113.128.90.52", Integer.parseInt("48888"));
        Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
         StringBuffer html = new StringBuffer();

            String result = null;

//测试 ip是否被代理

            try{
                URL url = new URL("http://www.ip138.com/ip2city.asp");
                URLConnection conn = url.openConnection(proxy);
                conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
                BufferedInputStream in = new BufferedInputStream(conn.getInputStream());

                try{
                    String inputLine;
                    byte[] buf = new byte[4096];
                    int bytesRead = 0;
                    while (bytesRead >= 0) {
                        inputLine = new String(buf, 0, bytesRead, "ISO-8859-1");
                        html.append(inputLine);
                        bytesRead = in.read(buf);
                        inputLine = null;
                    }
                    buf = null;
                }finally{
                    in.close();
                    conn = null;
                    url = null;
                }
                result = new String(html.toString().trim().getBytes("ISO-8859-1"), "gb2312").toLowerCase();
System.err.println(result);
            }catch (Exception e) {
                e.printStackTrace();
               
            }finally{
                html = null;            
            }
           
        getHtml("http://www.ip138.com/ip2city.asp");
    }
        
      private static String getHtml(String address){
            StringBuffer html = new StringBuffer();
            String result = null;
            try{
                URL url = new URL(address);
                URLConnection conn = url.openConnection();
                conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
                BufferedInputStream in = new BufferedInputStream(conn.getInputStream());

                try{
                    String inputLine;
                    byte[] buf = new byte[4096];
                    int bytesRead = 0;
                    while (bytesRead >= 0) {
                        inputLine = new String(buf, 0, bytesRead, "ISO-8859-1");
                        html.append(inputLine);
                        bytesRead = in.read(buf);
                        inputLine = null;
                    }
                    buf = null;
                }finally{
                    in.close();
                    conn = null;
                    url = null;
                }
                result = new String(html.toString().trim().getBytes("ISO-8859-1"), "gb2312").toLowerCase();

            }catch (Exception e) {
                e.printStackTrace();
                return null;
            }finally{
                html = null;            
            }
            return result;
        }
}

Java Selenium中使用私密代理IP主要是为了隐藏真实IP地址并提高网络请求的匿名性。Selenium本身并不直接支持代理设置,但是你可以通过一些第三方库,如`Selenium Grid`、`ProxyMiddleware`等配合浏览器的原生支持来实现。 以下是基本步骤: 1. **添加依赖**:首先,你需要安装一个支持HTTP代理的库,例如`selenium-webdriver-manager`,可以使用Maven或Gradle管理依赖。 ```java // Maven <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>selenium-wdm</artifactId> <version>4.6.0</version> </dependency> // Gradle implementation 'io.github.bonigarcia:selenium-wdm:4.6.0' ``` 2. **配置代理**:创建一个`DesiredCapabilities`实例,并设置代理信息。比如使用`webdriver_manager.proxy`模块。 ```java from selenium.webdriver.common.desired_capabilities import DesiredCapabilities capabilities = DesiredCapabilities.chrome(); proxy = webdriver_manager.proxy(); proxy.http_proxy = "http://your-proxy-ip:port"; proxy.ftp_proxy = proxy.http_proxy; // 如果需要代理FTP proxy.ssl_proxy = proxy.http_proxy; // 如果需要代理HTTPS proxy.no_proxy = "localhost"; // 排除某些域名不使用代理 capabilities.setCapability("proxy", proxy.to_capabilities()); ``` 3. **启动WebDriver**:创建一个新的浏览器实例时,传递配置好的`DesiredCapabilities`。 ```java from selenium import webdriver driver = webdriver.Chrome(desired_capabilities=capabilities) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值