我有以下 C# 代码:
static string TestWebClient(string url)
{
var urlFormatted = string.Format(url, List, UserName, AdminPw);
var returnValue = "URL:" + urlFormatted + Environment.NewLine;
try
{
using (var client = new WebClient())
{
client.Proxy = null;
using (var data = client.OpenRead(urlFormatted))
{
if (data != null)
{
using (var reader = new StreamReader(data))
{
var responseFromServer = reader.ReadToEnd();
returnValue += responseFromServer.Contains("1 members total")
? "User is a member"
: "User is not a member.";
returnValue += Environment.NewLine;
returnValue += responseFromServer;
}
}
else
{
returnValue += "Null data returned from OpenRead";
}
}
}
return returnValue;
}
catch (WebException wex)
{
if (wex.Message.Contains("(404) Not Found"))
{
returnValue =
string.Format("TestWebRequest: Page for group not found url : {0}" + Environment.NewLine + " WebException : {1}", urlFormatted, wex.Message);
}
else
{
returnValue = string.Format("TestWebRequest WebException: url : {0}" + Environment.NewLine + " Message : {1}", urlFormatted, wex.Message);
if (wex.Status == WebExceptionStatus.ProtocolError)
{
returnValue += string.Format(Environment.NewLine + "TestWebRequest: ProtocolError Status Code : {0}", ((HttpWebResponse)wex.Response).StatusCode);
returnValue += string.Format(Environment.NewLine + "TestWebRequest: ProtocolError Status Description : {0}", ((HttpWebResponse)wex.Response).StatusDescription);
}
}
return returnValue;
}
catch (Exception ex)
{
return string.Format("TestWebRequest: Error sending request url : {0}" + Environment.NewLine + " Exception : {1}", urlFormatted, ex);
}
}
当 Webclient.OpenRead()
方法尝试访问我需要的 URL(通过调用 client.OpenRead(urlFormatted)
)时,每次都会超时从 Windows Server 2008 R2 运行。
如果我通过控制台应用程序运行此代码,我会收到以下消息和堆栈跟踪:
Message : The operation has timed out
Stacktrace : at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at TestWebRequest.Program.TestWebClientDownloadString(String url)
如果我将该方法作为 asp.net 网络服务的一部分进行调用,我会得到以下信息:
System.Threading.ThreadAbortException: Thread was being aborted.
at System.Net.HttpWebRequest.GetResponse()
奇怪的是,它仅在我们的Windows Server 2008 R2 实例上运行时超时。它在我的本地设备(一台 Windows 7 机器)、一个 Windows Server 2012 实例和一个 Windows Server 2003 实例中按预期工作。
我试过传入其他 URL,导致超时的唯一 URL 是我需要的。我猜我在 Windows Server 2008 R2 上运行的应用程序与托管我需要的资源的服务器之间的通信正在发生某些事情。我说我的应用程序是因为我可以通过网络浏览器毫无困难地从我使用的任何机器访问 URL。
我正在使用 asp.net 4.0 和 c#。
最佳答案
我是与 Marc 一起工作的系统人员之一,因此我能够运行 Wireshark 并确定 TLS key 交换未完成。 TLS 协议(protocol)刚刚停止。我发现关于这个问题的适用信息很少。我使用 Nartac IIS Crypto 将未运行 2008r2 的工作 Windows 服务器与运行 2008r2 的 Windows 服务器进行了比较,并且设置是相同的。我决定尝试添加一个注册表项以禁用默认情况下不存在的客户端的 TLSv1.0。添加此 key 解决了问题。 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client] "已启用"=dword:00000000
关于c# - 为什么 WebClient.OpenRead 在 Windows Server 2008 R2 上超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33745515/