Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
ASP.NET: Access the web via a proxy server
From Wiki
Summary: Access the web via a proxy server
Need help with ASP.NET? Come and ask a question in our ASP.NET Forum
Sometimes we need to make a httpWebRequest to access web pages or sites from behind a proxy server (many companies employ this method so if you are at work this may be the case for you). Fortunately, you can use the WebProxy class to specify your proxy credentials and then assign those credentials to the web request:
- Imports System.data
- Imports System.Net
- Partial Class Default1
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- Dim x As HttpWebResponse
- x = GetResponse("http://aspnetlibrary.com")
- End Sub
- 'open an URL and return response
- Public Function GetResponse(ByRef URL As String) As HttpWebResponse
- 'Create a web request
- Dim m_Req As HttpWebRequest = HttpWebRequest.Create(URL)
- Dim newProxy As WebProxy = New WebProxy("the name of the proxy server", 80)
- Dim cred As New NetworkCredential("your username", "your password")
- newProxy.Credentials = cred
- 'Get a web response
- Try
- Return m_Req.GetResponse()
- Catch ex As System.Net.WebException
- If ex.Status = WebExceptionStatus.ProtocolError Then
- Return ex.Response
- End If
- End Try
- Return Nothing
- End Function
- End Class
This Hack is part of the ASP.NET Hacks collection



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.