百度,google加自己理解后,将所得方法总结一下:
方法1:修改注册表Software//Microsoft//Windows//CurrentVersion//Internet Settings下 ProxyEnable和ProxyServer。这种方法适用于局域网用户,拨号用户无效。
2 {
3
4 //用于刷新注册表
5 [DllImport(@"wininet",
6 SetLastError = true,
7 CharSet = CharSet.Auto,
8 EntryPoint = "InternetSetOption",
9 CallingConvention = CallingConvention.StdCall)]
10
11
12 public static extern bool InternetSetOption
13 (
14 int hInternet,
15 int dmOption,
16 IntPtr lpBuffer,
17 int dwBufferLength
18 );
19
20 private void btnStart_Click(object sender, EventArgs e)
21 {
22 RegistryKey pregkey;
23 pregkey = Registry.CurrentUser.OpenSubKey("Software//Microsoft//Windows//CurrentVersion//Internet Settings", true);
24 if (pregkey == null)
25 {
26 Console.WriteLine("键值不存在");
27 }
28 else
29 {
30 pregkey.SetValue("ProxyEnable", 1);
31 pregkey.SetValue("ProxyServer", "代理地址");
32 //激活代理设置
33 InternetSetOption(0, 39, IntPtr.Zero, 0);
34 InternetSetOption(0, 37, IntPtr.Zero, 0);
35 webBrowser1.Navigate(txtweb.Text, false);
36 // System.Threading.Thread.Sleep(10000);
37 }
38 }
39}
方法2: 修改注册表 Software//Microsoft//Windows//CurrentVersion//Internet Settings//Connections下以你拨号连接名为键的值,该键为二进制。这种方法适用于拨号用户。
{
[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 byte ChangeTobyte(char i)
{
byte key = 0;
switch (i)
{
case '0': key = 48; break;
case '1': key = 49; break;
case '2': key = 50; break;
case '3': key = 51; break;
case '4': key = 52; break;
case '5': key = 53; break;
case '6': key = 54; break;
case '7': key = 55; break;
case '8': key = 56; break;
case '9': key = 57; break;
case '.': key = 46; break;
case ':': key = 58; break;
}
return key;
}
private void btnStart_Click(object sender, EventArgs e)
{
int i = ("代理地址").Length;
byte[] key = new byte[50];
char[] source = ("代理地址").ToCharArray();
key[0] = 60;
key[4] = 3;
key[8] = 3;
key[12] = (byte)i;
for (int ii = 0; ii < source.Length; ii++)
{
key[16 + ii] = ChangeTobyte(source[ii]);
}
string sDirectX = "";
for (int k = 0; k < key.Length; k++)
{
if (key[k] != 0)
{
sDirectX += key[k] + " ";
}
}
//MessageBox.Show(sDirectX);
RegistryKey pregkey;
pregkey = Registry.CurrentUser.OpenSubKey("Software//Microsoft//Windows//CurrentVersion//Internet Settings//Connections", true);
if (pregkey == null)
{
Console.WriteLine("键值不存在");
}
else
{
pregkey.SetValue("拨号名字对应键值", key, RegistryValueKind.Binary);
//激活代理设置
InternetSetOption(0, 39, IntPtr.Zero, 0);
InternetSetOption(0, 37, IntPtr.Zero, 0);
webBrowser1.Navigate(txtweb.Text, false);
webBrowser1.Refresh();
}
}
}
方法3: 使用c#自带的 webproxy类,使用这种方法可以获得目标网站的响应,但我不会把这种响应用IE反馈出来,有高手帮个忙么?
网上的代码说是MSDN的:
HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create(" http://www.microsoft.com"/);
WebProxy myProxy=new WebProxy();
// Obtain the 'Proxy' of the Default browser.
myProxy=(WebProxy)myWebRequest.Proxy; 这行我编译不通过
// Print the Proxy Url to the console.
Console.WriteLine("/nThe actual default Proxy settings are {0}",myProxy.Address);
try
{
Console.WriteLine("/nPlease enter the new Proxy Address that is to be set:");
Console.WriteLine("(Example: http://myproxy.example.com:port/)");
string proxyAddress;
proxyAddress =Console.ReadLine();
if(proxyAddress.Length>0)
{
Console.WriteLine("/nPlease enter the Credentials ");
Console.WriteLine("Username:");
string username;
username =Console.ReadLine();
Console.WriteLine("/nPassword:");
string password;
password =Console.ReadLine();
// Create a new Uri object.
Uri newUri=new Uri(proxyAddress);
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address=newUri;
// Create a NetworkCredential object and associate it with the Proxy property of request object.
myProxy.Credentials=new NetworkCredential(username,password);
myWebRequest.Proxy=myProxy;
}
Console.WriteLine("/nThe Address of the new Proxy settings are {0}",myProxy.Address);
HttpWebResponse myWebResponse=(HttpWebResponse)myWebRequest.GetResponse();
我改了下:
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://www.123cha.com");
WebProxy myProxy = new WebProxy("代理地址", true);
try
{
Console.WriteLine("/nThe Address of the new Proxy settings are {0}", myProxy.Address);
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
webBrowser1.DocumentStream = myWebResponse.GetResponseStream();
}
catch { }