zoukankan      html  css  js  c++  java
  • .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);
            });
        });
  • 相关阅读:
    WebPart 生存周期
    【Linq to SharePoint】对列表查询的分页技术
    新闻联播 代码
    首页顶部图片带Flash代码
    [翻译]简单谈谈事件与委托
    asp.net调试
    ASP.NET 2.0加密Web.config 配置文件
    网站用户登录和验证的资料
    Membership的一些资料
    asp.net网站登录的一些资料。
  • 原文地址:https://www.cnblogs.com/ZkbFighting/p/12156170.html
Copyright © 2011-2022 走看看