嗨,我有一個程序,這就是看好如何跳過HttpWebResponse 404
for (int i=1; i < 1000; i++)
{
string id = i.ToString();
string url_source = get_url_source("http://mysite.com/"+id);
}
和方法
public static string get_url_source(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
return sr.ReadToEnd();
}
我應該如何改變我的方法,以便主程序會直接跳過該網址一些網頁並在404-未找到時繼續下一個ID?
我改變了我的循環這個
try
{
string url_source = get_url_source(url);
}
catch(WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
ConsoleBox.Text += i + ": " + WebExceptionStatus.ProtocolError.ToString() + "\r\n";
continue;
}
else
{
throw;
}
}
謝謝我遵循你的例子,現在罰款。 – Incognito