2 Replies - 43097 Views - Last Post: 29 May 2008 - 08:29 AM Rate Topic: -----

#1 Overhed   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-May 08

Using WebBrowser Object With Proxy Server

Posted 28 May 2008 - 11:58 AM

Hello, I've got this WebBrowser object I'm using to grab some data from an HTML page in a VB.Net application, and I was wondering if it was possible to configure Proxy settings for it inside my code.

I figured since the WebBrowser object is a clone of Internet Explorer, and in IE you can setup a proxy server, I should somehow be able to do the same for my program.

So far, I've looked into the Wininet.dll API, but haven't gotten anywhere. All the information I seem to find online seems very obfuscated and I'm not sure what is being done.

Any help on this would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

#7 RodgerB   User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: Using WebBrowser Object With Proxy Server

Posted 29 May 2008 - 12:52 AM

Use the following code to set the proxy settings for IE (which in turn will set the proxy settings for the web browser control). Copy and paste it into a class.

Code was derived from this link (click).

' The structure we use for the information
' to be interpreted correctly by API.
Public Structure Struct_INTERNET_PROXY_INFO
    Public dwAccessType As Integer
    Public proxy As IntPtr
    Public proxyBypass As IntPtr
End Structure

' The Windows API function that allows us to manipulate
' IE settings programmatically.
Private Declare Auto Function InternetSetOption Lib "wininet.dll" _
(ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, _
 ByVal lpdwBufferLength As Integer) As Boolean

' The function we will be using to set the proxy settings.
Private Sub RefreshIESettings(ByVal strProxy As String)
    Const INTERNET_OPTION_PROXY As Integer = 38
    Const INTERNET_OPEN_TYPE_PROXY As Integer = 3
    Dim struct_IPI As Struct_INTERNET_PROXY_INFO

    ' Filling in structure
    struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY
    struct_IPI.proxy = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(strProxy)
    struct_IPI.proxyBypass = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi("local")

    ' Allocating memory
    Dim intptrStruct As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))

    ' Converting structure to IntPtr
    System.Runtime.InteropServices.Marshal.StructureToPtr(struct_IPI, intptrStruct, True)
    Dim iReturn As Boolean = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))
End Sub

The function can be called by simply using something like this, place it in a subroutine (e.g. Form Load).


Hope that helps! :)
Was This Post Helpful? 1

#8 Overhed   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-May 08

Re: Using WebBrowser Object With Proxy Server

Posted 29 May 2008 - 08:29 AM

Great, thanks a lot. I'm sure this will help.

Up to this point what I was doing was manually changing the IE settings myself, obviously doing it programmatically is 100X better.

I'll look into how to turn off the proxy server programmatically also.

As it turns out the IE settings go back to normal after the app exits.

This post has been edited by Overhed: 29 May 2008 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1