zoukankan      html  css  js  c++  java
  • 8网络请求之http

    本篇博客对应视频讲解

    回顾

    上一篇讲了Linq的使用,大家自己上手实践之后,相信一定会感到非常快捷方便。更多详细的内容还是需要自己去阅读官方文档。 今天要讲网络请求中的http请求,这也是在编程当中经常使用到的内容之一。

    Http请求

    关于Http是什么,请求类型这些内容我在此都不说了,需要大家自己阅读相关资料。通常来讲,我们在进行数据采集,API调用,模拟登录等功能开发的时候,免不了使用http请求。 即使用浏览器能做到的事情,我们都可以通过编程去实现。毕竟浏览器本身也是一种应用程序,也是编程的产出结果。 关于.Net 平台提供的网络编程相关内容,大家可以阅读官方文档

    常用的类

    进行网络请求,对于一般的需求,我们使用HttpClientWebClient类即可。WebClient进行了更高级的封装,更容易使用,同时也意味着更少的自定义,更低的灵活性。 通常我们进行网络请求要进行相关的步骤:

    1. 分析目标源,比如编码格式,返回内容,是否需要鉴权,访问限制等。
    2. 发送请求获取返回数据。
    3. 处理返回的数据。

    我们通过实际的例子来说明:

    1. 将百度首页保存成baidu.html
                // webclient 的简单使用
                using (var wc = new WebClient())
                {
                    // 设置编码
                    wc.Encoding = Encoding.UTF8;
                    // 请求内容
                    var result = wc.DownloadString("https://www.baidu.com");
                    // 保存到文件
                    File.WriteAllText("baidu.html", result);
                }
    
    1. 使用httpClient进行多种方式请求 首先要知道请求可以有多种方式,可以传递参数、文件等内容。不同的类型要构造不同的请求内容的结构体。 C#提供了多种类型(继承HttpConent类)作为请求内容的容器。
    // httpClient 请求
        using (var hc = new HttpClient())
        {
            string result = "";
            var httpResponse = new HttpResponseMessage();
    
            // get请求
            httpResponse = hc.GetAsync("https://www.baidu.com").Result;
            result = httpResponse.Content.ReadAsStringAsync().Result;
            Console.WriteLine(result);
    
            // post请求,构造不同类型的请求内容
            var data = new List<KeyValuePair<string, string>> {
                new KeyValuePair<string, string>("from","msdev.cc"),
            };
            // 封闭成请求结构体
            var content = new FormUrlEncodedContent(data);
            // 进行请求,获取返回数据
            httpResponse = hc.PostAsync("https://msdev.cc", content).Result;
            // 将返回数据作为字符串
            result = httpResponse.Content.ReadAsStringAsync().Result;
    
            File.WriteAllText("post.html", result);
        }
    
        // 自定义请求及结果处理
        using (var hc = new HttpClient())
        {
            string result = "";
            var httpRequest = new HttpRequestMessage();
            var httpResponse = new HttpResponseMessage();
            // 请求方法
            httpRequest.Method = HttpMethod.Put;
            // 请求地址
            httpRequest.RequestUri = new Uri("https://msdev.cc");
            // 请求内容
            httpRequest.Content = new StringContent("request content");
            // 设置请求头内容
            httpRequest.Headers.TryAddWithoutValidation("", "");
            // 设置超时
            hc.Timeout = TimeSpan.FromSeconds(5);
            // 获取请求结果 
            httpResponse = hc.SendAsync(httpRequest).Result;
            // 判断请求结果
            if (httpResponse.StatusCode == HttpStatusCode.OK)
            {
                result = httpResponse.Content.ReadAsStringAsync().Result;
                File.WriteAllText("custom.html", result);
            }
            else
            {
                Console.WriteLine(httpResponse.StatusCode + httpResponse.RequestMessage.ToString());
            }
        }
    
  • 相关阅读:
    【git】强制覆盖本地代码(与git远程仓库保持一致)
    ffmpeg CLI常用命令
    旧机改造步骤
    macbook air 2012 mid 安装 windows10 双系统遇到错误 no bootable device insert boot disk and press any key
    window、Linux 文本文件转换
    phalcon bug: model的findFirst会自动忽略一些空格
    oss2罗列所有文件
    如何让linux的history命令显示时间记录
    nginx 常用配置
    shell脚本 切换用户
  • 原文地址:https://www.cnblogs.com/msdeveloper/p/9773833.html
Copyright © 2011-2022 走看看