如何使用Restsharp使用multipart/form-data上载XML文件? [英] How to upload an XML file using multipart/form-data with Restsharp?

查看:427
本文介绍了如何使用Restsharp使用multipart/form-data上载XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何通过Restsharp使用multipart/form-data上载XML文件?

Question: How to upload an XML file using multipart/form-data with Restsharp?

问题:

我正在使用Peppol通过Codabox API发送发票.
我想将xml上传到其余服务.
其余服务本身受提供者Codabox的控制.
我提供了2种我希望做同样的方法.

I'm using Peppol for sending invoices using the Codabox API.
I want to upload an xml to the rest service.
The rest service itself is under control by the provider Codabox.
I have 2 methods provided who I expect to do the same.

首先使用Postman和httpclient,一切正常. 我想从使用restsharp方式的httpclient方法获得相同的结果.

First of all with Postman and httpclient, all the things works fine. I want to get the same from the httpclient method working using the restsharp way.

RestSharp版本:106.2.1

RestSharp version: 106.2.1

Restsharp的错误消息

response ="StatusCode:BadRequest,Content-Type:application/json, 内容长度:-1)内容=" {\文件\":[\没有文件 已提交.\]}"

response = "StatusCode: BadRequest, Content-Type: application/json, Content-Length: -1)" Content = "{\"file\":[\"No file was submitted.\"]}"

为了实现这一点,我在标头中有一个X-Software-Company密钥,提供了一个有效的xml文件,该文件是使用form-data(multipart/form-data)和身份验证凭据发送的.

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.

期望的解决方案:

我想让Restsharp方法起作用,为什么现在不起作用. 因此,我提供的Restsharp方法需要执行与我提供的httpclient方法相同的操作.

I want to get the Restsharp method working and why it now doesn't work. So the Restsharp method I provided need to do the same as the httpclient method I provided.

我尝试过的事情:

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方法: ==>正常工作

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

邮递员生成的代码:

如果我执行邮递员的请求,没有问题,如果我检查邮递员生成的Restsharp代码,它会给我:

If I do the request by Postman there is no problem, if I check the Restsharp code generated by postman it gives me:

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.

编辑2018-03-19:

RestSharp中可能存在的问题:添加的文件无法接收#1079

EDIT 2018-03-19:

Possible issue in RestSharp: Added files not being recieved #1079

临时解决方案:

我使用的是RestSharp版本v105.2.3,那么它的工作原理就像是一种魅力.

I'm using RestSharp version v105.2.3 then it works like a charm.

有人知道为什么restsharp方法不起作用以及如何解决吗?

Have anyone an idea why the restsharp method does not work and how to solve that?

推荐答案

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

Try to put the content type parameter in the AddFile method, like this:

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

这篇关于如何使用Restsharp使用multipart/form-data上载XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆