Cloud Stack Ninja

I am just trying to figure out webscraping in C#. Right now I am trying to retrieve the user's external IP via HTTP GET API call. I have tried multiple different sites and I have had the same System.NullReferenceException for all of them. Here is my code:

using System;
using HtmlAgilityPack;

namespace Tracer
{
    class IPTracer
    {
        static void Main(string[] args)
        {
            scrape(@"http://api.ipify.org/");
        }

        static void scrape(string url)
        {
            HtmlWeb scraper = new HtmlWeb();
            HtmlDocument html = scraper.Load(url);

            string ip = html.DocumentNode.SelectSingleNode("/html/body/pre").InnerText;
            Console.WriteLine(ip);
        }
    }
}

This might be a very simple mistake as I am rather new to C# but this has been bugging me for days and I have yet to find a solution. And no, I checked and "html" itself is not null.



Read more here: https://stackoverflow.com/questions/64412117/why-is-selectsinglenode-causing-a-system-nullreferenceexception-for-every-site

Content Attribution

This content was originally published by Jackal at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: