Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

ASP.NET: Access the web via a proxy server

From Wiki

Jump to: navigation, search

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:

  1. Imports System.data
  2. Imports System.Net
  3. Partial Class Default1
  4.     Inherits System.Web.UI.Page
  5.  
  6.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  7.         Dim x As HttpWebResponse
  8.         x = GetResponse("http://aspnetlibrary.com")
  9.     End Sub
  10.  
  11.     'open an URL and return response  
  12.     Public Function GetResponse(ByRef URL As String) As HttpWebResponse
  13.  
  14.         'Create a web request  
  15.         Dim m_Req As HttpWebRequest = HttpWebRequest.Create(URL)
  16.         Dim newProxy As WebProxy = New WebProxy("the name of the proxy server", 80)
  17.         Dim cred As New NetworkCredential("your username", "your password")
  18.         newProxy.Credentials = cred
  19.  
  20.         'Get a web response  
  21.         Try
  22.             Return m_Req.GetResponse()
  23.         Catch ex As System.Net.WebException
  24.             If ex.Status = WebExceptionStatus.ProtocolError Then
  25.                 Return ex.Response
  26.             End If
  27.         End Try
  28.         Return Nothing
  29.     End Function
  30. End Class

This Hack is part of the ASP.NET Hacks collection

487 Rating: 2.7/5 (16 votes cast)