当前位置:编程学习 > C#/ASP.NET >>

关于使用OpenContactsNet导入MSN联系人的问题"基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系"

在使用OpenContactsNet导入MSN联系人时,报基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。郁闷了好久,请高人指点,不胜感谢!
部分代码如下:

           UriBuilder urib = new UriBuilder();
         urib.Scheme = "HTTPS";
         urib.Path = string.Format( "/{0}/LiveContacts", credential.UserName );
         urib.Host = "cumulus.services.live.com";
         urib.Port = 443;
         HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( urib.Uri );
         string authHeader = string.Format( "WLID1.0 t=\"{0}\"", ticket );
         request.Headers.Add( "Authorization", authHeader );
         WebResponse response = request.GetResponse();//这句就报以上的错误:"基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系" --------------------编程问答-------------------- HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( urib.Uri );
上加行
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 

然后再加两个函数
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        { // Always accept 
            return true;
        }
        public bool CheckValidationResult(ServicePoint sPoint, System.Security.Cryptography.X509Certificates.X509Certificate cert, WebRequest wRequest, int certProb)
        {
            // Always accept 
            return true;
        }  --------------------编程问答-------------------- --------------------编程问答-------------------- 帮顶!!! --------------------编程问答-------------------- 看看:


private void button6_Click(object sender, EventArgs e)//下载数据文件
        {
            string downloadAdd = "https://scm.auchan.com.cn/php/scm_items_stat_download.php?sup_no=au&code=all";
            string saveFile = "d:\\a.xls";
            if (downfile(downloadAdd, saveFile))
            {
                MessageBox.Show("下载完毕!");
            }
            else
            {
                MessageBox.Show("下载过程中出现错误:");
            }
        }
        public bool downfile(string url, string LocalPath)
        {
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                Uri u = new Uri(url);
                HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(u);
                mRequest.Method = "GET";
                mRequest.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse();
                Stream sIn = wr.GetResponseStream();
                FileStream fs = new FileStream(LocalPath, FileMode.Create, FileAccess.Write);
                long length = wr.ContentLength;
                long i = 0;                
                while (i < length)
                {
                    byte[] buffer = new byte[1024];
                    i += sIn.Read(buffer, 0, buffer.Length);
                    fs.Write(buffer, 0, buffer.Length);                   
                }
                sIn.Close();
                wr.Close();
                fs.Close();
                return true;
            }
            catch (WebException exp)
            {
                MessageBox.Show(exp.Message, "error");
                return false; 
            }

        }
        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {   //   Always   accept   
            return true;
        }
命名空间加上
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
--------------------编程问答-------------------- UP --------------------编程问答-------------------- 哈哈  谢谢分享了 
--------------------编程问答-------------------- 还是有问题。。这个东西报错了! --------------------编程问答-------------------- 不行啊!报错:服务法连接到服务器!
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,