上传文件 - 多格式数据

Upload File - Multiform Data

嘿,我如何在 C# 代码中复制它。我已经有了 byte[] 或流形式的文件。

$ curl https://someaddress.com/ 
-F parameter1='abc123' \
-F file=@myfile.someextension \
-F parameter2='abc123'

[更新]

我已经尝试过 RestSharp,但我收到状态代码为 0 的响应。实际上它似乎甚至没有发送请求。 {"The underlying connection was closed: An unexpected error occurred on a send."}

var client = new RestClient("https://someaddress.com");

RestRequest request = new RestRequest("/",Method.POST);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("Accept", "application/json");

request.AddParameter("parameter1", "abc123");
request.AddParameter("parameter2", "abc123");

request.AddFile("fileData", fileStream.CopyTo, filename);
//request.AddFile("fileData", fileByteArray, filename);

var response= client.Execute(request);

[更新 2]

这是我可以在响应 ErrorException 字段中看到的堆栈跟踪。

InnerException = {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) 
at System.Net.HttpWebRequest.GetRequestStream()
at RestSharp.Http.WriteRequestBody(HttpWebRequest webRequest)
at RestSharp.Http.PostPutInternal(String method)

是的,您可以使用 HttpWebRequest Class (OR) You can use RestSharp NuGet Package 来实现相同的

遇到同样的问题(105.2.3),经过一番研究,我们决定获取源代码并进行跟踪。但是,在构建它之后,问题就解决了。

https://github.com/restsharp/RestSharp/issues/860

我尝试过使用 HttpWebRequest Class 或其他项目,但没有成功。我也尝试下载并构建项目,但没有成功。一段时间后,我发现问题出在安全协议上。所以我添加了这一行并且它起作用了。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;