Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,263 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,252 people online right now. Registration is fast and FREE... Join Now!




Using WebBrowser Object With Proxy Server

 
Reply to this topicStart new topic

Using WebBrowser Object With Proxy Server

Overhed
28 May, 2008 - 10:58 AM
Post #1

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 10

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.

User is offlineProfile CardPM
+Quote Post


RodgerB
RE: Using WebBrowser Object With Proxy Server
28 May, 2008 - 11:52 PM
Post #2

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,213



Thanked: 28 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
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).

vb

' 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).

vb
RefreshIESettings("127.0.0.1:8080")


Hope that helps! smile.gif
User is offlineProfile CardPM
+Quote Post

Overhed
RE: Using WebBrowser Object With Proxy Server
29 May, 2008 - 07:29 AM
Post #3

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 10

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.

EDIT:
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 - 07:51 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 12:00PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month