【发布时间】:2018-06-20 08:34:01
【问题描述】:
我想将数据发布到 REST API,但它没有创建正确的请求:
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Accept", "text/csv");
HttpResponseMessage response = null;
string baseUrl = ServiceUrl + "/api/v25/upload/test";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("field1", "value1");
parameters.Add("field2", "value2");
MultipartFormDataContent form = new MultipartFormDataContent();
HttpContent content = new StringContent("long text...");
content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
HttpContent fields = new FormUrlEncodedContent(parameters);
form.Add(content, "message");
form.Add(fields);
response = client.PostAsync(baseUrl, form).Result;
var message = response.Content.ReadAsStringAsync().Result;
}
它使这个网址:
POST /api/v25/upload/test HTTP/1.1
这是正确的地址:
POST /api/v25/upload/test?field1=value1&field2=value2 HTTP/1.1
请问代码错误在哪里?
【问题讨论】:
-
您为什么认为将文件添加到 FORM 的 POST 会导致将参数添加到 URL?
-
只获取 URL 中的使用字段..
-
是的,我明白了。谢谢。
标签: c# dotnet-httpclient