我有一些代码调用HttpWebRequest的GetResponse()方法,从URL中检索HTML并将其返回给调用方法。
这在我的开发环境和QA环境中运行得很好,但是现在我已经将它上传到我的UAT服务器上了,我一直收到以下错误:
远程服务器返回一个错误:(404)未找到.
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;
}
在页面上呈现错误的更多详细信息:
发布于 2015-02-08 17:54:15
我遇到了类似的情况,但错误信息不同。我的问题是,我的UAT环境是Windows2008和.NET 4.5。在这种环境中,SSL握手/检测的执行方式与大多数web浏览器不同。因此,我在web浏览器中看到URL呈现没有错误,但是我的应用程序会生成一个错误。我的错误消息包括“基础连接已关闭:发送时发生意外错误”。这可能是你的问题。
我的解决办法是强行改变协议。我检测到特定的错误,然后强制更改我的应用程序的安全协议,然后再试一次。
这是我使用的代码:
catch (Exception ex)
{
if(ex.Message.Contains("The underlying connection was closed: An unexpected error occurred on a send."))
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
// retry the retrieval
}
}
https://stackoverflow.com/questions/28396989
复制相似问题