内部服务器错误500:请求正文中未指定文件

时间:2017-01-26 14:13:44

标签: c# http-post console-application

我正在使用和接受图像文件的API,并在控制台C#应用程序上返回一个JSON字符串作为响应。 我使用HTTP POST方法将图像发送到API,并且我不断收到来自服务器的错误消息:远程服务器返回错误:(500)内部服务器错误。请求正文中没有指定文件,我到目前为止编写的代码:

  public static void HttpUploadFile(string url, string file, string paramName, string contentType)
    {
      string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
      byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
        wr.ContentType = "multipart/form-data; boundary=" + boundary;
        wr.Method = "POST";
        wr.KeepAlive = true;
        wr.Credentials = System.Net.CredentialCache.DefaultCredentials; 
        Stream rs = wr.GetRequestStream();  
        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
        string header = string.Format(headerTemplate, paramName, file, contentType);

        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
        rs.Write(headerbytes, 0, headerbytes.Length-1);

        FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            rs.Write(buffer, 0, bytesRead);
        }
        fileStream.Close();

        byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
        rs.Write(trailer, 0, trailer.Length);
        rs.Close();

        WebResponse wresp = null;
        try
        {
            wresp = wr.GetResponse();
            Stream stream2 = wresp.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);
           Console.WriteLine(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
            Console.ReadKey();

        }
        catch (WebException ex)
        {
           Console.WriteLine("Error uploading file  "+ ex);

            WebResponse errResp = ex.Response;
            Console.ReadKey();

            using (Stream respStream = errResp.GetResponseStream())
            {
                StreamReader reader = new StreamReader(respStream);
                string text = reader.ReadToEnd();
                Console.WriteLine(text);
                Console.ReadKey();

            }

            if (wresp != null)
            {
                wresp.Close();
                wresp = null;
            }
        }
        finally
        {
            wr = null;
        }
    }

我将从main发送以下参数:

  HttpUploadFile(URL,
             @"SomeImage.jpg", "file", "image/jpg");

图像位置位于解决方案的目录中:Project / bin / debug。

确切的错误消息: 上传文件时出错System.Net.WebException:远程服务器返回错误:(500)内部服务器错误。    在System.Net.HttpWebRequest.GetResponse() {"代码":500,"错误":"无效请求 - 请求正文中未指定文件"}

我在互联网上查找类似的问题,但无法找到问题的解决方案,是否有人知道可能是什么错误?或有任何假设?

0 个答案:

没有答案