Friday, January 6, 2012

Could not establish trust relationship for the SSL/TLS secure channel

This is a common problem in the testing world where you deploy a service on a HTTPS endpoint by using a self-signed certificate.
When you try to send a request to this service using HTTPWebRequest/WebClient, you see the error:
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
The solution to this problem is to ignore the trust by using the following code:

using System.Net;
using System.Net.Security;

           
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.mysite.com");

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream stream = response.GetResponseStream();

StreamReader sr = new StreamReader(stream);

string resp = sr.ReadToEnd();

27 comments:

  1. nice arcticle,
    But can you please tell us how to use that code which you had specified.

    Regards,
    Rohit

    ReplyDelete
  2. Hi Rohit, I have added a more verbose code.

    ReplyDelete
  3. you given for coding solution
    but in my case view report and build executed successfully but when i deploy my report it self it was showing following error.


    The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

    ReplyDelete
  4. Gracias, me funcionó a la perfección

    ReplyDelete
  5. Thanks a lot, this issue was driving my crazy. The funiest thing is that my site has a valid SSL certificate, not a self-created.

    ReplyDelete
  6. Thanks Chaudhry. I spent two days in hit & try. I had tried every other way, adding certificate, set timeout, keepalive etc ...

    ReplyDelete
  7. Very good, thanks a lot. You saved my time.
    Thanks again.

    ReplyDelete
  8. Thank you very much, this really works

    ReplyDelete
  9. Thansk for your soltion

    ReplyDelete
  10. You - an are my hero of the day.

    ReplyDelete
  11. Super solution...
    Thanks a ton boss

    ReplyDelete
  12. sir this code try me but not working in my link
    my link is "https://smsgw.nic.in/sendmsg.php?uname=*******&pass=******&send=******&dest=mobile no&msg=hiallswell"
    error
    The remote server returned an error: (403) Forbidden.

    help me my email bkp4444@gmail.com

    ReplyDelete
    Replies
    1. It could be due to bad password or something similar. This solution wont work for 403 Forbidden error, it will only work if you are getting the error "Could not establish trust relationship for the SSL/TLS secure channel."

      Delete
  13. Newbie question - I have the same problem . Could you please tell me where I must put this code

    ReplyDelete
  14. You can put it in any method or simply in Main() http://www.codeproject.com/Articles/479467/Main-Method-in-Csharp

    ReplyDelete
  15. Thank you very much. You are a life saver

    ReplyDelete
  16. Hi, we are having the same problem in an application which worked fine until now.

    What could explain the problem knowing that we didn't change anthing the application ?

    Thanks

    ReplyDelete
  17. Try browsing the url in IE and you can check if there is an error related to certificate expiration, etc.

    ReplyDelete
  18. Hi Anuj,
    what is the alternative way in window phone 7 for ServicePointManager?

    Thanks

    ReplyDelete
  19. Try asking in Windows Phone forum. If there isn't an alternative, you can import the certificate on the phone so that its trusted and you don't see the error.

    ReplyDelete
  20. Thanks! You helped me a lot!!!

    ReplyDelete
  21. Hi Anuj

    Tried solution that you have described above but still getting error.
    error comes on line : WebRequest.GetRequestStream()
    I checked with IE it does not show any error but does not work , From Firefox URL works fine.
    I am using proxy. The code was working earlier and has suddenly started throwing this error.

    thanks
    Meghana

    ReplyDelete
  22. Sir thanks for the code but i get another error "The underlying connection was closed: An unexpected error occurred on a receive." please help me Sir..

    ReplyDelete