【问题标题】:WebClient.UploadData and Internal 500 errorWebClient.UploadData 和内部 500 错误
【发布时间】:2011-04-14 03:32:58
【问题描述】:

每个人。我正在使用 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

【问题讨论】:

    标签: c# file-upload webclient.uploaddata


    【解决方案1】:

    试试这个页面。它有一个发布示例。

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 2017-07-04
      • 2015-11-24
      • 2014-04-21
      • 2017-04-01
      相关资源
      最近更新 更多