469,894 Members | 2,158 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

WebClient problem

Dear All,

I am using WebClient in my Visual Studio .Net 2003 project to download
an image from the Internet. The following is my function:

C# Code:

public static bool downloadFile(string URL, ref string fileName)
{
bool result = true;
System.Net.WebClient webClient = new System.Net.WebClient();
try
{
string[] temp;
temp = URL.Split('/');
fileName = Application.StartupPath + "\\tempimage\\" +
temp[temp.Length-1];

webClient.Credentials =
System.Net.CredentialCache.DefaultCredentials;
webClient.DownloadFile(URL, fileName);
}
catch (System.Exception ex)
{
result = false;
}
finally
{
webClient.Dispose();
}
return result;
}
The above function works most of t he time but sometimes I got the
following exception:

System.Net.WebException: The underlying connection was closed: The
server committed an HTTP protocol violation.

at System.Net.HttpWebRequest.CheckFinalStatus()

at System.Net.HttpWebRequest.EndGetResponse(IAsyncRes ult asyncResult)

at System.Net.HttpWebRequest.GetResponse()

at System.Net.WebClient.DownloadFile(String address, String fileName)

at WindowsApplication4.Form11.downloadFile(String URL, String fileName)
in g:\project\visual studio
projects\windowsapplication4\windowsapplication4\f orm11.cs:line 392

Do anyone know what wrong is the function?

Thanks very much for your help.

Regards,
Vincent

Mar 20 '06 #1
1 6021
Thus wrote vi*****@gmail.com,
Dear All,

I am using WebClient in my Visual Studio .Net 2003 project to download
an image from the Internet. The following is my function:

C# Code:

public static bool downloadFile(string URL, ref string fileName)
{
bool result = true;
System.Net.WebClient webClient = new System.Net.WebClient();
try
{
string[] temp;
temp = URL.Split('/');
fileName = Application.StartupPath + "\\tempimage\\" +
temp[temp.Length-1];
webClient.Credentials =
System.Net.CredentialCache.DefaultCredentials;
webClient.DownloadFile(URL, fileName);
}
catch (System.Exception ex)
{
result = false;
}
finally
{
webClient.Dispose();
}
return result;
}
The above function works most of t he time but sometimes I got the
following exception:

System.Net.WebException: The underlying connection was closed: The
server committed an HTTP protocol violation.

at System.Net.HttpWebRequest.CheckFinalStatus()

at System.Net.HttpWebRequest.EndGetResponse(IAsyncRes ult asyncResult)

at System.Net.HttpWebRequest.GetResponse()

at System.Net.WebClient.DownloadFile(String address, String fileName)

at WindowsApplication4.Form11.downloadFile(String URL, String
fileName) in g:\project\visual studio
projects\windowsapplication4\windowsapplication4\f orm11.cs:line 392

Do anyone know what wrong is the function?


Probably nothing. "The server committed an HTTP protocol violation" indicates
that something in the response isn't HTTP 1.1 compliant. See http://blogs.msdn.com/mflasko/archiv...02/488370.aspx
for more details and a possible work around.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Mar 20 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gary Short | last post: by
2 posts views Thread by Max Gattringer | last post: by
1 post views Thread by Nurchi BECHED | last post: by
1 post views Thread by Cc | last post: by
6 posts views Thread by Giovanni | last post: by
4 posts views Thread by Lehel Kovach | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.