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();
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();
nice arcticle,
ReplyDeleteBut can you please tell us how to use that code which you had specified.
Regards,
Rohit
Hi Rohit, I have added a more verbose code.
ReplyDeleteyou given for coding solution
ReplyDeletebut 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.
Gracias, me funcionó a la perfección
ReplyDeleteThanks a lot, this issue was driving my crazy. The funiest thing is that my site has a valid SSL certificate, not a self-created.
ReplyDeleteThanks Chaudhry. I spent two days in hit & try. I had tried every other way, adding certificate, set timeout, keepalive etc ...
ReplyDeleteVery good, thanks a lot. You saved my time.
ReplyDeleteThanks again.
Thank you very much, this really works
ReplyDeleteThansk for your soltion
ReplyDeleteYou - an are my hero of the day.
ReplyDeleteThanks a lot.
ReplyDeleteSuper solution...
ReplyDeleteThanks a ton boss
yes ! it's working
Deletesir this code try me but not working in my link
ReplyDeletemy 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
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."
DeleteNewbie question - I have the same problem . Could you please tell me where I must put this code
ReplyDeleteYou can put it in any method or simply in Main() http://www.codeproject.com/Articles/479467/Main-Method-in-Csharp
ReplyDeleteThank you very much. You are a life saver
ReplyDeleteHi, we are having the same problem in an application which worked fine until now.
ReplyDeleteWhat could explain the problem knowing that we didn't change anthing the application ?
Thanks
Try browsing the url in IE and you can check if there is an error related to certificate expiration, etc.
ReplyDeleteHi Anuj,
ReplyDeletewhat is the alternative way in window phone 7 for ServicePointManager?
Thanks
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.
ReplyDeleteThanks! You helped me a lot!!!
ReplyDeleteHi Anuj
ReplyDeleteTried 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
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..
ReplyDeleteThank you, it really helped me.
ReplyDeletethanks a lot
ReplyDelete