用c#发布图像到Web服务器(远程服务器返回一个错误(500)内部服务器错误)

本文关键字:服务器 错误 返回 一个 内部 Web 布图像 图像 | 更新日期: 2023-09-27 18:03:09

我想使用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;
        }
    }

,并以以下方式调用上面的方法在按钮Click event…

 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);
    }
我希望我已经解释得够多了。如能就此事提供建议,我将不胜感激。

Thanks in advance

用c#发布图像到Web服务器(远程服务器返回一个错误(500)内部服务器错误)

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

  • 我发布的是图像路径(本地硬盘路径)而不是图像名称,这是错误的。
  • 但是服务器需要一个文件名(图像名称)来将文件保存在服务器的硬盘上..

这是服务器响应我的主要原因(远程服务器返回了一个错误(500)内部服务器错误)。

无论如何,当服务器代码中出现任何类型的异常时,服务器响应上述错误,而您没有管理异常处理。

希望对别人有所帮助