zoukankan      html  css  js  c++  java
  • C#中WebRequest请求的一些心得

    在C#开发中经常需要进行Web远程访问,方法很多,也使用了很久,但一直没有做一个总结。

      C#中用来进行远程请求的方法有很多,如WebClient,WebRequest等,也各有特点。今天在这里主要介绍WebRequest。

            先从相对入门的不需要证书验证,不需要登录的开始。

    //GET类型的请求
            public string GetContent(string uri, Encoding coding)
            {
                //Get请求中请求参数等直接拼接在url中
                WebRequest request = WebRequest.Create(uri);
    
                //返回对Internet请求的响应
                WebResponse resp = request.GetResponse();
    
                //从网络资源中返回数据流
                Stream stream = resp.GetResponseStream();
    
                StreamReader sr = new StreamReader(stream, coding);
    
                //将数据流转换文字符串
                string result = sr.ReadToEnd();
    
                //关闭流数据
                stream.Close();
                sr.Close();
    
                return result;
            }
    //POST
            public string GetContentPost(string uri, string data, Encoding coding)
            {
                WebRequest request = WebRequest.Create(uri);
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";
    
                //将字符串数据转化为字节串,这也是POST请求与GET请求区别的地方
                byte[] buffer = coding.GetBytes(data);
                
                //用于将数据写入Internet资源
                Stream stream = request.GetRequestStream();
                stream.Write(buffer, 0, buffer.Length);
                request.ContentLength = buffer.Length;
    
                WebResponse response = request.GetResponse();
    
                //从网络资源中返回数据流
                stream = response.GetResponseStream();
    
                StreamReader sr = new StreamReader(stream, coding);
    
                //将数据流转换文字符串
                string result = sr.ReadToEnd();
    
                //关闭流数据
                stream.Close();
                sr.Close();
    
                return result;
            }
    //回调验证证书问题
            public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                //直接返回true,接受指定证书进行身份验证  
                return true;
            }
    
            //Get
            public string GetContent(string uri, Encoding coding)
            {
    
                //下面一行代码一定卸载请求开始前。
                //使用回调的方法进行验证。
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                
                //Get请求中请求参数等直接拼接在url中
                WebRequest request = WebRequest.Create(uri);
    
                //返回对Internet请求的响应
                WebResponse resp = request.GetResponse();
    
                //从网络资源中返回数据流
                Stream stream = resp.GetResponseStream();
    
                StreamReader sr = new StreamReader(stream, coding);
    
                //将数据流转换文字符串
                string result = sr.ReadToEnd();
    
                //关闭流数据
                stream.Close();
                sr.Close();
    
                return result;
            }
    
            //POST
            public string GetContentPost(string uri, string data, Encoding coding)
            {
                //下面一行代码一定卸载请求开始前。
                //使用回调的方法进行验证。
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
    
                WebRequest request = WebRequest.Create(uri);
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";
    
                //将字符串数据转化为字节串,这也是POST请求与GET请求区别的地方
                byte[] buffer = coding.GetBytes(data);
    
                //用于将数据写入Internet资源
                Stream stream = request.GetRequestStream();
                stream.Write(buffer, 0, buffer.Length);
                request.ContentLength = buffer.Length;
    
                WebResponse response = request.GetResponse();
    
                //从网络资源中返回数据流
                stream = response.GetResponseStream();
    
                StreamReader sr = new StreamReader(stream, coding);
    
                //将数据流转换文字符串
                string result = sr.ReadToEnd();
    
                //关闭流数据
                stream.Close();
                sr.Close();
    
                return result;
            }
    //只需在证书验证的回调函数中添加几行代码即可
    //
    回调验证证书问题 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { Console.WriteLine("证书的有效日期:" + certificate.GetEffectiveDateString()); Console.WriteLine("证书的到期日期:" + certificate.GetExpirationDateString()); Console.WriteLine("证书格式名称:" + certificate.GetFormat()); Console.WriteLine("证书办法机构名称:" + certificate.Issuer); Console.WriteLine("密钥算法信息:" + certificate.GetKeyAlgorithm()); Console.WriteLine("证书的公钥:" + certificate.GetPublicKeyString()); Console.WriteLine("证书序列号:" + certificate.GetSerialNumberString()); // 总是接受 return true; }

    使用如下GET方式访问百度。即可得到截图效果。  

        GetContent("https://www.baidu.com/", Encoding.UTF8);
        Console.WriteLine(str);

  • 相关阅读:
    实验三 进程调度模拟程序
    实验二作业调度模拟程序
    最新广商小助手 项目进展 OpenGL ES 3D在我项目中引用 代码太多只好选重要部分出来
    最后冲刺 我的项目 广商小助手
    最新一课 老师指点用Listview适配器
    安卓小学生四则运算
    大三上学期安卓一边学一边开始做一个自己觉得可以的项目 广商小助手App 加油
    我要再接再力 学更多
    用场景来规划测试工作
    阅读第13,14,15,16,17章
  • 原文地址:https://www.cnblogs.com/DSC1991/p/12552012.html
Copyright © 2011-2022 走看看