首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebClient.UploadData和内部500错误

WebClient.UploadData和内部500错误
EN

Stack Overflow用户
提问于 2011-04-14 11:32:59
回答 1查看 11.2K关注 0票数 1

所有人。我正在使用c#语言编程,但我在使用WebClient类时遇到问题。我使用WebClient.DownloadData方法下载了一个图像,它工作得很好。但WebClient.UploadData并非如此。详细地说,我有一个包含图片内容的byte[] -名为bytes,以及我想要上传到的图片文件夹的网址-名为filePath。然后,

代码语言:javascript
复制
 WebClient wc = new WebClient(); 
 byte[] responseArray = wc.UploadData(filePath, "POST", bytes);

然后它返回给我下面的错误

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-14 11:40:33

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

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

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5658225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档