Pages

Thursday, April 17, 2014

The remote server returned an error 407 Proxy Authentication Required

One of our clients reported that they could not access a certain web page from another domain as they were getting the Proxy Authentication error even though they had entered the domain name, user name and password correctly for the proxy setting. The code handling proxy is shown below:

Code Snippet
  1. Imports System.Configuration
  2. Imports System.IO
  3. Imports System.Net
  4. Imports System.Text
  5.  
  6. Public Class Form1
  7.  
  8.     Dim InstallerSite As String
  9.     Dim UploadFile As String
  10.  
  11.     Dim MyProxy As New WebProxy
  12.     Dim ProxyPresent As Boolean
  13.  
  14.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  15.  
  16.         InitializeGlobals()
  17.         txtResult.Text = GetFile()
  18.  
  19.     End Sub
  20.  
  21.     Private Sub InitializeGlobals()
  22.  
  23.         InstallerSite = CType(ConfigurationManager.AppSettings("InstallerSite"), String)
  24.         UploadFile = CType(ConfigurationManager.AppSettings("UploadFile"), String)
  25.  
  26.         ' Case for when a Proxy is present.
  27.         ProxyPresent = CType(ConfigurationManager.AppSettings("ProxyPresent"), Boolean)
  28.         Dim ProxyUrl As String = CType(ConfigurationManager.AppSettings("ProxyUrl"), String)
  29.         Dim ProxyDomain As String = CType(ConfigurationManager.AppSettings("ProxyDomain"), String)
  30.         Dim ProxyUserName As String = CType(ConfigurationManager.AppSettings("ProxyUserName"), String)
  31.         Dim ProxyPassword As String = CType(ConfigurationManager.AppSettings("ProxyPassword"), String)
  32.         Dim credentials As New System.Net.NetworkCredential(ProxyUserName, ProxyPassword, ProxyDomain)
  33.         MyProxy.Address = New Uri(ProxyUrl)
  34.         MyProxy.Credentials = credentials
  35.  
  36.     End Sub
  37.  
  38.  
  39.     Private Function GetFile() As String
  40.  
  41.         Dim result As String = String.Empty
  42.  
  43.         Try
  44.  
  45.             Dim request As HttpWebRequest
  46.             Dim response As HttpWebResponse
  47.  
  48.             request = WebRequest.Create(InstallerSite & UploadFile)
  49.  
  50.             If ProxyPresent = True Then
  51.                 request.Proxy = MyProxy
  52.             End If
  53.  
  54.             response = request.GetResponse()
  55.             Dim responseStream As Stream = response.GetResponseStream()
  56.  
  57.             Dim buffer(10) As Byte
  58.             Dim bytesToRead As Integer = CInt(buffer.Length)
  59.             Dim bytesRead As Integer = 0
  60.             While bytesToRead > 0
  61.                 Dim i As Integer = responseStream.Read(buffer, bytesRead, bytesToRead)
  62.                 If i = 0 Then
  63.                     Exit While
  64.                 End If
  65.                 bytesRead += i
  66.                 bytesToRead -= i
  67.             End While
  68.  
  69.             result = Encoding.ASCII.GetChars(buffer)
  70.  
  71.         Catch ex As Exception
  72.  
  73.             result = ex.Message
  74.  
  75.         End Try
  76.  
  77.         Return result
  78.  
  79.     End Function
  80.     
  81. End Class


It turned out they needed to enter the IP address for their proxy domain not just the domain name.
download
alternative link download

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.