日期:2014-05-18  浏览次数:21148 次

关于webBrowser使用代理的问题
C# code

 [DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto,EntryPoint = "InternetSetOption",CallingConvention = CallingConvention.StdCall)]
        public static extern bool InternetSetOption(int hInternet, int dwoption, object lpbuffer, int dwbufferlength);

 private const int INTERNET_OPTION_PROXY = 38;
        private const int INTERNET_OPTION_PROXY_PASSWORD = 44;
        private const int INTERNET_OPTION_PROXY_USERNAME = 43;
        private const int INTERNET_OPEN_TYPE_PROXY = 3;

        [StructLayout(LayoutKind.Sequential)]
        public struct INTERNET_PROXY_INFO
        {
            public int dwAccessType;
            public string lpszProxy;
            public string lpszProxyBypass;
        }

       
        public void SetSockect()
        {
            INTERNET_PROXY_INFO op=new INTERNET_PROXY_INFO() ;
            op.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
            op.lpszProxy = "SOCKS=59.188.125.8:3370";
            op.lpszProxyBypass = "";

            string Username="ma";
            string Password="zsi";

            try
            {

                InternetSetOption(0, INTERNET_OPTION_PROXY, op, Marshal.SizeOf(op));//每次执行到这里就出现-->值不在预期的范围内的错误
                //InternetSetOption(webBrowser1.Handle.ToInt32(), INTERNET_OPTION_PROXY, null,0);
                InternetSetOption(webBrowser1.Handle.ToInt32(), INTERNET_OPTION_PROXY_USERNAME,Username, Username.Length);
                InternetSetOption(webBrowser1.Handle.ToInt32(), INTERNET_OPTION_PROXY_PASSWORD, Password, Password.Length);
            }
            catch (Exception e)
            {
                textBox1.Text = e.ToString();
            }

        }



InternetSetOption(0, INTERNET_OPTION_PROXY, op, Marshal.SizeOf(op));//每次执行到这里就出现-->值不在预期的范围内的错误

------解决方案--------------------
C# code
http://www.codeprof.com/dev-archive/66/9-31-662586.shtm



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