c# HttpWebRequest https的一些处理
先看下请求方法
public string Get_Request(
string strUrl,
CookieContainer _cookie = null,
string strHost = "",
string strRefer = "",
string strOrigin = "",
bool blnHttps = false,
Dictionary<string, string> lstHeads = null,
bool blnKeepAlive=false,
string strEncoding = "utf-8",
string strContentType = "",
string strCertFile="",
string strAccept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
string strUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
bool blnAllowAutoRedirect = true,
int intTimeout = * )
{
HttpWebRequest request;
HttpWebResponse response;
request = (HttpWebRequest)WebRequest.Create(strUrl);
if (blnHttps)
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request.ProtocolVersion = HttpVersion.Version10; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; }
request.KeepAlive = blnKeepAlive;
request.Accept = strAccept;
request.Timeout = intTimeout;
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
request.UserAgent = strUserAgent;
request.AllowAutoRedirect = blnAllowAutoRedirect;
request.Proxy = null;
if (!string.IsNullOrEmpty(strContentType))
{
request.ContentType = strContentType;
}
if (_cookie != null)
{
request.CookieContainer = _cookie;
}
if (!string.IsNullOrEmpty(strHost))
{
request.Host = strHost;
}
if (!string.IsNullOrEmpty(strRefer))
{
request.Referer = strRefer;
}
if (!string.IsNullOrEmpty(strOrigin))
{
request.Headers.Add("Origin", strOrigin);
}
if (lstHeads != null && lstHeads.Count > )
{
foreach (var item in lstHeads)
{
request.Headers.Add(item.Key, item.Value);
}
}
response = (HttpWebResponse)request.GetResponse();
var sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(strEncoding));
string strResult = sr.ReadToEnd();
sr.Close();
request.Abort();
response.Close();
return strResult; }
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
if (blnHttps)内的代码就是针对https所做的处理
需要注意的是
1、当使用https请求的时候需要确定加密协议是哪个,这个可以通过火狐查看到,如下图

2、只有Framework4.5及以上才支持1.1和1.2协议
如果仍有什么不明白的地方请留言吧
c# HttpWebRequest https的一些处理的更多相关文章
- 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第二节:WebRequest
本节主要来介绍一下,在C#中制造爬虫,最为常见.常用.实用的基础类 ------ WebRequest.WebResponse. 先来看一个示例 [1.2.1]: using System; usin ...
- 在 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请求 ...
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需 要获取认证信息(如Cookie),所以返回的是HttpWeb ...
- C# HttpWebRequest GET HTTP HTTPS 请求
下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需要获取认证信息(如Cookie),所以返回的是HttpWebResponse ...
- C#利用HttpWebRequest进行post请求的示例(HTTPS)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- 通过HttpWebRequest请求https接口
一.为什么进行代理接口的开发: 有些项目需要访问被墙了哒网站,比如前不久公司开发项目需要使用google地图的接口,而google在中国被墙了,所有打算做一个代理接口服务,将代理放到国外服务器上,通过 ...
- (转) 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
转自:http://blog.csdn.net/zhoufoxcn/article/details/6404236 通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中 ...
- 【转】Problems with HTTPS, HttpWebRequest, and iOS?
We're using HttpWebRequest to hit HTTPS urls, on iOS. In Debug, local builds, etc. everything works ...
- 【转】在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
http://zhoufoxcn.blog.51cto.com/792419/561934 这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全 ...
随机推荐
- C++基础之字符数组和字符串
无意中发现了一个非常有意思的技术类型小品文系列,通过大牛指导菜鸟的方式,解说讲C++知识,有的非常基础却是开发中easy忽略的地方. [Elminster的专栏] http://blog.csdn.n ...
- 【64.22%】【codefoces round 382A】Ostap and Grasshopper
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- android:layout_gravity和android:gravity属性的差别
gravity的中文意思就是"重心",就是表示view横向和纵向的停靠位置 android:gravity:是对view控件本身来说的,是用来设置view本身的文本应该显示在vie ...
- asp.net中c#求百分比
double m= 50;double n= 100; Response.Write((m/ (m+ n)).ToString("0%"));Response.Write((m/ ...
- iis启动 服务无法在此时接受控制信息。 (异常来自 HRESULT:0x80070425)
原文:iis启动 服务无法在此时接受控制信息. (异常来自 HRESULT:0x80070425) 问题描述:每隔一段时间应用程序池就会自动停止,报错:服务无法在此时接受控制信息. (异常来自 HRE ...
- 论题Cascade Object Detection with Deformable Part Models一个怀疑
该文的作者是Pedro F. Felzenszwalb等一下,著名DPM在目标检测模型.本文的工作是DPM(变形组件模型)级联,以加快检测速度. 加速的方式,现在其次是计算总结成绩的某些部分,假设小于 ...
- 1.跟着微软 https://docs.microsoft.com/zh-cn/dotnet/core/ 学习.net core
10分钟快速使用 安装之后 打开cmd 第一步. dotnet new console -o firstApp 第二步. cd firstApp 第三部.dotnet run 这样就运行了hello ...
- Android获取百度音乐下载音乐和歌词下载链接
首先,你必须通过以下连接下载歌曲: http://box.zhangmen.baidu.com/x?op=12&count=1&title={title}$${author}$$$$ ...
- python 教程 第三章、 运算符与表达式
第三章. 运算符与表达式 1) 运算符 + 加 - 减 * 乘 ** 幂 / 除 // 取整除 % 取模 << 左移 >> 右移 & 按位与 | 按位或 ^ 按位 ...
- 卷积(convolution)与相关(correlation)(matlab 实现)
1. 卷积(convolution) 输出 y(n) 是作为在 x(k) 和 h(n−k)(反转和移位)重叠之下的样本和求出的. 考虑下面两个序列: x(n)=[3,11,7,0,−1,4,2],−3 ...