accessing URLs outside firewall with WebClient? error: The underlying connection was closed

  • Thread starter Mad Scientist Jr
  • Start date
M

Mad Scientist Jr

For some reason I can't get a WebClient to access an outside URL from
behind our firewall. The code works when it runs outside the firewall.

I turned on windows authentication in the web.config
<authentication mode="Windows" />
and set up IIS to not allow anonymous access.

My browser is set up to auto detect the proxy server, so IE/Firefox can
access outside pages, so I added code to auto-detect the proxy server.

But still the function returns the error:
"The underlying connection was closed: Unable to connect to the remote
server."

Can someone explain how to get it to work from behind our firewall? My
code is below.

Thanks in advance...

'goto a URL and retrieve text from page
Private Function GetHtml(byval sURL as String) As String
Dim WebClient As New System.Net.WebClient
Dim webResponse As IO.Stream
Dim webResult As String
Dim myReader As IO.StreamReader
Try
'problem with credentials? attemt #1
'TODO: redo this to automatically detect if "anonymous access"
is enabled in IIS
'If
System.Configuration.ConfigurationSettings.AppSettings.Get("AnonymousAccessEnabled")
<> "1" Then
' WebClient.Credentials =
System.Web.HttpContext.Current.User.Identity
' ' ' System.Web.HttpContext.Current.User.Identity
' ' 'WebClient.Credentials =
' ' 'System.Net.ICredentials
'End If

'access web thru proxy:
'GlobalProxySelection.Select = New WebProxy("proxy", 8080)
GlobalProxySelection.Select = WebProxy.GetDefaultProxy

webResponse = WebClient.OpenRead(remoteFile)
'objWebClient.DownloadData (strURL)

myReader = New IO.StreamReader(webResponse)
webResult = myReader.ReadToEnd
myReader.Close()
webResponse.Close()

Return webResult
Catch ex As Exception
HandleWebError(ex)
End Try
End Function ' GetHtml
 
M

Mad Scientist Jr

Try explicitly setting the url, port, username and password of your proxy,
and assign that to the Proxy property of the WebClient.

Thanks for your reply... I tried this and am still getting an error:
"The underlying connection was closed: Unable to connect to the remote
server."

Here is the code...

'goto a URL and retrieve text from page
Private Function GetHtml(byval sURL as String) As String
Dim WebClient As New System.Net.WebClient
Dim webResponse As IO.Stream
Dim webResult As String
Dim myReader As IO.StreamReader
Try
GlobalProxySelection.Select = New WebProxy("111.11.111.111", 8080)
'WebClient.Credentials = New NetworkCredential("myuser", "mypass")
WebClient.Credentials = New NetworkCredential("mydomain/myuser",
"mypass")
webResponse = WebClient.OpenRead(remoteFile)
myReader = New IO.StreamReader(webResponse)
webResult = myReader.ReadToEnd
myReader.Close()
webResponse.Close()
Return webResult
Catch ex As Exception
HandleWebError(ex)
End Try
End Function ' GetHtml


-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top