1.1和2.0下进行https请求的不同实现
来源:互联网 发布:手机淘宝怎么看信用度 编辑:程序博客网 时间:2024/05/26 17:49
今天做项目中进行https请求时遇到这样的调试错误,内部错误:未能为 SSL/TLS 安全通道建立信任关系。错误页面:根据验证过程,远程证书无效。经过分析,在浏览器中打开要进行一个安全确认。就是这个对话框引起的问题。在网上搜了一下一般的解决办法,但是搜的内容比较少,现在在这里总结一下。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//for 1.1 在2.0下ServicePointManager.CertificatePolicy已经过时
//ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
//for 2.0
//ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://someurl");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Page.Response.Write(readStream.ReadToEnd());
response.Close();
readStream.Close();
}
//for 2.0
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // Always accept
return true;
}
//for 1.1
internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy()
{
}
public bool CheckValidationResult(ServicePoint sPoint, System.Security.Cryptography.X509Certificates.X509Certificate cert, WebRequest wRequest, int certProb)
{
// Always accept
return true;
}
}
}
- 1.1和2.0下进行https请求的不同实现
- .net2005和2003下进行https请求的不同实现
- AFNetworking进行https请求遇到的错误和解决方式
- HTTP与HTTPS不同请求的区别
- iOS9----HTTPS 进行网络请求的解决方案
- android json实现网络请求 和普通的http请求 还有https请求安全认证
- HTTPS进行网络请求
- https请求和http请求的转换
- Window下使用Charles对手机的Https请求进行抓包
- 浏览器和服务器在基于https进行请求链接到数据传输过程中,用到的技术
- android ksoap2下webservice的 https请求
- JAVA利用HttpClient进行POST和GET请求(HTTPS)
- 用http和https请求进行访问数据
- HTTP和HTTPS的请求和响应
- http 和 https 的请求区别
- C#利用HttpWebRequest进行post请求的示例(HTTPS)
- java进行https请求以及相关证书的生成
- java 实现https请求
- 验证字符串是否为空
- 验证字符串是否为数字
- 验证字符串是否为日期
- XML基础 (2)
- 精典的笑话 中美撞机事件大话西游版
- 1.1和2.0下进行https请求的不同实现
- javadoc,在 Java 的注释上做文章
- 翻页控件
- Collections容器的搜索方法
- ASP.NET的Page.IsPostBack 属性详细说明
- 禅意花园zen of css出版前----征集国内网站公司设计师和程序员对此书的评价
- 采用母板页的页面使用JavaScript问题
- Tomcat+JSP经典配置实例
- Unable to load class for JSP