在 if 语句中处理 HttpStatusCode,而不是 try/catch
本文关键字:try catch 语句 if 处理 HttpStatusCode | 更新日期: 2023-09-27 18:34:03
此代码必须根据页码查找有效页面。这应该实现逻辑 - 如果带有地址http://hostname/page/x
的页面返回了 OK 状态,则 url 有效,如果响应状态为 NotFound,则 url 无效。我想在 if 语句中处理 HttpStatusCode,但它总是通过 try/catch 块捕获。
public void GetAllLinks()
{
const string baselink = "http://buzzon.khaleejtimes.com/ad-category/real-estate/page/";
//Check if OK status answer from server, page link is valid
for (int i = 1; i < 10000; i++)
{
var url = baselink + i;
if (LinkExist(url) != true)
{
try {
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for a response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.NotFound)
{
//Exit loop if status 404.
break;
}
WriteUrl(url);
myHttpWebResponse.Close();
}
catch (Exception ex) {
throw new Exception(ex.Message);
}
}
ParsePages();
}
试试这个
public string GetAllLinks()
{
const string baselink = "http://buzzon.khaleejtimes.com/ad-category/real-estate/page/";
//Check if OK status answer from server, page link is valid
for (int i = 1; i < 10000; i++)
{
var url = baselink + i;
if (LinkExist(url) != true)
{
try
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for a response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (HttpStatusCode.OK == response.StatusCode)
{
WriteUrl(url);
myHttpWebResponse.Close();
}
else
break;
}
}
catch (Exception wex)
{
string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
return pageContent;
}
}
ParsePages();
return "Response Established";
}
try adding a null check ..
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
// Sends the HttpWebRequest and waits for a response.
var temp = myHttpWebRequest.GetResponse();
if (temp == null)
{
//break code
}
HttpWebResponse myHttpWebResponse = (HttpWebResponse) temp;
好吧,如果有人提供更好的解决方案,我会很高兴,但现在这个解决方案正在工作:
catch (WebException ex)
{
var resp = (HttpWebResponse) ex.Response;
if (resp.StatusCode == HttpStatusCode.NotFound)
break;
}
我不喜欢这个解决方案,因为它并不美好。因此,如果有人有更好的主意 - 不客气。