为了账号安全,请及时绑定邮箱和手机立即绑定

Visual Studio 2012 中的 HTTPClient POST

Visual Studio 2012 中的 HTTPClient POST

C#
德玛西亚99 2021-08-22 17:30:25
我想将数据发布到 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请问代码错误在哪里?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 255 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信