Problem about using WebClient DownloadFile

kpao

Active member
Joined
Apr 3, 2006
Messages
29
Programming Experience
Beginner
The following is my fragment code:
VB.NET:
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim myWebClient As New WebClient
        myWebClient.DownloadFile(New Uri("http://www.google.com"), Request.MapPath("Files\") & "google.htm")

End Sub

The above code works in my pc. But when I put this code to my server(Window 2000 Terminal). It doesn't work. And the error message is the following:

"The remote name could not be resolved: 'www.google.com'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote name could not be resolved: 'www.google.com'"

And I have tried surfing www.google.com in my server. It can find www.google.com. But why the above code did not work? I can't figure out how to solve it. Can anyone help me?
 
Last edited:
...DownloadFile("sourceFilename.txt", "targetFilename.txt")
 
Sorry, may be my threads misunderstand you. I have edited my post.

VB.NET:
        Dim myWebClient As New WebClient
        myWebClient.DownloadFile(New Uri("http://www.google.com"), Request.MapPath("Files\") & "google.htm")

VB.NET:
        Dim myWebClient As New WebClient
        myWebClient.DownloadFile("http://www.google.com", Request.MapPath("Files\") & "google.htm")

Actually, the two above codes are the same. What my questions are the above code didn't work in my Window 2000 server terminal, but it works in my pc and window 2003 server. The error message show me that : "The remote name could not be resolved. ' www.google.com' ". so, I suspect this is a DNS issue. But I tried to surf the web www.google.com in my window 2000 server. It can find the website.
 
Ok, you had 'httt:localhost' as source in first post and was talking about Google, so that didn't make much sense. Name resolve is DNS problem, but I don't know why since your one application have internet and the other don't.
 
I found the solution finally. The reason why I got this error message is leaving out to indicate the proxy server to the WebClient. The internet of my office is managed by a proxy server. So, I changed the code to the following:

VB.NET:
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim proxy As WebProxy = New WebProxy("129.0.2.1", 80)
       
        Dim myWebClient As New WebClient
        myWebClient.Proxy = proxy
        myWebClient.DownloadFile(New Uri("http://www.google.com"), Request.MapPath("Files\") & "google.htm")

End Sub

The above code worked in both xp, 2000 server and windows 2003.
 
It doesn't make sense this either, since IE settings is used for default proxy. This is what I found in related documentation:
By default, the Internet Explorer proxy settings are used to detect the proxy.
The proxy is set by the system using configuration files and the Internet Explorer Local Area Network settings.
For example, if you use this code:
VB.NET:
myWebClient.Proxy = Net.WebProxy.GetDefaultProxy
...you now get a warning:
This method has been deprecated. Please use the proxy selected for you by default.
The Proxy property getter has this code internally:
VB.NET:
If Not Me.m_ProxySet Then
    Return WebRequest.InternalDefaultWebProxy
End If
So this could indicate that there is an configuration problem with that machine, because you normally don't have to set that property if IE is already configured.

By the way, you should update your forum profile to reflect that you use .Net 2.0.
 
Back
Top