使用HttpWebRequest和HttpWebResponse会引发错误,但仅限于某些网站

时间:2015-08-03 19:40:55

标签: c# httpwebrequest

此功能检查url是否为有效地址,并使用http://www.google.com

public static bool CheckValidURL(string url)
{
    try
    {
        //Creating the HttpWebRequest
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        //Setting the Request method HEAD, you can also use GET too.
        request.Method = "GET";
        //Getting the Web Response.
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        //Returns TRUE if the Status code == 200
        bool isValid;
        if (response.StatusCode == HttpStatusCode.OK)
        {
            isValid = true;
        }
        else
        {
            isValid = false;
        }
        response.Close();
        return isValid;
    }
    catch
    {
        //Any exception will returns false.
        return false;
    }
}

单击菜单项时会调用此代码,并且该代码用于更新卡片价格。它位于foreach循环内,变量c属于自定义类MagicCard

string url = "";
string webCardName = c.name.ToLower().Replace(" ", "-").Replace(",", "").Replace("\'", "");
string webSetName = MagicCard.GetSetName(c).ToLower().Replace(" ", "-").Replace(",", "").Replace("\'", "");
url = string.Format("shop.tcgplayer.com/magic/{0}/{1}", webSetName, webCardName);
if (WebScraper.CheckValidURL(url) == false)
{
    MessageBox.Show("ERROR: URL = " + url);
    return;
}

最终网址的示例是this虽然这是一个有效的地址,但是它被检测为没有,并且任何其他卡都会产生类似的网址,这些网址也不起作用。为什么会这样?

1 个答案:

答案 0 :(得分:0)

您的网站需要User-Agent。我添加了这一行

request.UserAgent = "SO/1.0";

你的代码,它工作....