码迷,mamicode.com
首页 > Web开发 > 详细

httpclient之get/post

时间:2015-03-09 15:40:52      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

终于搞得差不多明白了,总结如下(省略了try catch),参考自这里

1.get:

一坨url带着"编码好"(urlencode需要自己处理)的参数(可以看到)一块发送给服务器:eg:

技术分享
public async Task<string> GetAsync(string uri)
{
    var httpClient = new HttpClient {Timeout = new TimeSpan(0, 0, 0, 17)};
    var content = await httpClient.GetStringAsync(uri);
    return content;
}
Get Method

2.post:

一坨url+看不到的参数一块转化为数据流(urlencode可以提前处理)发送给服务器。

eg:

技术分享
 public async Task<string> PostAsync(string url, Dictionary<string, string> para)
        {
            var httpClient = new HttpClient {Timeout = new TimeSpan(0, 0, 0, 17)};
            var contenta = new FormUrlEncodedContent(para);
            var response = await httpClient.PostAsync(url, contenta);
            string content = await response.Content.ReadAsStringAsync();
            return content;
        }
Post Method

 

httpclient之get/post

标签:

原文地址:http://www.cnblogs.com/defoliate/p/4323455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!