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();

29 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. Super solution...
    Thanks a ton boss

    ReplyDelete
  11. 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
  12. Newbie question - I have the same problem . Could you please tell me where I must put this code

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

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

    ReplyDelete
  15. 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
  16. Try browsing the url in IE and you can check if there is an error related to certificate expiration, etc.

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

    Thanks

    ReplyDelete
  18. 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
  19. Thanks! You helped me a lot!!!

    ReplyDelete
  20. 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
  21. 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
  22. Thank you, it really helped me.

    ReplyDelete