WEB开发网      濠电娀娼ч崐濠氬疾椤愶附鍋熸い鏍ㄧ〒闂勫嫰鏌﹀Ο渚Ц闁诲氦顕ч湁婵犲﹤楠告禍鍓х磼鏉堛劌绗氶柟宄版嚇閹晠宕归銈嗘濠电偞鍨堕幐鎾磻閹捐秮褰掓偐閻戞﹩妫勯梺鎼炲妼鐎涒晝绮嬪澶樻晝闁挎繂鏌婇敃鍌涚厵閻庢稒锚閻忥絾绻濇繝鍐ㄧ伌闁诡垰鍟村畷鐔碱敂閸♀晙绱樺┑鐐差嚟婵儳螞閸曨剚鍙忛柍鍝勬噹缁€澶嬬箾閹存繄锛嶆鐐灲閹綊宕惰濡插鏌涢妸銉ヮ劉缂佸倸绉归弫鎾绘晸閿燂拷 ---闂備焦瀵уú鈺呭箯閿燂拷
开发学院WEB开发ASP.NET 1.1和2.0下进行https请求的不同实现 阅读

1.1和2.0下进行https请求的不同实现

 2006-11-07 17:16:31 来源:WEB开发网 闂備線娼уΛ鎾箯閿燂拷闂備礁鎲¢崹鐢垫崲閹扮増鍎嶆い鎺戝€甸崑鎾斥槈濞嗗秳娌紓鍌氱▌閹凤拷濠电姭鎷冮崨顓濈捕闂侀潧娲ゅú銊╁焵椤掍胶鈯曢柕鍥╁仧缁辩偤鏁撻敓锟�闂備線娼уΛ鎾箯閿燂拷  闂備胶枪缁绘鈻嶉弴銏犳瀬闁绘劖顐煎☉妯锋瀻闁归偊鍓涘▔姘舵⒑閸涘⿴娈旀繛灞傚妼閳绘捇骞嬪┑鎰濡炪倖姊婚崢褏鎲撮敓锟�
核心提示:今天做项目中进行https请求时遇到这样的调试错误,内部错误:未能为 SSL/TLS 安全通道建立信任关系,1.1和2.0下进行https请求的不同实现,错误页面:根据验证过程,远程证书无效,经过分析,在网上搜了一下一般的解决办法,但是搜的内容比较少,在浏览器中打开要进行一个安全确认,就是这个对话框引起的问题

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

http://www.cnblogs.com/david8k/archive/2006/11/06/551911.html

Tags:进行 https 请求

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接