![](/img/trans.png)
[英]HttpWebRequest.GetResponse() returning 404: Not Found for a valid URL
[英]HttpWebRequest.GetResponse() returning 404 Error
我有一些代碼調用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末尾沒有斜杠來解決此問題(請參閱: SSL上的簡單HttpWebRequest(https)提供了C#下的404 Not Found )。但這對我沒有影響。
現在,我嘗試了本文中建議的內容(請參閱: HttpWebResponse返回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;
}
該頁面上呈現的錯誤的更多詳細信息:
我遇到了類似的情況,但是有不同的錯誤消息。 我的問題原來是我的UAT環境是帶有.NET 4.5的Windows 2008。 在這種環境下,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
}
}
我終於找到了解決我問題的方法...
使我走上正確軌道的第一個線索是IIS出現404錯誤時顯示了錯誤的物理路徑。 原來,此錯誤的物理路徑已映射到我的IIS安裝程序中的另一個站點。 這個特殊的事物自然也具有約束力。 端口443。您可能知道,端口443是https的默認端口。
現在查看我試圖傳遞給HTTPWebRequest.GetResponse()
方法的URL,它看起來像這樣:
https://www.my-web-site.com
考慮到這一點,當將此應用程序托管在SSL范圍內的IIS上時,將發生以下錯誤:
GetHtmlValues()
https://www.my-web-site.com
https://www.my-web-site.com
答復 https://www.my-web-site.com:443
響應。 現在為解決方案...
https://www.my-web-site.com
的值,該值將更改為http://www.my-web-site.com:16523
請注意, https
如何變成http
並在末尾指定端口號。 現在,當應用程序嘗試獲取響應時,它不再使用默認的ssl端口,因為已指定了正確的端口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.