【问题标题】:Post request upload PDF file C#Post请求上传PDF文件C#
【发布时间】:2019-06-07 15:06:47
【问题描述】:

我正在发送附有 pdf 文件的 post 请求 example.pdf - 此文件作为“内容”添加到 Visual Studio 中的项目中。问题是我收到 400 Bad request。 API 服务器正在接收(IFormFile uploadFile),但在我的情况下,uploadedFile 为空。 授权很好,url,headers也。我通过邮递员检查了它,它工作正常。 调试模式下的 requestbody 是 '{byte[63933]}' 如何在 C# 中解决这个问题?

string pathToPdfFile = "Scenarios\DefaultScenario\example.pdf";
byte[] requestBody = File.ReadAllBytes(pathToPdfFile);     

public static string PostRequestUploadFile(string url, Dictionary<string, string> headersDictionary, byte[] requestbody)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            if (headersDictionary != null)
            {
                foreach (KeyValuePair<string, string> entry in headersDictionary)
                {
                    request.Headers.Add(entry.Key, entry.Value);
                }
            }
            request.ContentType = "application/pdf";
            Stream dataStream = request.GetRequestStream();
            byte[] byteArray = requestbody;
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                return Ex.ToString();
            }
        }

【问题讨论】:

  • 错误请求意味着服务器不喜欢你正在做的事情。可以是服务器的一些限制。你能分享一下那是什么服务器/服务吗?另外在您的dataStream 上添加using(您已经为responsestreamreader 这样做了)
  • 另一条评论:因为它是一个新的实现,我建议不要使用HttpWebRequest,因为它现在是旧的。阅读更多关于它的信息here
  • 服务器没有限制。当我使用邮递员发出类似的帖子请求时,我将桌面磁盘中的 pdf 文件添加为“正文”作为“表单数据”。 - 有用。 Pdf 文件保存在 MySql 数据库 (AWS) 中。我知道我应该考虑不使用 HttpWebRequest。
  • 您应该添加 ContentLength 标头 request.ContentLength = byteArray .Length; 以便服务器知道您将发送多少

标签: c# pdf post upload request


【解决方案1】:

我做了一些改变 我添加了内容长度标题

您可能需要将application/pdf 更改为application/x-www-form-urlencoded 最后,我不知道你在 headersDictionary 中发送了哪些参数,但可能缺少表单“文件”字段名称

var request = (HttpWebRequest)WebRequest.Create(url);

request.Method = "POST"; // Consider using WebRequestMethods.Http.Post instead of "POST"
if (headersDictionary != null){
    foreach (KeyValuePair<string, string> entry in headersDictionary){
        request.Headers.Add(entry.Key, entry.Value);
    }
}

request.ContentType = "application/pdf";
// Dependending on your server, you may have to change
// to request.ContentType = "application/x-www-form-urlencoded";

byte[] byteArray = requestbody; // I don't know why you create a new variable here
request.ContentLength = byteArray.Length;

using (var dataStream = request.GetRequestStream()){
    dataStream.Write(byteArray, 0, byteArray.Length);
}

using(var response = (HttpWebResponse)request.GetResponse()){
    using(var reader  = new StreamReader(response.GetResponseStream())){
        return reader.ReadToEnd();
    }
}

在我使用 this 的测试中,我必须使用 request.ContentType = "application/x-www-form-urlencoded" 而不是 PDF(我无法模拟您的整个设置)

由于我没有您尝试发送此内容的服务器,也没有参数,因此我无法在您的环境中对其进行测试

供将来参考,HttpWebRequest 是应避免使用的旧(过时)实现,新实现应使用 HttpClient read more

【讨论】:

    猜你喜欢
    • 2022-12-10
    • 2021-08-10
    • 2018-12-13
    • 2018-12-16
    • 2017-12-25
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多