Problem with WebClient and downloading file from HTTPS site

allend2010

Newcomer
Joined
Sep 5, 2003
Messages
4
Hello:

I am attempting to download a file from an https site using the WebClient class. I am able to successfully plug the url with

the uid and pwd in the url from IE and it works fine. However when I use this same string from VB.Net I get an

exception:

"The underlying connection was closed: Unable to connect to the remote server."

I wasn't sure if perhaps I needed additional info for proxy settings or how to set them or if there is just something wrong

with my code.

Private Sub btnGetFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetFile.Click

Dim strURL As String = "https://myUserName:myPassword@www.theSite.com/files/all/test/"
Dim strFileSource As String = "TestTmp.Dat"
Dim strFileDestination As String = strFileSource
Dim strWebResource As String = strURL + strFileSource
Dim wc As New WebClient()

'I get the same error with or without this line.
'wc.Credentials = CredentialCache.DefaultCredentials

Try
wc.DownloadFile(strWebResource, strFileDestination)
Catch ex As System.Exception
lblStatus.Text = ex.Message
End Try

End Sub

Any help anybody could provide with this would be greatly apprecieated.

Thanks,
Al
 
I am not using an HTTPS connection, but I found that I needed to set username and password by setting Credentials, and set up a proxy, by calling System.et.GlobalProxySelection.Select.

Try something like this:

System.Net.WebClient client = new System.Net.WebClient();
System.Net.GlobalProxySelection.Select = new System.Net.WebProxy("myproxy.company.com:8080");
client.Credentials = new System.Net.NetworkCredential("username","password");
client.DownloadFile("http://passwordprotectedsite.com/file.txt","c:\\file.txt");

Hope this helps,

Richard
 
Back
Top