【发布时间】:2015-11-16 22:07:15
【问题描述】:
我有以下 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 Web 服务的一部分调用,我会得到以下信息:
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#。
【问题讨论】:
-
在手牌期间暂停调试器,发布完整的堆栈。
-
其实我误读了这个问题。这会在您写入时超时。你是如何确保 URL 正常的?
-
我可以在我尝试过的所有机器上通过网络浏览器使用 URL 访问资源。我可以通过我的应用程序访问它,除非我的应用程序在 Windows Server 2008 R2 上运行。我试图澄清这个问题。
标签: c# asp.net c#-4.0 tcp windows-server-2008-r2