无法从网站

时间:2017-08-29 13:14:20

标签: c# httpwebrequest tls1.2

我正在尝试捕获以下URL提供的JSON: https://www.gasunietransportservices.nl/en/shippers/balancing-regime/sbs-and-pos/graphactualjson/M3G

我已经为我的问题找到了各种文章和解决方案,我将在下面逐一介绍。它们似乎都不起作用。

简单案例

HttpWebRequest req =
                (HttpWebRequest)
                    WebRequest.Create(
                        "https://www.gasunietransportservices.nl/en/shippers/balancing-regime/sbs-and-pos/graphactualjson/M3G");

            var resp = req.GetResponse();

提供以下Web异常:

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.

下一个尝试:TLS版本

..The underlying connection was closed: An unexpected error occurred on a receive,我最初尝试了简单的答案,设置了TLS版本和Expect100。代码变为(有或没有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();

给予例外:

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.

尝试3:MSDN解决方案

查看以下MSD文章“https://support.microsoft.com/en-us/help/915599/you-receive-one-or-more-error-messages-when-you-try-to-make-an-http-re”(通过其他帖子的另一个答案链接,建议如下,所有这些都提供了前面已经说明的相同例外:

解决方案A:更新到最新框架。

我已尝试过.Net 4.5,结果相同。最初为.Net 1.1撰写的文章。由于我的环境受到限制,我无法继续使用任何其他版本。

解决方案D:将KeepAlive设置为false

如上文摘录所示,这没有区别。

解决方案E:设置MaxIdleTime属性

我也试过这个,结果没有区别。代码变为:

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();

结果F:修改服务器超时

Web服务器是外部的,不受我控制。

解决方案0:HTTP-100标头

我已尝试使用不带'Expect100Continue属性的上面列出的代码。相同的结果。

我已经尝试过上述所有解决方案,无论是共同还是孤立,都没有成功。如果有人能够提供任何建议,则提供的代码应“按原样”运行。

最后,我尝试使用WebClient类,而不是HttpWebRequest - 再次使用相同的结果。

感谢。

编辑1 - 代理 我可以从同一台机器上的浏览器访问该URL。我还添加了以下代码:

req.Proxy = new WebProxy("http://MY-PROXY-URL");
            req.Proxy.Credentials = CredentialCache.DefaultCredentials;

结果仍未受影响。

编辑2 - 相同代码,独立环境

使用“Next Try:Tls Version”中的代码,无论从我的服务器运行,都会产生不同的异常: System.Net.WebException:请求已中止:无法创建SSL / TLS安全通道。

编辑3 - 内部异常

从我桌面上的初始代码完成错误跟踪:

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"

1 个答案:

答案 0 :(得分:0)

希望这有助于其他人。

通过将代码示例(" Next Try - TLS Version)发布到Windows 10上修复了该问题。我读过的所有内容听起来都像Windows 7和.Net 4.5应该支持完整的TLS1.2。

但是,对目标网址的扫描显示它们非常依赖于它们允许的内容。我们的一位安全工程师建议这是我们遇到的第二个类似问题,它与我们自己的Windows-7构建配置有关。

我还没有解决服务器上的问题(Windows 2008 R2)。