我正在使用RestSharp上传多部分消息(基本上,表格数据末尾附有文件)。接收服务似乎要求我在每个部分上指定Content-Transfer-Encoding标头,否则我会在POST上收到400错误。
我在RestSharp中找不到任何设置方法,或者指定实际位于部件主体内部的“标题”。我需要生成的是有效载荷,如下所示:
POST http://server/object/create HTTP/1.1
User-Agent: RestSharp/105.2.3.0
Content-Type: multipart/form-data; boundary=-----------------------------28947758029299
Host: server:8080
Content-Length: 540
-------------------------------28947758029299
Content-Type: application/xml;charset=US-ASCII
Content-Disposition: form-data; name="form"
Content-Transfer-Encoding: 8bit
<form>this is some form data in XML format</form>
-------------------------------28947758029299
Content-Disposition: form-data; name="1.eml"; filename="1.email"
Content-Type: application/octet-stream; charset=ISO-8859-1
Content-Transfer-Encoding: binary
This is arbitrary file content to attach to the form.
-------------------------------28947758029299--
到目前为止,这就是我所拥有的(我在PowerShell中进行原型设计,但结果在C#中是相同的):
$request = New-Object RestSharp.RestRequest("iss", [RestSharp.Method]::POST)
$request.AddParameter("application/xml;charset=UTF-8", $form, [RestSharp.ParameterType]::RequestBody) | Out-Null
$request.AddFile("att.txt", "C:\Development\att.txt", "application/octet-stream; charset=ISO-8859-1") | Out-Null
$response = $rest.Execute($request)
除了Content-Transfer-Encoding部分外,它还能完成我需要的一切。
这是否可以使用RestSharp,还是应该直接使用HTTP客户端?