everyone. I'm programming with c# language and I have a problem with WebClient class. I downloaded an image by using method WebClient.DownloadData, it worked fine. But, WebClient.UploadData didn't. In details, I ha开发者_开发技巧ve a byte[] contains a image's content - named bytes, and an URL of image folder which I want to upload to - named filePath. then,
WebClient wc = new WebClient();
byte[] responseArray = wc.UploadData(filePath, "POST", bytes);
And it return back to me following error
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)
I also researched some solutions for this problem, however none of them worked for me. :( Please help! :-s
try this page. It has a posting example.
It uses webrequest rather that webclient, but it is from microsoft and should be a good example.
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 ();
i slimmed down the code and commented the sections you will want to look at.
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
精彩评论