猿问

Visual Studio 2012 中的 HTTPClient POST

我想将数据发布到 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


请问代码错误在哪里?


德玛西亚99
浏览 256回答 1
1回答
随时随地看视频慕课网APP
我要回答