Search code examples
c#.net.net-3.5httpwebrequest

HTTPWebRequest Could not create SSL/TLS secure channel




I am trying to call a Web API using HttpWebRequest(Console Application). To upload the file, I am using the code snippet provided by @Cristian Romanescu ont this question Upload files with HTTPWebrequest (multipart/form-data)

If I set the req.ProtocolVersion as HttpVersion.Version10,I get Bad request when I try to do req.GetResponseStream().

If i set the req.ProtocolVersion as HttpVersion.Version11,I get Could not create SSL/TLS secure channel. when i try to do req.GetResponseStream().

If tried to post the data using POSTMAN, but even Postman says Could not get any response".

If I try to hit the URL using IE-11 I get HTTP 404.[I know i am not posting the actual file], but Chrome displays the custom/appropriate error message.

I tried the solutions already provided on stackoverflow, but unfortunately they do not solve my problem.

Thankyou.


Solution

  • Two things that I was doing wrong.

    1. The Server wanted date in yyyy-MM-ddTHH:mm:ss, I was providing the date in yyyy-MM-ddTHH:mm:ss.ssss Format(DateTime.UtcNow.ToString("o")). This was the cause of Could not create SSL/TLS secure channel

    2. The server wanted the parameters in request body as well. I had passed the parameters only as query string. This was the cause of HTTP 404

    In the process, I had asked a guy outside my team to help. He had asked me to see if there were any SSL related errors. To do this he asked me to add System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); and define AcceptAllCertifications, which was not the case. So to resolve the issue, I had to take care of the things mentioned above.