C# webbrowser 代理
百度,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下以你撥號連接名為鍵的值,該鍵為二進制。這種方法適用于撥號用戶。
???? public?partial?class?FrmMain?:?Form
????{
[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 { }
總結
以上是生活随笔為你收集整理的C# webbrowser 代理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webservice 启用代理服务器
- 下一篇: C#图片处理基本应用(裁剪,缩放,清晰度