简体   繁体   English

如何在Restsharp中使用multipart / form-data上传XML文件?

[英]How to upload an XML file using multipart/form-data with Restsharp?

Question: How to upload an XML file using multipart/form-data with Restsharp? 问题:如何在Restsharp中使用multipart / form-data上传XML文件?

Problem: 问题:

I'm using Peppol for sending invoices using the Codabox API. 我正在使用Peppol通过Codabox API发送发票。
I want to upload an xml to the rest service. 我想将xml上传到其余服务。
The rest service itself is under control by the provider Codabox. 其余服务本身受提供者Codabox的控制。
I have 2 methods provided who I expect to do the same. 我提供了2种我希望做同样的方法。

First of all with Postman and httpclient, all the things works fine. 首先使用Postman和httpclient,一切正常。 I want to get the same from the httpclient method working using the restsharp way. 我想从使用restsharp方法的httpclient方法获得相同的结果。

RestSharp version: 106.2.1 RestSharp版本:106.2.1

Error message with Restsharp Restsharp的错误消息

response = "StatusCode: BadRequest, Content-Type: application/json, Content-Length: -1)" Content = "{\\"file\\":[\\"No file was submitted.\\"]}" response =“ StatusCode:BadRequest,内容类型:application / json,Content-Length:-1)” Content =“ {\\” file \\“:[\\”未提交文件。“”“}}”

For realizing this I have an X-Software-Company key in the header, providing a valid xml file that I send using form-data (multipart/form-data) and my authentication credentials. 为了实现这一点,我在标题中有一个X-Software-Company密钥,提供了一个我使用表单数据(multipart / form-data)和身份验证凭据发送的有效xml文件。

Expected solution: 预期解决方案:

I want to get the Restsharp method working and why it now doesn't work. 我想让Restsharp方法起作用,为什么现在不起作用。 So the Restsharp method I provided need to do the same as the httpclient method I provided. 因此,我提供的Restsharp方法需要执行与我提供的httpclient方法相同的操作。

What I have tried: 我试过的

Restsharp method: ==> here is the problem Restsharp方法: ==>这是问题所在

  public void TestUpload()
    {
        byte[] fileBytes = File.ReadAllBytes(@"C:\temp\test.xml");

        var client = new RestClient("url for the rest call");

        var request = new RestRequest(Method.POST);
        request.AlwaysMultipartFormData = true;

        request.Credentials = new NetworkCredential("username", "password");

        request.AddHeader("X-Software-Company", "software key");
        request.AddHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
        request.AddFile("file", @"C:\temp\test.xml");
        //request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
        //request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\temp\\test.xml\"\r\nContent-Type: false\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
    }

HttpClient method: ==> it works fine HttpClient方法: ==>正常工作

public void TestUploadHttpClient()
    {
        byte[] fileBytes = File.ReadAllBytes(@"C:\temp\test.xml");

        using (HttpClient httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "credentials");
            httpClient.DefaultRequestHeaders.Add("X-Software-Company", "software key");
            using (var content = new MultipartFormDataContent("boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"))
            {
                content.Add(new StreamContent(new MemoryStream(fileBytes)), "file", "test.xml");

                using (var message = httpClient.PostAsync("url for the rest call", content).Result)
                {
                    var input = message.Content.ReadAsStringAsync().Result;
                }
            }
        }
    }

Postman generated code: 邮递员生成的代码:

If I do the request by Postman there is no problem, if I check the Restsharp code generated by postman it gives me: 如果我按邮递员的要求进行操作没有问题,如果我检查由邮递员生成的Restsharp代码,它会给我:

var client = new RestClient("url for the rest call");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic credentials");
request.AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("X-Software-Company", "software key");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\temp\\test.xml\"\r\nContent-Type: false\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

I have exactly the code tested generated from postman but it doesn't work. 我已经测试了邮递员生成的经过测试的代码,但是它不起作用。

EDIT 2018-03-19: 编辑2018-03-19:

Possible issue in RestSharp: Added files not being recieved #1079 RestSharp中的可能问题: 无法接收添加的文件#1079

Temporary solution: 临时解决方案:

I'm using RestSharp version v105.2.3 then it works like a charm. 我正在使用RestSharp版本v105.2.3,然后它像一个魅力一样工作。

Have anyone an idea why the restsharp method does not work and how to solve that? 有谁知道为什么restsharp方法不起作用以及如何解决呢?

尝试将内容类型参数放在AddFile方法中,如下所示:

request.AddFile("file", @"C:\temp\test.xml", "application/octet-stream");

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

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