0

I'm trying to read an xml file from a https url

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
using(WebClient client = new WebClient()) {
   contents = client.DownloadString(dr["XmlImpotURL"].ToString() + dr["ApiKey"].ToString());
}

I get this error

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

I spent like 2 hours to resolve this and I can't seem to find any solutions.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Tunisiano32
  • 180
  • 1
  • 8
  • 1
    Did you display the entire exception by using `ex.ToString()`? There may be an `InnerException` trying to tell you in more detail what the problem is. – John Saunders Oct 22 '13 at 00:02
  • For sample code it is much better to provide representative sample data instead of random properties (i.e. `DownloadString("https://myserver/foo?bar=222")` instead of `DownloadString(NotGoingToShowValue)` ) – Alexei Levenkov Oct 22 '13 at 00:21
  • Check with my answer if you are still facing problem ... share the URL and tell us what you need excatly ... What is tha " dr " you have mentioned – Aravind Oct 22 '13 at 04:48

2 Answers2

0

try this

        string sVal = "http://www.w3schools.com/xml/note.xml";
        XDocument document = XDocument.Load(sVal);

or

   Uri url = new Uri("http://www.w3schools.com/xml/note.xml");
        using (var wc = new WebClient())
        {
            string sss =  wc.DownloadString(url);
        }

if you are facing Security Issue

try this

  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "GET";
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;


    // allows for validation of SSL conversations
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };


    WebResponse respon = req.GetResponse();
    Stream res = respon.GetResponseStream();

    string ret = "";
    byte[] buffer = new byte[1048];
    int read = 0;
    while ((read = res.Read(buffer, 0, buffer.Length)) > 0)
    {
        Console.Write(Encoding.ASCII.GetString(buffer, 0, read));
        ret += Encoding.ASCII.GetString(buffer, 0, read);
    }
    return ret;

or

 private void Test()
 {    ServicePointManager.ServerCertificateValidationCallback += new                       
      RemoteCertificateValidationCallback(Certificate);
  }

 private static bool Certificate(object sender, X509Certificate certificate,  
                             X509Chain chain, SslPolicyErrors  policyErrors) {
                           return true;
                       }

check this link for more info http://blogs.msdn.com/b/dgorti/archive/2005/09/18/471003.aspx

Aravind
  • 1,521
  • 2
  • 12
  • 23
0

This is resolved, Instead of

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;

I used

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;

Now it's working, Thanks everyone for you answers.

Tunisiano32
  • 180
  • 1
  • 8