2011-04-14 197 views
0

每個人。我使用c#語言進行編程,並且在WebClient類中遇到問題。 我使用方法WebClient.DownloadData下載了一個圖像,它工作正常。但是,WebClient.UploadData沒有。 詳細來說,我有一個字節[]包含一個圖像的內容 - 名爲字節,以及一個我想上傳到圖像文件夾的URL - 名爲filePath。然後,WebClient.UploadData和Internal 500錯誤

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

+0

@Prix對不起,我剛剛收到AB Ove錯誤日誌,僅此而已。所以,它讓我瘋狂,因爲我不明白這個bug的原因。 – Michelle 2011-04-14 03:49:07

回答

2

試試這個網頁它有一個帖子例子

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

+0

我試過了,但沒有工作:( – Michelle 2011-04-14 03:44:45

+0

這種方法對你有用嗎?它不完全適應你現有的代碼,但是這應該完成你想要的任務 – 2011-04-14 04:18:29

+0

是的,它工作得很好,非常感謝Jason。我正試圖將其轉移到我現有的代碼中。祝您有個美好的一天:D – Michelle 2011-04-14 04:51:57