locked
Webbrowser control for setting proxy? RRS feed

  • Question

  • I trying to create my own custom web browser, using the webbrowser control in VS 2005. I trying to deal with setting proxy server, but i just cannot do it.

    How to set proxy for my custom webbrowser? i try using webproxy and webclient class, but still doesnt work for me. Try not to suggest modifying registry directly, and not to suggest going to internet explorer setting to change proxy. All I wanted to know if whether there is a class, such as mshtml, or whatever, that can be used to set proxy, such that the webbrowser control (that come along with VS 2005) can use that proxy for navigation.

    This question is the one of the most pathetic question asked on internet..cos I surf many forums. Many people ask this question, but  this question has been not replied or replied in an unsatisfactory manner...sad.....


    Monday, May 1, 2006 2:43 AM

Answers

  • The WebBrowser control is just an embeddded IE Control, I believe any settings in IE, like the proxy settings, are honered just the same as they are in IE.

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


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

     

    Monday, May 1, 2006 8:01 AM

All replies

  • The WebBrowser control is just an embeddded IE Control, I believe any settings in IE, like the proxy settings, are honered just the same as they are in IE.

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


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

     

    Monday, May 1, 2006 8:01 AM
  •  PJ. van de Sande wrote:
    The WebBrowser control is just an embeddded IE Control, I believe any settings in IE, like the proxy settings, are honered just the same as they are in IE.

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


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




    It wokrs! Thanks! The only difference is that inside the Struct_INTERNET_PROXY_INFO, my proxy and proxyByPass is defined as string, and I just pass the string "http://111.11.11.11:1234" to proxy and "local" to proxyByPass, and it still works! Thanks a lot.

    But according to microsoft, they seem to recommend
    INTERNET_OPTION_PER_CONNECTION_OPTION instead of INTERNET_OPTION_PROXY.....I will go and take a look at the difference..
    Monday, May 1, 2006 12:05 PM
  • Hi There

    Can anybody help me with a VB sample of the same code.

    Thanx

    Monday, May 8, 2006 11:48 AM
  • *bump*

    I would really appreciate it if someone could post the VB code of this solution.

    Thanks

    Thursday, August 24, 2006 12:57 AM
  •  

    ' Module bypassproxy server for loacl   
    Option Explicit

    Private Type INTERNET_PER_CONN_OPTION
    dwOption As Long
    dwValue1 As Long
    dwValue2 As Long
    End Type
    Private Type INTERNET_PER_CONN_OPTION_LIST
    dwSize As Long
    pszConnection As Long
    dwOptionCount As Long
    dwOptionError As Long
    pOptions As Long
    End Type
    Private Const INTERNET_PER_CONN_FLAGS As Long = 1
    Private Const INTERNET_PER_CONN_PROXY_SERVER As Long = 2
    Private Const INTERNET_PER_CONN_PROXY_BYPASS As Long = 3
    Private Const PROXY_TYPE_DIRECT As Long = &H1
    Private Const PROXY_TYPE_PROXY As Long = &H2
    Private Const INTERNET_OPTION_REFRESH As Long = 37
    Private Const INTERNET_OPTION_SETTINGS_CHANGED As Long = 39
    Private Const INTERNET_OPTION_PER_CONNECTION_OPTION As Long = 75
    Private Declare Function InternetSetOption _
    Lib "wininet.dll" Alias "InternetSetOptionA" ( _
    ByVal hInternet As Long, ByVal dwOption As Long, _
    lpBuffer As Any, ByVal dwBufferLength As Long) As Long

    ' Set Proxy

    Public Function SetConnectionOptions(ByVal conn_name As String, ByVal proxy_full_addr As String) As Boolean
    ' conn_name: active connection name. (LAN = "")
    ' proxy_full_addr : eg "193.28.73.241:8080"
    Dim list As INTERNET_PER_CONN_OPTION_LIST
    Dim bReturn As Boolean
    Dim dwBufSize As Long
    Dim options(0 To 2) As INTERNET_PER_CONN_OPTION
    Dim abConnName() As Byte
    Dim abProxyServer() As Byte
    Dim abProxyBypass() As Byte

    dwBufSize = Len(list)

    ' Fill out list struct.
    list.dwSize = Len(list)

    ' NULL == LAN, otherwise connection name.
    abConnName() = StrConv(conn_name & vbNullChar, vbFromUnicode)
    list.pszConnection = VarPtr(abConnName(0))

    ' Set three options.
    list.dwOptionCount = 3

    ' Set flags.
    options(0).dwOption = INTERNET_PER_CONN_FLAGS
    options(0).dwValue1 = PROXY_TYPE_DIRECT Or PROXY_TYPE_PROXY

    ' Set proxy name.
    options(1).dwOption = INTERNET_PER_CONN_PROXY_SERVER
    abProxyServer() = StrConv(proxy_full_addr & vbNullChar, vbFromUnicode)
    options(1).dwValue1 = VarPtr(abProxyServer(0)) '//"http://proxy:80"

    ' Set proxy override.
    options(2).dwOption = INTERNET_PER_CONN_PROXY_BYPASS
    abProxyBypass() = StrConv("local" & vbNullChar, vbFromUnicode)
    options(2).dwValue1 = VarPtr(abProxyBypass(0))

    list.pOptions = VarPtr(options(0))
    ' Make sure the memory was allocated.
    If (0& = list.pOptions) Then
    ' Return FALSE if the memory wasn't allocated.
    Debug.Print "Failed to allocate memory in SetConnectionOptions()"
    SetConnectionOptions = 0
    End If

    ' Set the options on the connection.
    bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)

    ' Free the allocated memory.
    Erase options
    Erase abConnName
    Erase abProxyServer
    Erase abProxyBypass
    dwBufSize = 0
    list.dwOptionCount = 0
    list.dwSize = 0
    list.pOptions = 0
    list.pszConnection = 0
    Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0)
    Call InternetSetOption(0, INTERNET_OPTION_REFRESH, ByVal 0&, 0)
    SetConnectionOptions = bReturn
    End Function

    ' Disable Proxy

    Public Function DisableConnectionProxy(ByVal conn_name As String) As Boolean
    ' conn_name: active connection name. (LAN = "")
    Dim list As INTERNET_PER_CONN_OPTION_LIST
    Dim bReturn As Boolean
    Dim dwBufSize As Long
    Dim options(0) As INTERNET_PER_CONN_OPTION
    Dim abConnName() As Byte

    dwBufSize = Len(list)

    ' Fill out list struct.
    list.dwSize = Len(list)

    ' NULL == LAN, otherwise connectoid name.
    abConnName() = StrConv(conn_name & vbNullChar, vbFromUnicode)
    list.pszConnection = VarPtr(abConnName(0))

    ' Set three options.
    list.dwOptionCount = 1

    ' Set flags.
    options(0).dwOption = INTERNET_PER_CONN_FLAGS
    options(0).dwValue1 = PROXY_TYPE_DIRECT

    list.pOptions = VarPtr(options(0))
    ' Make sure the memory was allocated.
    If (0 = list.pOptions) Then
    ' Return FALSE if the memory wasn't allocated.
    Debug.Print "Failed to allocate memory in DisableConnectionProxy()"
    DisableConnectionProxy = 0
    End If

    ' Set the options on the connection.
    bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)

    ' Free the allocated memory.
    Erase options
    Erase abConnName
    dwBufSize = 0
    list.dwOptionCount = 0
    list.dwSize = 0
    list.pOptions = 0
    list.pszConnection = 0
    Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0)
    Call InternetSetOption(0, INTERNET_OPTION_REFRESH, ByVal 0&, 0)
    DisableConnectionProxy = bReturn
    End Function

     

    '-------------------------------------------------------------------------------------- in the form put the following
    'Form
    Option Explicit

    Private Sub cmdSetProxy_Click()
    Dim conn_name As String, proxy_full_addr As String
    conn_name = ""
    proxy_full_addr = "167.35.217.71:8080"
    Call SetConnectionOptions(conn_name, proxy_full_addr)
    End Sub

    Private Sub cmdDisableProxy_Click()
    Dim conn_name As String
    conn_name = ""
    Call DisableConnectionProxy(conn_name)
    End Sub

    Monday, May 12, 2008 10:44 AM
  •  Would it be possible for someone to post the Visual C++ version of this solution? I attempted to translate the C# version, but haven't been sucessful ...

    thanks so much,

    LJ
    Friday, March 27, 2009 9:11 PM
  • Hello hanct,

     

    Considering that many developers in this forum ask how to manipulate WebBrowser component (enabling flash, suppressing error messages, proxy support), rotate or flip images, my team has created a code sample for this frequently asked programming task in Microsoft All-In-One Code Framework. You can download the code samples at:

     

    VBWebBrowserWithProxy

     

    http://bit.ly/VBWebBrowserWithProxy

     

    CSWebBrowserWithProxy

     

    http://bit.ly/CSWebBrowserWithProxy

     

    With these code samples, we hope to reduce developers’ efforts in solving the frequently asked

    programming tasks. If you have any feedback or suggestions for the code samples, please email us: onecode@microsoft.com.

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

    The Microsoft All-In-One Code Framework (http://1code.codeplex.com) is a free, centralized code sample library driven by developers' needs. Our goal is to provide typical code samples for all Microsoft development technologies, and reduce developers' efforts in solving typical programming tasks.

    Our team listens to developers’ pains in MSDN forums, social media and various developer communities. We write code samples based on developers’ frequently asked programming tasks, and allow developers to download them with a short code sample publishing cycle. Additionally, our team offers a free code sample request service. This service is a proactive way for our developer community to obtain code samples for certain programming tasks directly from Microsoft.

    Thanks

    Microsoft All-In-One Code Framework

    Thursday, March 24, 2011 10:50 AM
  • This is not a good code sample or sample project!  As a matter of fact, this is pretty horrible!  This sets the proxy for the entire box.  You need to set it only for that particular process.

    This is the way to do it for just that process: http://blogs.msdn.com/b/jpsanders/archive/2011/04/26/how-to-set-the-proxy-for-the-webbrowser-control-in-net.aspx

     


    Jeff Sanders (MSFT)
    • Proposed as answer by ildjarn Thursday, November 20, 2014 2:37 AM
    Tuesday, April 26, 2011 5:15 PM
  • hello, your code sets for that process, what if i need to set proxy for each webbrowser in my process or app?

    Do you know we will see our dead loved one again? John 5:25

    Wednesday, June 6, 2012 2:29 AM
  • You would need to use that code in each distinct process.

    If you want to set it system wide you could do that by setting the proxy for the entire box


    Jeff Sanders (MSFT)

    Wednesday, June 6, 2012 12:20 PM