将映像发布到c#中的Web服务器(远程服务器返回错误(500)内部服务器错误)

将映像发布到c#中的Web服务器(远程服务器返回错误(500)内部服务器错误),c#,http-post,C#,Http Post,我想使用HTTP post将图像发布到我的web服务器。 我正在使用下面的方法,从“stackoverflow”平台获得它,但是下面的代码最初给了我一个错误“(405)方法不允许”但是今天它给了我一个错误“远程服务器返回了一个错误(500)内部服务器错误” 我肯定我做错了什么。。只是需要你的专家建议 public static void HttpUploadFile(string url, string file, string paramName, string contentType, Na

我想使用HTTP post将图像发布到我的web服务器。 我正在使用下面的方法,从“stackoverflow”平台获得它,但是下面的代码最初给了我一个错误“(405)方法不允许”但是今天它给了我一个错误“远程服务器返回了一个错误(500)内部服务器错误” 我肯定我做错了什么。。只是需要你的专家建议

public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
    {
        //log.Debug(string.Format("Uploading {0} to {1}", file, url));
      //  MessageBox.Show(string.Format("Uploading {0} to {1}", file, url));
        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 formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
        foreach (string key in nvc.Keys)
        {
            rs.Write(boundarybytes, 0, boundarybytes.Length);
            string formitem = string.Format(formdataTemplate, key, nvc[key]);
            byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
            rs.Write(formitembytes, 0, formitembytes.Length);
        }
        rs.Write(boundarybytes, 0, boundarybytes.Length);

        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);

        FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[4096];
        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(); **//Catching Exception in this line...**
            Stream stream2 = wresp.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);
            MessageBox.Show(reader2.ReadToEnd(), "File uploaded, server response is: ");
           // log.Debug(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
        }
        catch (Exception ex)
        {
            //log.Error("Error uploading file", ex);
            MessageBox.Show(ex.Message, "Error uploading file");
            if (wresp != null)
            {
                wresp.Close();
                wresp = null;
            }
        }
        finally
        {
            wr = null;
        }
    }
并在按钮单击事件下按以下方式调用上述方法

 private void btn_Click(object sender, EventArgs e)
    {
        NameValueCollection nvc = new NameValueCollection();
        nvc.Add("username", "Haris");
        nvc.Add("password", "pass");
        nvc.Add("Title", "Test Image");
        nvc.Add("Comments", "Test Image");
        //nvc.Add("fileUpload1", "a.jpg");
        HttpUploadFile("http://blog.test.co/testpost.aspx", @imgpath, "fileUpload1", "image/jpeg", nvc);

    }
我希望我已经解释够了。。。 非常感谢你对此事的建议


提前谢谢

谢谢您回答我的问题。我找到了上述问题的解决办法。。 上面的客户端代码非常适合通过HTTP将图像发布到服务器,但问题是

  • 我发布的图像带有图像路径(本地硬盘的路径)而不是图像名称,这是错误的
  • 但是服务器需要一个文件名(映像名)来将文件保存在服务器的硬盘上
这是服务器响应我的主要原因(远程服务器返回错误(500)内部服务器错误)

无论如何,当服务器代码中出现任何类型的异常并且您尚未管理异常处理时,服务器会响应上述错误


希望它能帮助其他人

我认为,需要服务器代码来了解期望的内容以及服务器无法正确处理您的请求的原因。感谢Rafa的回复。。。服务器代码很好,因为我使用html表单发布所有上述详细信息。。它正在正确地发布所有数据。。。我被卡住了。。到底发生了什么。。。我不明白,我明白了,我们可以看看你们是如何从html发布这些数据的吗?即使我面临同样的问题,我得到405如何给文件名保存在服务器上,你们能解释一下吗??