先看下请求方法

  1. public string Get_Request(
  2. string strUrl,
  3. CookieContainer _cookie = null,
  4. string strHost = "",
  5. string strRefer = "",
  6. string strOrigin = "",
  7. bool blnHttps = false,
  8. Dictionary<string, string> lstHeads = null,
  9. bool blnKeepAlive=false,
  10. string strEncoding = "utf-8",
  11. string strContentType = "",
  12. string strCertFile="",
  13. string strAccept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  14. string strUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
  15. bool blnAllowAutoRedirect = true,
  16. int intTimeout = * )
  17. {
  18. HttpWebRequest request;
  19. HttpWebResponse response;
  20. request = (HttpWebRequest)WebRequest.Create(strUrl);
  21. if (blnHttps)
  22. {
  23. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  24. request.ProtocolVersion = HttpVersion.Version10;
  25.  
  26. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  27.  
  28. }
  29. request.KeepAlive = blnKeepAlive;
  30. request.Accept = strAccept;
  31. request.Timeout = intTimeout;
  32. request.Method = "GET";
  33. request.Credentials = CredentialCache.DefaultCredentials;
  34. request.UserAgent = strUserAgent;
  35. request.AllowAutoRedirect = blnAllowAutoRedirect;
  36. request.Proxy = null;
  37. if (!string.IsNullOrEmpty(strContentType))
  38. {
  39. request.ContentType = strContentType;
  40. }
  41. if (_cookie != null)
  42. {
  43. request.CookieContainer = _cookie;
  44. }
  45. if (!string.IsNullOrEmpty(strHost))
  46. {
  47. request.Host = strHost;
  48. }
  49. if (!string.IsNullOrEmpty(strRefer))
  50. {
  51. request.Referer = strRefer;
  52. }
  53. if (!string.IsNullOrEmpty(strOrigin))
  54. {
  55. request.Headers.Add("Origin", strOrigin);
  56. }
  57. if (lstHeads != null && lstHeads.Count > )
  58. {
  59. foreach (var item in lstHeads)
  60. {
  61. request.Headers.Add(item.Key, item.Value);
  62. }
  63. }
  64. response = (HttpWebResponse)request.GetResponse();
  65. var sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(strEncoding));
  66. string strResult = sr.ReadToEnd();
  67. sr.Close();
  68. request.Abort();
  69. response.Close();
  70. return strResult;
  71.  
  72. }
  73. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  74. {
  75. return true; //总是接受
  76. }
  1. if (blnHttps)内的代码就是针对https所做的处理

需要注意的是

1、当使用https请求的时候需要确定加密协议是哪个,这个可以通过火狐查看到,如下图

2、只有Framework4.5及以上才支持1.1和1.2协议

如果仍有什么不明白的地方请留言吧

c# HttpWebRequest https的一些处理的更多相关文章

  1. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第二节:WebRequest

    本节主要来介绍一下,在C#中制造爬虫,最为常见.常用.实用的基础类 ------ WebRequest.WebResponse. 先来看一个示例 [1.2.1]: using System; usin ...

  2. 在 IIS 6 和 IIS 7中配置Https,设置WCF同时支持HTTP和HTPPS,以及使用HttpWebRequest和HttpClient调用HttpS

    IIS 7 ,给IIS添加CA证书以支持https IIS 6 架设证书服务器 及 让IIS启用HTTPS服务 WCF IIS 7中配置HTTPS C#利用HttpWebRequest进行post请求 ...

  3. 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求

    通用辅助类  下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需 要获取认证信息(如Cookie),所以返回的是HttpWeb ...

  4. C# HttpWebRequest GET HTTP HTTPS 请求

    下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需要获取认证信息(如Cookie),所以返回的是HttpWebResponse ...

  5. C#利用HttpWebRequest进行post请求的示例(HTTPS)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  6. 通过HttpWebRequest请求https接口

    一.为什么进行代理接口的开发: 有些项目需要访问被墙了哒网站,比如前不久公司开发项目需要使用google地图的接口,而google在中国被墙了,所有打算做一个代理接口服务,将代理放到国外服务器上,通过 ...

  7. (转) 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求

    转自:http://blog.csdn.net/zhoufoxcn/article/details/6404236 通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中 ...

  8. 【转】Problems with HTTPS, HttpWebRequest, and iOS?

    We're using HttpWebRequest to hit HTTPS urls, on iOS. In Debug, local builds, etc. everything works ...

  9. 【转】在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求

    http://zhoufoxcn.blog.51cto.com/792419/561934 这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全 ...

随机推荐

  1. 【noip模拟】德充符

    时间限制:2s 内存限制:512MB [题目描述] 申徒嘉和郑子产都是伯昏无人的学生,子产因为申徒嘉是残疾人,非常看不起他,于是想要刁难他. 子产给了申徒嘉 n个数 a1,a2...an. 现在他要求 ...

  2. Android app 第三方微信支付接入详解

    微信支付做了好几遍了,都没有出现什么棘手的问题,下面一一为大家分享一下,欢迎吐槽. 还是老样子,接入微信的支付要第一步添加微信支付官方的包libammsdk.jar 首先就处理略坑的一个问题,app应 ...

  3. 【71.76%】【codeforces 732A】Buy a Shovel

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【63.63%】【codeforces 724A】Checking the Calendar

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. matlab 中使用 GPU 加速运算

    为了提高大规模数据处理的能力,matlab 的 GPU 并行计算,本质上是在 cuda 的基础上开发的 wrapper,也就是说 matlab 目前只支持 NVIDIA 的显卡. 1. GPU 硬件支 ...

  6. java基础篇---文件上传(组件)

    转载自:http://www.cnblogs.com/oumyye/p/4234969.html 文件上传几乎是所有网站都具有的功能,用户可以将文件上传到服务器的指定文件夹中,也可以保存在数据库中,本 ...

  7. WPF 使用 SharpDX 在 D3DImage 显示

    原文:WPF 使用 SharpDX 在 D3DImage 显示 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://linde ...

  8. [!!**!F6782A84A3BECEAADDB11DAC0C4E6346AC07E5344100738DAF4C6DA639D9081F!!**!]

    testt 版权声明:本文博主原创文章,博客,未经同意不得转载.

  9. 熟知MySQL存储过程

    存储过程(Stored Procedure)是一组为了完毕特定功能的SQL语句集,经编译后存储在数据库中.用户通过指定存储过程的名字并给定參数(假设该存储过程带有參数)来调用运行它. MySQL 存储 ...

  10. 在服务器中无法自宿主启动一个webapi项目,引出查看系统端口是否被占用

    在windows server下启动一个webapi的.net core项目报错 如下图: 让我以为服务器还需要别的配置.后来发现原来是线程被占用了. 在windows下查看某线程是否被占用的命令如下 ...