469,295 Members | 2,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,295 developers. It's quick & easy.

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

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
1 6111
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.

Similar topics

4 posts views Thread by Justin Malloy | last post: by
2 posts views Thread by John | last post: by
2 posts views Thread by Gary Short | last post: by
1 post views Thread by yyy | last post: by
5 posts views Thread by Helen | last post: by
reply views Thread by Kumar | last post: by
reply views Thread by Nate Hekman | last post: by
1 post views Thread by Deepson Thomas | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.