All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
ASP.NET1.1和2.0下进行https请求的不同实现_.net资料_编程技术

Writer: aaa Article type: Programming skills(编程技巧) Time: 2013/12/11 8:28:54 Browse times: 372 Comment times: 0

ASP.NET1.1和2.0下进行https请求的不同实现_.net资料_编程技术


Head photo

Go homepage
Upload pictures
Write articles

ASP.NET1.1和2.0下进行https请求的不同实现_.net资料_编程技术-你的首页-uuhomepage.com

今天做项目中进行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;
}
}
}






There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.