使用.Net Framework使用SSL证书

时间:2015-04-17 12:43:55

标签: c# ssl get yandex

我尝试向Yandex服务语音识别引擎发出GET请求以获得流识别:

GET /asr_partial HTTP/1.1\r\n
User-Agent:Ross' Third apartmentAliveClient\r\n
Host: voice-stream.voicetech.yandex.net:80\r\n
Upgrade: dictation\r\n\r\n

所以,我编写简单的代码来做到这一点:

public string HttpGet(String serviceUri, String host, String userAgent, String upgradeValue)
    {
     req = WebRequest.Create(serviceUri)
                    as HttpWebRequest;

        req.ClientCertificates.Add(_certificat);

        req.Host = host;
        req.UserAgent = userAgent;
        req.Date = DateTime.Now;
        Console.WriteLine(req.ToString());
        req.Headers["Upgrade"] = upgradeValue;
        string result = null;
        using (HttpWebResponse resp = req.GetResponse()
                                      as HttpWebResponse)
        {
            StreamReader reader =
                new StreamReader(resp.GetResponseStream());
            result = reader.ReadToEnd();
        }
        return result;
    }

但是当我调用method-catch异常时:

  

System.Net.WebException:底层连接已关闭:无法为SSL / TLS安全通道建立信任关系。

好的,我认为这个问题是我应该将( Yandex site的CA证书导入我的机器。 所以,我试着这样做 - 找到一些CA-but -Certum,Certum Level 4 CA,但据我所知 - 现在是我需要的。

好的,我编写该代码以获取证书并将其存储到文件中:

 ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateValidation);

private bool customXertificateValidation(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
    {
        if (cert.Subject == "CN=*.asr.yandex.net, O=YANDEX, OU=ITO, L=Moscow, S=Russia, C=RU")
        {
            var certBytes= cert.Export(X509ContentType.Cert);
            File.WriteAllBytes("yandexCA.txt", certBytes);
            return true;
        }
        return false;
    }

然后,我修改了方法,导入证书并将其添加到请求中:

 public string HttpGet(String serviceUri, String host, String userAgent, String upgradeValue)
    {          
        _certificat = new X509Certificate2();

        var certBytes = File.ReadAllBytes("yandexCA.txt");            
        _certificat.Import(certBytes);           

        HttpWebRequest req = WebRequest.Create(serviceUri)
                    as HttpWebRequest;

        req.ClientCertificates.Add(_certificat);

        req.Host = host;
        req.UserAgent = userAgent;
        req.Date = DateTime.Now;
        Console.WriteLine(req.ToString());
        req.Headers["Upgrade"] = upgradeValue;
        string result = null;
        using (HttpWebResponse resp = req.GetResponse()
                                      as HttpWebResponse)
        {
            StreamReader reader =
                new StreamReader(resp.GetResponseStream());
            result = reader.ReadToEnd();
        }
        return result;
    }
}

证书主题:

Subject = "CN=*.asr.yandex.net, O=YANDEX, OU=ITO, L=Moscow, S=Russia, C=RU"

但错误是一样的:

  

System.Net.WebException:底层连接已关闭:无法为SSL / TLS安全通道建立信任关系。

我应该怎么做才能解决这个错误? 我是否需要将此证书安装到我的机器上?如果是 - 如何做? - 我只得到字节数组。

P.S。在代码我使用回调:

 ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateValidation);

所以,回复是:

  

未处理的类型' System.Net.WebException'发生在System.dll中   附加信息:远程服务器发送消息:(501)不受支持。

0 个答案:

没有答案