我想将数据发布到 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;
}
它使这个 URL:
POST /api/v25/upload/test HTTP/1.1
这将是正确的地址:
POST /api/v25/upload/test?field1=value1&field2=value2 HTTP/1.1
请问代码错误在哪里?
相关分类