簡體   English   中英

WebClient.UploadData和Internal 500錯誤

[英]WebClient.UploadData and Internal 500 error

大家。 我用c#語言編程,我遇到了WebClient類的問題。 我使用方法WebClient.DownloadData下載了一個圖像,它運行正常。 但是,WebClient.UploadData沒有。 詳細地說,我有一個byte []包含一個圖像的內容 - 命名字節,以及我要上傳到的圖像文件夾的URL - 名為filePath。 然后,

 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

試試這個頁面。 它有一個帖子示例。

它使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM