.Net Standard Http请求实例

一 、.Net Standard http协议封装

程序集:

System.Net.Http.dll

命名 空间:System.Net.Http

HttpClient :http请求 发送类

FormUrlEncodedContent:Http请求表单参数

HttpResponseMessage:  http请求相应操作

HttpContent:http请求相应内容读取

 

二、Http Get请求示例

//使用 HttpClient创建Get请求
HttpClient client = new HttpClient();
Task<HttpResponseMessage> resp = client.GetAsync("http://www.gongjuji.net");
resp.ContinueWith(q =>
{
    //获取相应状态
    HttpResponseMessage respMsg = q.Result;
    Console.WriteLine(respMsg.StatusCode);

    //获取相应内容
    HttpContent respClient = respMsg.Content;
    respClient.ReadAsStringAsync().ContinueWith(str =>
    {
        string result = str.Result;

        Console.WriteLine(result);
    });
});

三、Http  Post请求示例

//使用HttpClient 创建Post请求
HttpClient client = new HttpClient();
//指定提交表单数据
List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
paramList.Add(new KeyValuePair<string, string>("Content", "e"));
FormUrlEncodedContent data = new FormUrlEncodedContent(paramList);

client.PostAsync("http://md5.gongjuji.net/common/md5encrypt", data)
    .ContinueWith(q =>
    {
        HttpResponseMessage respMsg = q.Result;
        //读取请求结果
        respMsg.Content.ReadAsStringAsync().ContinueWith(str =>
        {
            string result = str.Result;
            Console.WriteLine(result);
        });
    });

 

四、 StringContent 类,传递string字符串

            HttpClient client = new HttpClient();
            string body = new
            {
                business_id = businessid,
                game_status = 1,
                channel_id = channel_id
            }.ToJsonString();

            string timestamp = TimeHelper.GetCurrentStr().ToString();
            string nonce = new Random().Next(100).ToString();
            //签名
            string signature = SecurityHelper.MD5(body + timestamp + access_key + nonce).ToLower();
 
            client.DefaultRequestHeaders.Add("Ual-Access-Requestid", timestamp);
            client.DefaultRequestHeaders.Add("Ual-Access-Businessid", businessid);
            client.DefaultRequestHeaders.Add("Ual-Access-Timestamp", timestamp);
            client.DefaultRequestHeaders.Add("Ual-Access-Nonce", nonce);
            client.DefaultRequestHeaders.Add("Ual-Access-Signature", signature);

            HttpContent content = new StringContent(body, Encoding.UTF8);
            content.Headers.Add("UserAgent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");


            var resp = client.PostAsync(url, content).Result;
            if (resp.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string result = resp.Content.ReadAsStringAsync().Result;
                //File.Create("text.txt");
                File.WriteAllText("test2.txt", result);
                Console.WriteLine(result);
            }
            else
            {
                Console.WriteLine("出错:");
                Console.WriteLine(resp.Headers.GetValues("ual-access-ret").FirstOrDefault());
                Console.WriteLine(resp.Headers.GetValues("trpc-error-msg").FirstOrDefault());
            }

 

 

更多:

.Net Standard简介

.Net Core配置文件读取整理

EF Core 1.0 和 SQLServer 2008 分页的问题

posted @ 2017-06-12 22:28  天马3798  阅读(1680)  评论(0编辑  收藏  举报