【问题标题】:Trouble including a file in multipart/form-data POST using RestSharp使用 RestSharp 在 multipart/form-data POST 中包含文件时遇到问题
【发布时间】:2019-09-30 23:06:32
【问题描述】:

我对使用 C# 进行 Web 交互比较陌生,并且在使用 API 上传文件的 POST 请求时遇到了一些问题。 API 只接受文件作为正文中 multipart/form-data 部分的一部分。根据其他人的建议,我一直在尝试使用 RestSharp 来执行此操作,但我似乎无法将文件本身放入 POST。从 Postman 建议代码派生的代码块 - POST 工作的地方。

我已经尝试了一些方法。此块导致 POST 通过正文中的正确参数,但未包含文件。

var client = new RestClient(postURL);
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", string.Format("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"upfile\"; filename=\"{0}\"\r\nContent-Type: application/xml\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"overwrite\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", xmlPath), ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string test = response.Content.ToString();

我还尝试了 AddFile 的一些变体 - 物理文件路径、字节数组和内容类型 = application/xml 的字节数组。通过这些迭代,我能够获得要发布的文件,但 overwrite 参数未正确通过以强制文件覆盖。

var client = new RestClient(postURL);
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
// The different part below
request.AddFile("upfile", @xmlPath);
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"overwrite\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string test = response.Content.ToString();

*注意:我正在使用旧版本的 RestSharp (105.2.3) 以与 .Net 4.0 兼容(在这种情况下坚持使用它)。

关于我在这里做错了什么有什么想法吗?

【问题讨论】:

    标签: c# restsharp


    【解决方案1】:

    睡了之后想通了,最后真的很简单。让我陷入循环的所有额外的 webkit 和多部分的东西都是完全没有必要的 - 来自 Postman 的自动生成的代码似乎过于复杂。

    var client = new RestClient(postURL);
    var request = new RestRequest(Method.POST);
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
    request.AddFile("upfile", @xmlPath);
    request.AddParameter("overwrite", "true", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
    string test = response.Content.ToString();
    

    【讨论】:

      猜你喜欢
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 2018-08-06
      • 2015-09-19
      • 1970-01-01
      相关资源
      最近更新 更多