HttpWebRequest req;
HttpWebResponse myResp;
//判斷url對應的商品是否存在,存在返回真,不存在返回假
public bool getUrlTorF(string linkUrl, string errUrl, long leng, string neiRong)
{
bool justInfo = false;
req = (HttpWebRequest)WebRequest.Create(linkUrl);
req.Method = "GET";
req.AllowAutoRedirect = false;
try
{
myResp = (HttpWebResponse)req.GetResponse();
if (leng != 0)
{
//根據內容大小來判斷該商品是否有效
long content = myResp.ContentLength;
if (content < leng)
{
justInfo = true;
}
}
else
{
//根據跳轉頁面來判斷該商品是否有效
if (myResp.StatusCode == HttpStatusCode.Redirect)
{
string redirectUrl = myResp.GetResponseHeader("Location").ToLower();
if (redirectUrl == errUrl.ToLower())
{
justInfo = true;
}
}
//根據內容關鍵字來判斷該商品是否有效
else
{
Stream mystream = (Stream)req.GetResponse().GetResponseStream();
StreamReader mr = new StreamReader(mystream, Encoding.GetEncoding("gb2312"));
if (mr.ReadToEnd().IndexOf(neiRong) >= 0 && neiRong.Trim() != "")
{
justInfo = true;
//Console.WriteLine("找到");
}
else if (new StreamReader(mystream, Encoding.GetEncoding("utf-8")).ReadToEnd().IndexOf(neiRong) >= 0 && neiRong.Trim() != "")
{
justInfo = true;
}
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
justInfo = true;
// Console.WriteLine(linkUrl);
}
return justInfo;
}