有没有办法让代码即使超时或 404 错误仍然继续...... HttpWebRequest 和 HttpWebResponse 案例:
这是我的代码:
string argumentTest = textBox1.text.Trim();
string[] lines = textBox1.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
string newLine = ((char)13).ToString() + ((char)10).ToString();
foreach (string vr in lines)
{
string sourceCode = getSourceCode(vr);
if (sourceCode != null)
{
//if this code is up
} else {
//if this code is down
}
}
public static string getSourceCode(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ContentType = "text/html; charset=ISO-8859-15";
//ERROR in here 404 or timeout
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string sourceCode = sr.ReadToEnd();
sr.Close();
resp.Close();
return sourceCode;
}