Faced with a very strange problem.
The program on the production server sends a get request to googleapis.
I get the error "The underlying connection was closed: An unexpected error occurred on a send."
OS: Windows Server 2008 R2 SP1
IIS 7
TLS 1.0/1.1./1.2, SSL 2.0/3.0 are enabled
.Net Framework 4.8
The URL "https://googleapi.com" added to the "Trusted sites" list
This URL opens in IE.
It works correctly on a working PC (and on all my others PCs) .
The problem is only on the production server.
Maybe someone has encountered something like this and will tell me where to find solution in this situation?
Test code:
string url = $"https://www.googleapis.com/analytics/v3/data/ga";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (WebResponse response = request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream);
string result = reader.ReadToEnd();
reader.Close();
Console.WriteLine($"nn{result}nn");
}
}
Exception:
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> Syste
m.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the r
emote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
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 asyncReq
uest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncReq
uest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncReq
uest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRe
quest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object
state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, B
oolean 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)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at GoogleFetchTest.Program.Main(String[] args)
2
Answers
Thanks to everyone! I was helped by the inclusion of SSL 2.0 / 3.0 in the Internet options and adding the url "https://www.gogleapis.com" to trusted sites.
Internet Option Sreenshot
This is an issue related to protocols(TLS protocol). Using below solution before calling service should solve the issue,
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12