By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,781 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,781 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.AppSett ings.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

Dec 7 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
-----
Peter wrote:
Try explicitly setting the url, port, username and password of your proxy,
and assign that to the Proxy property of the WebClient.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Mad Scientist Jr" wrote:
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.AppSett ings.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
Dec 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.