此代码必须根据页码查找有效页面。这应该实现逻辑 - 如果具有地址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();
}
答案 0 :(得分:1)
试试这个
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";
}
答案 1 :(得分:0)
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;
答案 2 :(得分:0)
好吧,如果有人提供更好的解决方案,我会很高兴,但现在这个解决了:
catch (WebException ex)
{
var resp = (HttpWebResponse) ex.Response;
if (resp.StatusCode == HttpStatusCode.NotFound)
break;
}
我不喜欢这个解决方案,因为它并不美丽。所以如果有人有更好的想法 - 欢迎你。