如何发送带参数的帖子

时间:2016-04-11 22:56:46

标签: c# http-post httprequest

我想发送带参数的POST,但我不知道该怎么做。

我的代码:

 Uri resourceAddress = new Uri("http://web.com/geo");
 try
 {
    HttpResponseMessage response=await httpClient.PostAsync(resourceAddress,
        new HttpStringContent("")).AsTask(cts.Token);
 }
 catch (Exception ex) { }
 finally { }

如何发送包含以下代码的帖子:{ latitude:-1.323141, lng:24.42342 }

1 个答案:

答案 0 :(得分:1)

使用要发送的键/值填充HttpContent。

具体做法是:

Uri resourceAddress = new Uri("http://web.com/geo");
var values = new Dictionary<string, double>
{
   { "latitude", -1.323141 },
   { "lng", 24.42342 }
};

var content = new FormUrlEncodedContent(values);

try{
    HttpResponseMessage response=await httpClient.PostAsync(resourceAddress,
         content).AsTask(cts.Token);
 } catch (Exception ex){
 }finally{

 }