本文介绍了使用 RestSharp 在 multipart/form-data POST 中包含文件时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm relatively new to web interactions with C# and I'm having some trouble making a POST request to upload a file using an API. The API only accepts the files as part of a multipart/form-data section in the body. At other's suggestions I've been trying to use RestSharp to do this, but I can't seem to get the file itself into the POST. Chunks of code derived from Postman suggested code - where the POST works.

我尝试了一些东西.这个块导致 POST 在正文中使用正确的参数,但没有包含任何文件.

I've tried a few things. This chunk resulted in a POST going through with the correct parameter in the body, but no file was included.

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 的字节数组.通过这些迭代,我能够发布一个文件,但覆盖参数没有正确通过以强制覆盖文件.

I also tried some variations of AddFile - physical file path, byte array, and byte array with content type = application/xml. With these iterations, I was able to get a file to post, but the overwrite parameter wasn't coming through correctly to force a file 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 兼容(在这种情况下坚持使用).

*note: I'm using an older version of RestSharp (105.2.3) to be compatible with .Net 4.0 (stuck with it in this case).

对我在这里做错了什么有任何想法吗?

Any ideas as to what I'm doing wrong here?

推荐答案

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

Figured it out after sleeping on it, was really simple in the end. All of the extra webkit and multipart stuff that was throwing me for a loop is entirely unnecessary - autogenerated code from Postman is overly complicated it seems.

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

这篇关于使用 RestSharp 在 multipart/form-data POST 中包含文件时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 05:29