htmlAgilityPack Load() method does nothing

c# html-agility-pack winforms

Question

I'm using htmlAgilityPack to get the content of a website:

private String getImageUrl(String websiteUrl)
{
    HtmlAgilityPack.HtmlDocument docHtml = new HtmlWeb().Load(websiteUrl);
    // ...
}

I don't reach the breakpoint at the second line, the first line does not throw an exception. The URL is existing and available.

docHtml is also not null, the line just doesn't seem to execute, noly kill my thread.

What can make this happen / how can I get more info about what's happening?

edit: The function is called from a class, which is instantiated by my main-form. the calling class runs a thread. For the first instance of my class, it is working, but not for the second instance.

class Image
{
    BackgroundWorker downloadWorker = new BackgroundWorker();

    private String fileName;
    private String directory;
    private String url;

    RichTextBox rtxtStatus;


    public Image(String _fileName, String _directory, String _url)
    {
        fileName = _fileName;
        directory = _directory;
        url = _url;

        downloadWorker.WorkerReportsProgress = true;

        downloadWorker.WorkerSupportsCancellation = true;

        downloadWorker.DoWork += new DoWorkEventHandler(worker_doWork);
        downloadWorker.ProgressChanged += new ProgressChangedEventHandler(worker_progressChanged);
        downloadWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_runWorkerCompleted);
    }

    private void worker_doWork(object sender, DoWorkEventArgs e)
    {
        download();
    }

    private void download()
    {
        WebClient downloadClient = new WebClient();

        if (!Directory.Exists(directory))
        {
            MessageBox.Show("Directory to save image not found.");
        }
        else
        {
            HttpWebRequest HttpReq = (HttpWebRequest)WebRequest.Create(url);

            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)HttpReq.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }

            if (response.StatusCode == HttpStatusCode.OK)
            {
                string image = getImageUrl(url);

                // Replace HTML-Characters
                image = WebUtility.HtmlDecode(image);
                string saveName = directory + @"\" + fileName + ".png";
            }
            try
            {
                downloadClient.DownloadFile(image, saveName);
            }
            catch (Exception)
            {
                MessageBox.Show("Error while downloading");
            }                                       
        }
    }

    private void worker_progressChanged(object sender, ProgressChangedEventArgs e)
    {
        // Nothing to do
    }

    private void worker_runWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show("Download cancelled, please check URL.");
        }
        rtxtStatus.AppendText("\nDownload finished.");
    }



    Properties.Settings.Default.SaveFileLocation = directory;
    Properties.Settings.Default.Save();
}

// this method is called from outside on image-object.
public void downloadImage(RichTextBox _rtxtStatus)
{
    rtxtStatus = _rtxtStatus;
    if (!downloadWorker.IsBusy)
    {
        downloadWorker.RunWorkerAsync();
    }
    else
    {
        MessageBox.Show("Download already running.");
    }
}
private String getImageUrl(String websiteUrl)
{
    HtmlAgilityPack.HtmlDocument docHtml = new HtmlWeb().Load(websiteUrl);
    var nodes = docHtml.DocumentNode.SelectNodes("//img");
    return nodes[0].Attributes["src"].Value;
}

Popular Answer

Maybe the site you are trying to reach requires cookies enabled. Add a CookieContainer to the HtmlWeb object and try Load method.

HtmlWeb htmlWeb = new HtmlWeb();
htmlWeb.PreRequest += request =>
    {
        request.CookieContainer = new System.Net.CookieContainer();
        return true;
    };
var htmlDoc = htmlWeb.Load(yourUrl);



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why