我有一個網絡服務,我通過PDA與之交談。在與web服務asmx文件相同的目錄中,我有一個html文件,可以從中提取web請求,以查看目錄是否可用。精簡版框架 - WebRequest有時無故失敗?
我使用下面的代碼:
/// <summary>
/// Holds the web request for checking the connectivity.
/// </summary>
private static WebRequest m_WebRequest;
/// <summary>
/// Tests the connection to the provided URL.
/// </summary>
/// <param name="url">The URL to test.</param>
/// <returns>True if the URL was resolved.</returns>
public static bool TestUrl(string url)
{
try
{
// Ensure the url is valid
url = url.Replace("http:\\", "http://");
url = url.Replace("\\", "/");
// Create the request
m_WebRequest = WebRequest.Create(url);
m_WebRequest.Timeout = 30000;
// Get the request
HttpWebResponse response = (HttpWebResponse)m_WebRequest.GetResponse();
return true;
}
catch
{
m_WebRequest.Abort();
return false;
}
}
文件我得到的是永遠不變的,總是從我的電腦訪問的響應。它似乎只在web服務(與html文件位於同一目錄中)錯誤時失敗。這可能是相關的嗎?
或者,有沒有更好的方式來查看該文件是否存在於網絡上?
你會得到什麼錯誤? – 2009-01-30 11:04:52