C# 基础连接已关闭:发生意外错误

C# 基础连接已关闭:发生意外错误,c#,web-services,webclient,webrequest,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 +

我有一个非常简单的服务,它调用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"));
myServiceURL行的“DownloadString”抛出错误“底层连接已关闭:发生意外错误”,这行的Fiddler中没有显示任何内容,而google.com的“DownloadString”有效,我看到了控制台输出

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

client.UseDefaultCredentials = true;
client.Encoding = Encoding.UTF8;
当我在浏览器中导航到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()

这一切都是在Windows7(64位)PC上运行,使用.NETFramework4.0;myServiceURL上的服务是一项我无法控制的第三方服务。

终于找到了问题的症结所在,而答案可能并不适用于所有有相同问题的人;我认为线索在于我们可以从一些HTTPS站点获取信息,但不是全部,并通过Fiddler、WireShark和防火墙的组合跟踪事件

       //assuming this is set
        byte[] Data;
        string url = string.Format("{0};{1}" ,myServiceURL, "login");
        // Request
        HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(url);

        wreq.Method = "POST";
        wreq.Proxy = WebProxy.GetDefaultProxy();

        (wreq as HttpWebRequest).Accept = "text/xml";


        if (Data != null && Data.Length > 0)
        {
            wreq.ContentType = "application/x-www-form-urlencoded";
            System.IO.Stream request = wreq.GetRequestStream();
            request.Write(Data, 0, Data.Length);
            request.Close();
        }
        WebResponse wrsp = wreq.GetResponse();
在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 Community Edition以使用.NET Framework 4.5.2在项目中测试“相同”的代码,它立即起作用。

您是否尝试过req.Proxy=WebProxy.GetDefaultProxy();我刚刚尝试使用GetDefaultProxy(它已经被弃用了?),但没有任何区别。哦,很抱歉,您可以使用guess而不是ContentType作为HttpWebRequest。Accept=“text/xml”;您也可以参考Jon Skeet的答案,使用Wireshark将ContentType交换为Accept。。。没有变化。我不太确定这段代码与我一直使用的代码有什么显著的不同;但是我试过这个。。。同样的错误,我真的怀疑除了代码之外还有什么事情在发生,因为对google.com的相同请求和一个简单的文本文件(上传到我自己的服务器)都像我预期的那样工作。调查仍在继续,我明白,只是一次尝试。更改在contentType“application/x-www-form-urlencoded”中。请看这篇文章