I am trying to capture the JSON served by the following URL: https://www.gasunietransportservices.nl/en/shippers/balancing-regime/sbs-and-pos/graphactualjson/M3G
I have found various articles and solutions for my issue, I will walk through each of them below. None of them appear to work.
Simple Case
HttpWebRequest req =
(HttpWebRequest)
WebRequest.Create(
"https://www.gasunietransportservices.nl/en/shippers/balancing-regime/sbs-and-pos/graphactualjson/M3G");
var resp = req.GetResponse();
Give the following Web Exception:
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The underlying connection was closed: An unexpected error occurred on a send.
Next Try: TLS Version
From ..The underlying connection was closed: An unexpected error occurred on a receive, I tried the simple answer initially, setting the TLS Version and Expect100. Code becomes (same result with or without KeepAlive):
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.Expect100Continue = true;
HttpWebRequest req =
(HttpWebRequest)
WebRequest.Create(
"https://www.gasunietransportservices.nl/en/shippers/balancing-regime/sbs-and-pos/graphactualjson/M3G");
req.KeepAlive = false;
var resp = req.GetResponse();
Giving exception:
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The underlying connection was closed: An unexpected error occurred on a send.
Try 3: MSDN Solutions
Looking at the following MSD Article 'https://support.microsoft.com/en-us/help/915599/you-receive-one-or-more-error-messages-when-you-try-to-make-an-http-re' (linked to by another answer on other thread, the following is suggested, all of which provide the same exception already stated earlier:
Resolution A: Update to latest framework.
I have tried this up to .Net 4.5 with same result. Article originally written for .Net 1.1. I am unable to go up any further versions due to restrictions with my environments.
Resolution D: Set KeepAlive to false
As shown in excerpt above this makes no difference.
Resolution E: Set MaxIdleTime property
I have tried this as well with no difference in the result. Code becomes:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.Expect100Continue = true;
ServicePointManager.MaxServicePoints = 4;
ServicePointManager.MaxServicePointIdleTime = 1000;
HttpWebRequest req =
(HttpWebRequest)
WebRequest.Create(
"https://www.gasunietransportservices.nl/en/shippers/balancing-regime/sbs-and-pos/graphactualjson/M3G");
req.KeepAlive = false;
var resp = req.GetResponse();
Resultion F: Amend Server Timeout
The web server is an external and out of my control.
Resolution 0: HTTP-100 Header
I have tried the code listed above both with an without the 'Expect100Continue property. Same results.
I have tried all solutions above, both jointly and in isolation with no success. The code provided should run 'as is' if anyone is able to provide any suggestions.
Finally, I tried using the WebClient class, as opposed to HttpWebRequest - again identical results.
Thanks.
Edit 1 - Proxy I am able to access the URL from a browser on the same machine. I have also added the following code:
req.Proxy = new WebProxy("http://MY-PROXY-URL");
req.Proxy.Credentials = CredentialCache.DefaultCredentials;
Outcome remains unaffected.
Edit 2 - Same code, Separate Environment
Using code from 'Next Try: Tls Version', however running from my server, produces a different exception: System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
Edit 3 - Inner Exception
Complete error trace from initial code on my desktop:
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\r\n at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)\r\n at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)\r\n --- End of inner exception stack trace ---\r\n at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)\r\n at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)\r\n at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)\r\n at System.Ne
t.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)\r\n at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)\r\n at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)\r\n at System.Net.ConnectStream.WriteHeaders(Boolean async)\r\n --- End of inner exception stack trace ---\r\n at System.Net.HttpWebRequest.GetResponse()\r\n at GasUnieSBS.Program.Main(String[] args) in c:\\dev\\Sandpit\\MessagingPlay\\GasUnieSBS\\Program.cs:line 37"