0

I am working on to consuming the 3rd party web service . Both the client and server(Windows server 2016) has .Net Framework 4.7.2 . Web service is on HTTPS.

When I used below code I am getting error. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

System.ServiceModel.CommunicationException: An error occurred while making the HTTP request to "URL" . This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server.

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.

System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

When I passed the TLS1.2 as security protocol I am getting below error

System.NullReferenceException: Object reference not set to an instance of an object.

Detailed Error

    System.ServiceModel.CommunicationException: An error occurred while making the HTTP request to " URL "
This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case.
This could also be caused by a mismatch of the security binding between the client and the server. 
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. 
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. 
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
End of inner exception stack trace
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.TlsStream.CallProcessAuthentication(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
Server stack trace: 
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
  • TLS 1.2 is the *default* protocol in .NET 4.7.2 and Windows Server 2016. You don't need to configure anything. SSL3 is a *deprecated* protocol. Simply remove the line `ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;` This will let .NET use the OS's default, which in Windows 2016 is TLS1.2 – Panagiotis Kanavos Apr 07 '20 at 10:00
  • 1
    If you get a NullReferenceException it means you have an error in your code. This has nothing to do with TLS. – Panagiotis Kanavos Apr 07 '20 at 10:01
  • Thanks Panagiotis Kanavos. I have tried but now I am getting error System.ServiceModel.CommunicationException: An error occurred while making the HTTP request to " HTTPS URL " This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server. – Jay Parikh Apr 08 '20 at 06:24
  • You haven't posted your code or complete error information, so it's impossible to help. Complete error information means the *complete* exception string returned by Exception.ToString(). That includes the entire message, call stack and inner exceptions. Quite likely the server uses an invalid or self-signed (therefore invalid) certificate. In that case you can fix the error either by getting a valid certificate or trusting the self-signed certificate on the client machine – Panagiotis Kanavos Apr 08 '20 at 06:55
  • Hey Panagiotis Kanavos , I have sent you a mail for detailed error. Any help would be appreciated. – Jay Parikh Apr 08 '20 at 08:22
  • Post the information **in the question itself** – Panagiotis Kanavos Apr 08 '20 at 08:43
  • I have added the error in question. – Jay Parikh Apr 08 '20 at 10:30
  • That's the original error, not the NRE. This error is *caused* by using a deprecated algorithm like Ssl3. Remove the line `ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;` completely. If that doesn't work, set it to TLS1.2 explicitly with `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;`. If you google for that error text you'll see there are many duplicates like [this one](https://stackoverflow.com/questions/2013880/wcf-error-this-could-be-due-to-the-fact-that-the-server-certificate-is-not-conf) – Panagiotis Kanavos Apr 09 '20 at 07:10
  • All this is well documented [here](https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls#if-your-app-targets-net-framework-47-or-later-versions) and the answer for .NET 4.7 and later on modern OSs is `don't set the version explicitly` unless you have to – Panagiotis Kanavos Apr 09 '20 at 07:11
  • I am getting this error after removing Ssl3 and when I explicitly set to TLS1.2 I am getting System.NullReferenceExcepton In this case TLS1.2 is not assigned to protocol . As you said by default it is TLS 1.2 – Jay Parikh Apr 09 '20 at 08:13
  • Do you think Is there any issue on server side like SSL Certificate ? – Jay Parikh Apr 09 '20 at 08:14

0 Answers0