关于HttpRequest访问网页时proxyIP切换问题

2020-06-02 22:56发布

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ip-api.com/json");
WebProxy proxy = new WebProxy("209.205.2.1", 444);
proxy.Credentials = new NetworkCredential("xiexibi123", "0b8487-516e1e-9826a1-b091c8-a449123");
request.Proxy = proxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();//获得回应的数据流
//将数据流转成 String
string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
textBox1.Text = result;

以上代码 当程序运行起来时 每次执行 都会得到相同的IP
当程序重新启动以后 再次执行 就会得到新的IP
求高手解答 如何控制每次访问web的IP

1条回答
我欲成王,谁敢阻挡
2楼-- · 2020-06-02 23:49
#python改编了一套
from urllib import request
proxy_support = request.ProxyHandler({'http':'http://xiexibi123:0b8487-516e1e-9826a1-b091c8-a449d9@209.205.212.38:444'})
auth = request.HTTPBasicAuthHandler()
opener = request.build_opener(proxy_support, auth, request.HTTPHandler)
request.install_opener(opener)
response = request.urlopen("http://ip-api.com/json")
html = response.read()
print(html)

#第一次结果
b'{"status":"success","country":"United States","countryCode":"US","region":"NY","regionName":"New York","city":"Buffalo","zip":"14202","lat":42.8943,"lon":-78.8736,"timezone":"America/New_York","isp":"B2 Net Solutions Inc.","org":"ServerMania Inc","as":"AS55286 B2 Net Solutions Inc.","query":"23.236.197.231"}'
#第二次
b'{"status":"success","country":"United States","countryCode":"US","region":"NY","regionName":"New York","city":"Buffalo","zip":"14202","lat":42.8943,"lon":-78.8736,"timezone":"America/New_York","isp":"B2 Net Solutions Inc.","org":"ServerMania Inc","as":"AS55286 B2 Net Solutions Inc.","query":"45.57.203.180"}'

#补充,你把你公司的代理池账户密码公开这样好吗?
#很明显一台主机做分发,访问这台主机需要账户密码,你最后访问接口只不过返回你请求时候的ip而已
#内网屏蔽也没做,就单单账户密码做权限认证,你还写出来骚年
查看更多
登录 后发表回答