C#无效的URI:URI为空

时间:2015-07-10 08:33:30

标签: c# httpwebrequest

您好,我试图从网站上收集所有数据。

一开始我收集该网站上的所有链接。之后访问它们。 问题是,当我访问多个网站页面时,"无效的URI:URI为空"某些页面中显示错误。这些是一些失败的网址。

http://syayoyu.com/category/zensinyokudiet
http://syayoyu.com/fruitgranola-6759.html
http://syayoyu.com/category/diet/fruitgranola

从浏览器访问并逐个访问它们正常工作。但是当我尝试在循环中访问时,错误正在发生。你能告诉我它有什么问题吗?

这是我的访问代码

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.UserAgent = "A .NET Web Crawler";
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string htmlText = reader.ReadToEnd();

1 个答案:

答案 0 :(得分:2)

List<string> list = new List<string>(){
@"http://syayoyu.com/category/zensinyokudiet",
@"http://syayoyu.com/fruitgranola-6759.html",
@"http://syayoyu.com/category/diet/fruitgranola"
};
foreach (var url in list)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.UserAgent = "A .NET Web Crawler";
    WebResponse response = request.GetResponse();
    //Stream stream = response.GetResponseStream();
    //StreamReader reader = new StreamReader(stream);
    //string htmlText = reader.ReadToEnd();
    //Console.WriteLine(htmlText);
    using (Stream stream = response.GetResponseStream())
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            string htmlText = reader.ReadToEnd();
            Console.WriteLine(htmlText);
        }
    }
}

在我的解决方案中,它运行正常,那么您有什么问题吗?