Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read data from a website. but after giving some output it gives me error
"Unable to read data from the transport connection: The connection was closed."
the code i have written is this :
C#
private void GetData(string url)
    {
        string readfile = string.Empty;
        lbl.Text = string.Empty;
        HtmlWeb web = new HtmlWeb();
        
        HtmlDocument myNode = web.Load(url); // Error part
        myNode.DocumentNode.Descendants().Where(n => n.Name == "script" && n.Name == "style" && n.Name == "link").ToList().ForEach(n => n.Remove());
        myNode.DocumentNode.Descendants().Where(n => n.Name == "href" && n.Name == "img").ToList().ForEach(n => n.Remove());
        foreach (var elem in elementHtml)
        {
            HtmlNode node = myNode.DocumentNode.SelectSingleNode(".//ul[@class='" + elem + "']");
            if (node != null)
            {
                readfile = node.InnerText.ToString().Trim();
                string[] str = readfile.Split(new string[] { "Add to Cart", "or Add to Wish List", "» Add to Compare" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string val in str)
                {
                    DataRow dr = ProdList.NewRow();
                    dr["Desc"] = val.ToString().Trim().Replace(" ", "");
                    ProdList.Rows.Add(dr);
                    SaveData();
                    //WriteTxt();
                }
            }
        }
    }


i still looking for the solution ...

pls help..
Thank You
Posted
Comments
virusstorm 13-Aug-13 15:57pm    
Try to load the file async

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900