C#webBrowser使用代理服务器的方法winform

其实在C#中使用webBrowser大家应该都会了,论坛也有很多相前的例子大家可以查询一下就知道了
但是像直接使用浏览器一样设置代理 的方法可能很多人还不知道吧。
这个其实是调用一个Dll文件进行设置的,
下面大家跟我一起来看看吧
首先还是要先建一个结构就是代理信息的结构体
如下

[C#]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
/// <summary>
    /// 代理结构体
    /// </summary>
    public struct Struct_INTERNET_PROXY_INFO
    {
        public int dwAccessType;
        public IntPtr proxy; //IP以及端口号
        public IntPtr proxyBypass;
    };


下面是如何 设置代理 的具体实现

[C#]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// <summary>
         /// 设置代理的Api
         /// </summary>
         /// <returns></returns>
         [DllImport( "wininet.dll" , SetLastError = true )]
         private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
 
         /// <summary>
         /// 代理IP以及端口号
         /// </summary>
         /// <param name="strProxy"></param>
         private void RefreshIESettings( string strProxy)
         {
             const int INTERNET_OPTION_PROXY = 38;
             const int INTERNET_OPEN_TYPE_PROXY = 3;
 
             Struct_INTERNET_PROXY_INFO struct_IPI;
 
             // Filling in structure
             struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
             struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
             struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi( "local" );
 
             // Allocating memory
             IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
 
             // Converting structure to IntPtr
             Marshal.StructureToPtr(struct_IPI, intptrStruct, true );
 
             bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));
         }


使用的时候也非常的简单

[C#]  纯文本查看 复制代码
?
01
02
RefreshIESettings( "41.129.53.227:80" );
webBrowser1.Navigate( "http://www.sufeinet.com" );


这样就可以了。
好了大家自己试试吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值