使用脚本任务连接到RESTful站点

时间:2018-04-04 19:36:19

标签: c# https ssis-2012

我一直在尝试HttpWebRequest个对象(在SSIS中)的各种组合,以连接到网络服务地理编码网站,以便在没有运气的情况下撤回个别地理编码信息。这是一个JSON响应和https URL。

        string wUrl = "https://geocoding.geo.census.gov/geocoder/geographies/onelineaddress?address=1600+Pennsylvania+Ave+NW,+Washington,+DC,+20500&benchmark=Public_AR_Current&vintage=4&format=json";
        X509Certificate cert = X509Certificate.CreateFromCertFile("C:\\Users\\kwhauser\\geocode.cer");

        HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
        //WebRequest httpWReq = WebRequest.Create(wUrl);
        //httpWReq.Method = "GET";
        httpWReq.ClientCertificates.Add(cert);
        //httpWReq.ContentType = "application/json";

        //ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
        //ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

        //HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
        //HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
        try
        {
            HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
            //WebResponse response = httpWReq.GetResponse();
            //Stream responseStream = response.GetResponseStream();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            if (ex.InnerException != null)
                MessageBox.Show(" InnerException: " + ex.InnerException.Message);

        }

如果我尝试从文件加载cert或使用回调函数无关紧要,我仍然会收到相同的错误消息:

System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.

DotNet框架版本是4.7。该网站预计TLS12。我已经为chrome添加了简单的REST Client扩展,并成功测试了URL。它在GetResponse上失败。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;