Showing posts with label 407. Show all posts
Showing posts with label 407. Show all posts
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:
It turned out they needed to enter the IP address for their proxy domain not just the domain name.
Read more »
Code Snippet
- Imports System.Configuration
- Imports System.IO
- Imports System.Net
- Imports System.Text
- Public Class Form1
- Dim InstallerSite As String
- Dim UploadFile As String
- Dim MyProxy As New WebProxy
- Dim ProxyPresent As Boolean
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- InitializeGlobals()
- txtResult.Text = GetFile()
- End Sub
- Private Sub InitializeGlobals()
- InstallerSite = CType(ConfigurationManager.AppSettings("InstallerSite"), String)
- UploadFile = CType(ConfigurationManager.AppSettings("UploadFile"), String)
- ' Case for when a Proxy is present.
- ProxyPresent = CType(ConfigurationManager.AppSettings("ProxyPresent"), Boolean)
- Dim ProxyUrl As String = CType(ConfigurationManager.AppSettings("ProxyUrl"), String)
- Dim ProxyDomain As String = CType(ConfigurationManager.AppSettings("ProxyDomain"), String)
- Dim ProxyUserName As String = CType(ConfigurationManager.AppSettings("ProxyUserName"), String)
- Dim ProxyPassword As String = CType(ConfigurationManager.AppSettings("ProxyPassword"), String)
- Dim credentials As New System.Net.NetworkCredential(ProxyUserName, ProxyPassword, ProxyDomain)
- MyProxy.Address = New Uri(ProxyUrl)
- MyProxy.Credentials = credentials
- End Sub
- Private Function GetFile() As String
- Dim result As String = String.Empty
- Try
- Dim request As HttpWebRequest
- Dim response As HttpWebResponse
- request = WebRequest.Create(InstallerSite & UploadFile)
- If ProxyPresent = True Then
- request.Proxy = MyProxy
- End If
- response = request.GetResponse()
- Dim responseStream As Stream = response.GetResponseStream()
- Dim buffer(10) As Byte
- Dim bytesToRead As Integer = CInt(buffer.Length)
- Dim bytesRead As Integer = 0
- While bytesToRead > 0
- Dim i As Integer = responseStream.Read(buffer, bytesRead, bytesToRead)
- If i = 0 Then
- Exit While
- End If
- bytesRead += i
- bytesToRead -= i
- End While
- result = Encoding.ASCII.GetChars(buffer)
- Catch ex As Exception
- result = ex.Message
- End Try
- Return result
- End Function
- End Class
It turned out they needed to enter the IP address for their proxy domain not just the domain name.
Subscribe to:
Posts (Atom)