简体   繁体   English

发布请求上传PDF文件C#

[英]Post request upload PDF file C#

I am sending post request with pdf file attached example.pdf - this file is added to project in Visual Studio as "content". 我发送的帖子请求附带pdf文件example.pdf - 此文件作为“内容”添加到Visual Studio中的项目中。 Problem is that I am receiving 400 Bad request. 问题是我收到400 Bad请求。 API server is receiving (IFormFile uploadedFile) but in my case uploadedFile is null. API服务器正在接收(IFormFile uploadedFile),但在我的情况下,uploadedFile为null。 Authorization is good, url, headers also. 授权是好的,网址,标题也是。 I checked it via postman and it is working properly. 我通过邮递员检查了它,它工作正常。 requestbody in debug mode is '{byte[63933]}' How to solve this in C#? 调试模式下的请求体是'{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();
            }
        }

I have made some changes I added the Content Length header 我做了一些更改,我添加了内容长度标题

You may have to change application/pdf to application/x-www-form-urlencoded Finally, I don't know which parameters you are sending in headersDictionary, but may be missing the form 'file' field name 您可能必须将application/pdf更改为application/x-www-form-urlencoded最后,我不知道您在headersDictionary中发送了哪些参数,但可能缺少表单'file'字段名称

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

In my tests using this I have to use request.ContentType = "application/x-www-form-urlencoded" instead of PDF (I can't mock your entire setup) 在我使用它的测试中,我必须使用request.ContentType = "application/x-www-form-urlencoded"而不是PDF(我不能模拟你的整个设置)

As I don't have the server you are trying to send this and don't have the parameters I can't test this in your environment 由于我没有服务器,因此您尝试发送此服务器并且没有参数,我无法在您的环境中对此进行测试

For future reference, HttpWebRequest is a Legacy (obsolete) implementation that should be avoided and new implementations should use HttpClient read more 为了将来参考, HttpWebRequest是一个应该避免的Legacy(过时的)实现,新的实现应该使用HttpClient 阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM