C# WebBrowser 設定代理完全解決方案

來源:互聯網
上載者:User

微軟webbrowser控制項也就是IE外掛程式,他的所有功能就像IE類似,當然設定也是一樣的,下面介紹下webbrowser如何設定代理,可不要用這個對抗廣告聯盟哦

You can change the proxy with InternetSetOption method from the wininet.dll, here is a example to set the proxy:

using System.Runtime.InteropServices;

Public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
};
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

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));
}

private void SomeFunc()
{
RefreshIESettings("192.168.1.200:1010");

System.Object nullObject = 0;
string strTemp = String.Empty;
System.Object nullObjStr = strTemp;
axWebBrowser1.Navigate("http://willstay.tripod.com", ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
}

-------------------------------------------------------------------------------------------------------------------------------------

昨 天做的投票機遇到個新問題,昨天開始那個投票開始現在ip地址,每個地址只能投5票/天。如果每次更改ie的串連為Proxy 伺服器,那也麻煩死了,如 果改用webclient,那昨天的2個多小時就白費了,上網一通狂收還真找到了辦法,這下好辦了,建了一個proxy.txt文檔,裡面放上從網上收到 的Proxy 伺服器,然後程式讀到一個listbox裡面,每次需要更換ip的時候只要單擊一次,就可以還一個地址重新投票了。
附上proxy.cs

using System.Runtime.InteropServices;//需要添加這個引用
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
};
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
public 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));
}

使用的時候,調用RefreshIESettings
py.proxy py1 = new proxy();
py1.RefreshIESettings(”221.4.155.51:3128″);
System.Object nullObject = 0;
string strTemp = String.Empty;
System.Object nullObjStr = strTemp;
webBrowser1.Navigate(”http://www.hfxsy.cn”, null, null, null);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.