通过代理访问一些网址

import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Properties;

public class HttpProxy {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties prop = System.getProperties();
prop.put("proxySet","true");
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", "130.194.11.121");
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", "1080");
prop.list(System.out);
// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
//prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
// 设置安全访问使用的代理服务器地址与端口
// 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问
/*
prop.setProperty("https.proxyHost", "192.168.0.254");
prop.setProperty("https.proxyPort", "443");
*/
// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
/*
prop.setProperty("ftp.proxyHost", "192.168.0.254");
prop.setProperty("ftp.proxyPort", "2121");
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
*/
// socks代理服务器的地址与端口
/*
prop.setProperty("socksProxyHost", "192.168.0.254");
prop.setProperty("socksProxyPort", "8000");
*/
// 设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator("", ""));
URL url;
HttpURLConnection http;
InputStream urlstream;
try {
url = new URL("http://www.blogger.com/");
http = (HttpURLConnection)url.openConnection();
http.connect();
urlstream = http.getInputStream();
byte buffer[] = new byte[1024];
int i;
while ((i =urlstream.read(buffer)) != -1) {
System.out.write(buffer, 0, i);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
vector<CString> vecList; vecList.push_back(_T("110.77.0.3:80 ")); vecList.push_back(_T("106.60.94.127:80 ")); vecList.push_back(_T("49.94.32.45:80 ")); vecList.push_back(_T("119.188.94.145:80 ")); vecList.push_back(_T("37.49.137.243:80 ")); vecList.push_back(_T("111.13.132.92:80 ")); vecList.push_back(_T("49.94.148.119:80 ")); vecList.push_back(_T("49.94.164.209:80 ")); vecList.push_back(_T("49.94.2.84:80 ")); vecList.push_back(_T("49.90.1.251:80 ")); vecList.push_back(_T("117.37.243.194:80 ")); vecList.push_back(_T("110.4.12.175:80 ")); vecList.push_back(_T("36.98.30.234:80 ")); vecList.push_back(_T("110.244.111.56:80 ")); vecList.push_back(_T("49.94.131.174:80 ")); vecList.push_back(_T("111.1.61.23:80 ")); vecList.push_back(_T("54.252.97.45:80 ")); vecList.push_back(_T("111.13.12.202:80 ")); vecList.push_back(_T("106.33.2.219:80 ")); vecList.push_back(_T("110.176.86.5:80 ")); vecList.push_back(_T("171.9.147.106:80 ")); vecList.push_back(_T("200.33.27.33:80 ")); vecList.push_back(_T("49.94.141.104:80 ")); vecList.push_back(_T("110.176.178.131:80 ")); vecList.push_back(_T("49.93.24.73:80 ")); vecList.push_back(_T("111.12.128.166:80 ")); vecList.push_back(_T("223.15.233.54:80 ")); vecList.push_back(_T("123.173.206.192:80 ")); vecList.push_back(_T("110.77.197.132:80 ")); vecList.push_back(_T("49.94.0.196:80 ")); WebClient wc; WebClient wc2; CString strData=_T(""); for(int i=0;i<vecList.size();i++) { try { CString str1; CString strIP=vecList[i]; strIP.Trim(); wc.SetProxy(strIP); CString strTemp=_T(""); wc.Get(_T("http://www.2345.com/?24384-1217"),strTemp); str1.Format(_T("%s ->%s\r\n"),strIP,strTemp); strData+=str1; wc2.SetProxy(strIP); wc.Get(_T("http://www.haosooo.cn/?f=me"),strTemp); str1.Format(_T("%s ->%s\r\n"),strIP,strTemp); CSuperLog::WriteLog(str1); Sleep((1000)); } catch (...) { } }
在多网卡环境下指定访问特定网站(如百度)时使用的网络接口,可以通过配置路由表和策略路由来实现。这种操作通常适用于需要精细化控制网络流量的场景,例如同时连接有线和无线网络、需要区分不同程序或服务使用的网络接口等。 ### 配置静态路由规则 对于简单的网络环境,可以使用 `route` 或 `ip route` 命令添加静态路由规则。假设你已经知道目标网站的 IP 地址范围(如百度的 IP 段),可以通过以下方式为特定 IP 指定出口网卡: ```bash # 添加一条路由规则,指定访问 180.97.33.0/24 网段的数据包通过 eth0 接口发送 sudo ip route add 180.97.33.0/24 dev eth0 ``` 该命令会将所有发往百度服务器(假设其 IP 范围为 180.97.33.0/24)的流量强制通过 `eth0` 接口传输[^1]。 ### 使用策略路由实现更灵活的控制 如果需要根据源地址、协议类型或应用程序进行更复杂的路由决策,可以使用 Linux 的 **策略路由**(Policy Routing)功能。具体步骤如下: 1. 创建一个新的路由表(例如名为 `table_baidu`),可以在 `/etc/iproute2/rt_tables` 文件中添加一行: ``` 100 table_baidu ``` 2. 为这个新路由表添加默认路由或特定路由规则: ```bash sudo ip route add default via <网关IP> dev <网卡名> table table_baidu ``` 3. 设置规则,使得访问百度的流量走这个新路由表: ```bash sudo ip rule add to 180.97.33.0/24 table table_baidu ``` 这样,当系统检测到目的地是百度 IP 段的数据包时,就会使用 `table_baidu` 中定义的路由规则进行转发[^1]。 ### 应用层绑定网卡(针对特定程序) 某些程序(如浏览器或自定义客户端)可能不支持直接指定网络接口,但你可以借助 Linux 的 `iptables` 和 `cgroup` 实现基于进程的网络隔离。例如,结合 `nsenter` 或 `unshare` 创建独立的网络命名空间,让特定程序运行在指定网络环境中。 此外,一些高级工具如 `proxychains` 可以配合本地代理(如 `squid` 或 `privoxy`)实现程序级的代理设置,从而间接控制其使用的网络接口[^2]。 ### 注意事项 - 百度的 IP 地址可能会动态变化,因此建议使用 DNS 解析后获取其当前 IP 并定期更新路由规则。 - 多网卡环境下应确保路由表不会发生冲突,否则可能导致部分服务不可达。 - 在 Windows 系统中也可以通过 `route add` 命令或注册表编辑器调整路由优先级,但不如 Linux 灵活。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值