C#自动更换IE代理IP并判断代理是否可连接网络
来源:互联网 发布:oracle sql脚本编写 编辑:程序博客网 时间:2024/05/22 04:57
using System;
using System.Collections.Generic;
using System.Linq; using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Win32;
using System.Net.NetworkInformation;
using System.Net; using System.IO;
namespace TestBlog {
class Program {
[DllImport(@"wininet", SetLastError = true, CharSet = CharSet.Auto,
EntryPoint = "InternetSetOption",
CallingConvention = CallingConvention.StdCall)]
public static extern bool InternetSetOption(
int hInternet,
int dmOption,
IntPtr lpBuffer,
int dwBufferLength
);
public static void SetProxy(string proxy){
//打开注册表
RegistryKey regKey = Registry.CurrentUser;
string SubKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true); //更改健值,设置代理,
optionKey.SetValue("ProxyEnable", 1);
optionKey.SetValue("ProxyServer", proxy);
//激活代理设置【用于即使IE没有关闭也能更新当前打开的IE中的代理设置。】
InternetSetOption(0, 39, IntPtr.Zero, 0);
InternetSetOption(0, 37, IntPtr.Zero, 0);
}
static void Main(string[] args){
//本事例中未对代理服务器设置密码的情况进行尝试
String ip = null;
for (int i = 1; i <254;i++ ){
ip = "172.0.0." + i + ":808";
//ip请替换为你需要查找的ip段
SetProxy(ip);
if (prcessBaidu()){
Console.WriteLine(ip+"_____________TestOK");break;
}else{
Console.WriteLine(ip + "__false");
}
}
}
//成功返回true,错误返回false
public static Boolean prcessBaidu(){
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com");
myRequest.Method = "POST";
//采用post方式提交访问163主页
// Get response
try//当无法访问163网站时,下面的对象会有错误产生,所以用try..catch处理掉这些异常
{
Stream newStream = myRequest.GetRequestStream();//获取请求流
//Send the data.
newStream.Close();//关闭请求流
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();//获取应答对象
StreamReader reader = new StreamReader(myResponse.GetResponseStream());//获取应答流
string content = reader.ReadToEnd();//将流对象读取到string 中
if (content.IndexOf("http://reg.163.com") > -1)//如果访问网站成功,则网页中包含置顶的关键字符串“http://reg.163.com”表示访问网页成功
{
return true;
}else{
return false;
}
}catch(Exception ex){}
return false;
}
}
}
using System.Collections.Generic;
using System.Linq; using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Win32;
using System.Net.NetworkInformation;
using System.Net; using System.IO;
namespace TestBlog {
class Program {
[DllImport(@"wininet", SetLastError = true, CharSet = CharSet.Auto,
EntryPoint = "InternetSetOption",
CallingConvention = CallingConvention.StdCall)]
public static extern bool InternetSetOption(
int hInternet,
int dmOption,
IntPtr lpBuffer,
int dwBufferLength
);
public static void SetProxy(string proxy){
//打开注册表
RegistryKey regKey = Registry.CurrentUser;
string SubKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true); //更改健值,设置代理,
optionKey.SetValue("ProxyEnable", 1);
optionKey.SetValue("ProxyServer", proxy);
//激活代理设置【用于即使IE没有关闭也能更新当前打开的IE中的代理设置。】
InternetSetOption(0, 39, IntPtr.Zero, 0);
InternetSetOption(0, 37, IntPtr.Zero, 0);
}
static void Main(string[] args){
//本事例中未对代理服务器设置密码的情况进行尝试
String ip = null;
for (int i = 1; i <254;i++ ){
ip = "172.0.0." + i + ":808";
//ip请替换为你需要查找的ip段
SetProxy(ip);
if (prcessBaidu()){
Console.WriteLine(ip+"_____________TestOK");break;
}else{
Console.WriteLine(ip + "__false");
}
}
}
//成功返回true,错误返回false
public static Boolean prcessBaidu(){
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com");
myRequest.Method = "POST";
//采用post方式提交访问163主页
// Get response
try//当无法访问163网站时,下面的对象会有错误产生,所以用try..catch处理掉这些异常
{
Stream newStream = myRequest.GetRequestStream();//获取请求流
//Send the data.
newStream.Close();//关闭请求流
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();//获取应答对象
StreamReader reader = new StreamReader(myResponse.GetResponseStream());//获取应答流
string content = reader.ReadToEnd();//将流对象读取到string 中
if (content.IndexOf("http://reg.163.com") > -1)//如果访问网站成功,则网页中包含置顶的关键字符串“http://reg.163.com”表示访问网页成功
{
return true;
}else{
return false;
}
}catch(Exception ex){}
return false;
}
}
}
0 0
- C#自动更换IE代理IP并判断代理是否可连接网络
- 解决SecureCRT连接总自动断开的问题
- RMI、Hessian、Burlap、Httpinvoker、WebService的比较
- Linux注册apache服务开机启动
- Cannot find or open the PDB file解决方案
- 100句javascript一句话代码(简短常用代码集合,js开发必备的小程序集)
- Device-Mapper+Multipath
- MyEclipse和Eclipse下安装XFire插件CXF插件 WebService
- MFC API——》PreCreateWindow
- Oracle_11gR2_SCAN_IP scan ip配置详解
- 详解使用wsgen和wsimport开发WebService
- EXECUTE IMMEDIATE
- Oracle - expdp、impdp 用法详解
- SVN与Eclipse集成操作手册
- TortoiseSVN及Eclipse的svn插件安装使用
- jQuery plugin开发
- svn eclipse中svn插件的使用
- 用MyEclipse 打包JAR文件
- 【Python 2 到 3 系列】 print 是函数
- 关于mysql5.6 decimal 类型的几个小bug
- kamailio,asteisk,realtime 实现负载均衡(集群)完整配置笔记
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
- eclipse中汉字过小的解决办法
- C#自动更换IE代理IP并判断代理是否可连接网络
- Android 中文字符转UTF-8编码
- 【Python 2 到 3 系列】 此整型非彼整型
- php备份备份mysql
- leetcode-Implement strStr()
- 索引——二元高度
- 行人检测之HOG特征(Histograms od Oriented Gradients)
- oracle恢复被覆盖的存储过程
- springmvc+spring+mybatis+maven项目构建