WebClient.UploadData and Internal 500 error

本文关键字:error Internal and UploadData WebClient | 更新日期: 2023-09-27 17:50:00

每个人。我用c语言编程,WebClient类有问题。我使用WebClient.DownloadData方法下载了一个图像,效果很好。但是,WebClient.UploadData没有。详细地说,我有一个字节[],它包含一个名为字节的图像内容,以及一个我想上传到名为filePath的图像文件夹的URL。然后,

 WebClient wc = new WebClient(); 
 byte[] responseArray = wc.UploadData(filePath, "POST", bytes);

它在错误后返回给我

System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
   at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
   at System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
   at System.Net.WebClient.UploadData(String address, String method, Byte[] data)

我还研究了这个问题的一些解决方案,但没有一个对我有效(请帮忙!:-s

WebClient.UploadData and Internal 500 error

试试这个页面。它有一个张贴的例子。

它使用的是webrequest,而不是webclient,但它来自微软,应该是一个很好的例子。

WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
request.Method = "POST";
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

//Here is the Business end of the code...
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
//and here is the response.
WebResponse response = request.GetResponse ();
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();
Console.WriteLine (responseFromServer);
reader.Close ();
dataStream.Close ();
response.Close ();

我精简了代码,并评论了你想看的部分。

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx