Search code examples
c#httpsssis-2012

Connecting to RESTful site using Script Task


I have been trying various combinations of HttpWebRequest objects (in SSIS) to connect to webservices geocoding site to pull back individual geocoding information with no luck. This is a JSON response and 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);

        }

It doesn't matter if I try to load cert from file or use the callback one, I still get the same error message:

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

DotNet framework version is 4.7. The site expects TLS12. I've added simple REST Client extension to chrome and tested the URL with success. It fails on the GetResponse.

What am I missing?


Solution

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