底层连接已关闭:发生意外错误

Sea*_*ean 3 c# web-services webclient webrequest

我有一个非常简单的服务,它调用URL并捕获由该服务写出的状态;

// Service call used to determine availability
System.Net.WebClient client = new System.Net.WebClient();

// I need this one (sorry, cannot disclose the actual URL)
Console.WriteLine(client.DownloadString(myServiceURL + ";ping"));

// I added this for test purposes
Console.WriteLine(client.DownloadString("https://www.google.com"));
Run Code Online (Sandbox Code Playgroud)

myServiceURL行的"DownloadString"引发错误"底层连接已关闭:发生了意外错误",Fiddler中没有显示此行,而google.com的"DownloadString"工作正常,我看到控制台输出.

根据错误的其他建议,我尝试了设置UseDefaultCredentials,编码选项,为请求添加适当的标头的组合,这些都没有任何区别.

client.UseDefaultCredentials = true;
client.Encoding = Encoding.UTF8;
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中导航到myServiceURL时,它正常工作,并按预期显示"OK".

来自同一服务的另一种方法编码如下:

// Request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(myServiceURL + ";login");

// Set the request configuration options
req.Method = "POST";
req.ContentType = "text/xml";
req.ContentLength = bytes.Length;
req.Timeout = -1;

// Call for the request stream
using (Stream os = req.GetRequestStream())
{
    os.Write(bytes, 0, bytes.Length);
}

// ....snip

// This line fails with the same error as before
WebResponse resp = req.GetResponse()
Run Code Online (Sandbox Code Playgroud)

这一切都在使用.NET Framework 4.0的Windows 7(64位)PC上运行; myServiceURL上的服务是我无法控制的第三方服务.

Sea*_*ean 6

终于到底了,虽然答案可能不适用于遇到同样问题的每个人; 我建议我们可以从某些HTTPS站点获取信息,但不是全部,并通过Fiddler,WireShark和我们的防火墙组合追踪事件.

在Google Chrome浏览器中打开网站并点击网站URL地址中的"https"挂锁,查看"安全概述",我们看到,对于我们尝试过的大多数网站,都列出了"有效证书"和对于'安全资源',但是这个网站还有一个"安全TLS连接"条目,WireShark确认握手(来自Chrome)使用的是TLS v1.2

TLS v1.2似乎仅支持.NET Framework 4.5(或更高版本),因此需要Visual Studio 2012(或更高版本)

我们目前正在使用Visual Studio 2010运行.NET Framework 4.0

下载Visual Studio 2015社区版,使用.NET Framework 4.5.2测试项目中的"相同"代码,并立即开始工作.