【发布时间】:2015-02-08 17:26:47
【问题描述】:
我有一些代码调用 HttpWebRequest 的 GetResponse() 方法从 URL 检索 HTML 并将其返回给调用方法。
这在我的开发和 QA 环境中运行良好,但现在我已将其上传到我的 UAT 服务器,我不断收到以下错误:
远程服务器返回错误:(404) Not Found。
Dev/QA 和 UAT 的主要区别在于 UAT 使用基于 SSL/HTTPS 的 URL,而 Dev/QA 使用 HTTP。我引入了以下代码行来帮助我进一步进步:
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
AcceptAllCertifications 总是返回 true,但我仍然收到 404 错误。
我之前遇到此错误的人能够通过仅确保用于 HttpWebRequest 的 URI 在末尾没有斜杠来解决问题(请参阅:Simple HttpWebRequest over SSL (https) gives 404 Not Found under C#)但这并没有什么区别对我来说。
我现在已经尝试了这篇文章中的建议(参见:HttpWebResponse returns 404 error),我在页面上呈现异常。这绕过了黄色警告屏幕,并为我提供了更多信息,包括它试图从中获得响应的 URL。但是,当我将 URL 复制并粘贴到浏览器中时,它可以正常工作并在页面上呈现 HTML。因此,我很高兴在 GetResponse 调用中使用了正确的 URL。
有没有人知道是什么让我如此悲伤?如前所述,这似乎只是我使用 SSL 的 UAT 服务器上的问题。
这是我的帮助代码:
public static string GetHtmlValues()
{
var webConfigParentUrlValue = new Uri(ConfigurationManager.AppSettings["ParentUrl"]);
var destinationUrl = HttpContext.Current.Request.Url.AbsoluteUri;
var path = "DestinationController" + "/" + "DestinationAction" + "?destinationUrl=" + destinationUrl;
var redirect = new Uri(webConfigParentUrlValue, path).AbsoluteUri;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
var request = (HttpWebRequest)WebRequest.Create(redirect);
//Ensures that if the user has already signed in to the application,
// their authorisation is carried on through to this new request
AttachAuthorisedCookieIfExists(request);
HttpWebResponse result;
try
{
result = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
result = ex.Response as HttpWebResponse;
}
String responseString;
using (Stream stream = result.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
responseString = reader.ReadToEnd();
}
return responseString;
}
在页面上呈现的错误的更多详细信息:
【问题讨论】:
-
远程服务器返回错误:(404) Not Found.
-
感谢您的澄清,我完全错过了粗体字。
-
我找到了我的问题,但不确定解决方案。查看错误的图像,物理路径是错误的。尽管我的应用程序池/站点指向正确的物理路径,但 IIS 在尝试生成 HttWebRequest.GetResponse() 时却以错误的路径为目标。 D:\webroot\wwwroot\TestWebsite 不包含我在此应用程序中使用的任何代码。它恰好是绑定到 IIS 列表中第一个站点的物理路径。关于它为何尝试定位此站点/物理路径的任何想法?
-
嗨 Stu,如果您还没有发现问题。我相信这是因为您在 IIS 列表中的第一个站点中绑定了端口 443
标签: c# asp.net-mvc iis ssl web-config