Jump to content

C# get text from a URL

Go to solution Solved by savstars,

Let me know if it helps.  There are other ways, such as;

 

    internal class WebTransfer : IWebTransfer, IDisposable
    {
        private WebClient _client;

        public WebTransfer()
        {
            _client = new WebClient();
        }

        public string DownloadFileAsString(string url)
        {
            return _client.DownloadString(new Uri(url));
        }

        public void Dispose()
        {
            if (_client != null)
            {
                _client.Dispose();
                _client = null;
            }
        }
    }

or perhaps a more simplified version 

string fileContents;

using (WebClient client = new WebClient())
{
    fileContents = client.DownloadString(new Uri(@"http://localhost/directory/file"));
}

 

So, I need to access some text in a textfile and I need to access it via a URL.

 

How can I get whatever is in the textfile in C#?

 

Thanks much :).

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
https://linustechtips.com/topic/655480-c-get-text-from-a-url/
Share on other sites

Link to post
Share on other sites

Are you just trying to get the contents of a file using HTTP?  if so you could probably do something like this...

 

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:9000/");

    HttpResponseMessage response = await client.GetAsync("directory/filename.txt");
    if (response.IsSuccessStatusCode)
    {
        string fileContents = await response.Content.ReadAsAsync<string>();
        Console.WriteLine(fileContents);
    }
}

 

Link to comment
https://linustechtips.com/topic/655480-c-get-text-from-a-url/#findComment-8435277
Share on other sites

Link to post
Share on other sites

8 minutes ago, savstars said:

Are you just trying to get the contents of a file using HTTP?  if so you could probably do something like this...

 


using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:9000/");

    HttpResponseMessage response = await client.GetAsync("directory/filename.txt");
    if (response.IsSuccessStatusCode)
    {
        string fileContents = await response.Content.ReadAsAsync<string>();
        Console.WriteLine(fileContents);
    }
}

 

I'm getting an error telling me this "HttpContent does not contain a definition for ReadAsAsync and no extension method ReadAsAsync accepting a first argument of HttpContent could be found".

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
https://linustechtips.com/topic/655480-c-get-text-from-a-url/#findComment-8435583
Share on other sites

Link to post
Share on other sites

Opps sorry about that.  I just did it from memory.  The offending line should be 

 

string fileContents = await response.Content.ReadAsStringAsync();

https://msdn.microsoft.com/en-us/library/system.net.http.httpcontent.readasstringasync(v=vs.118).aspx

Link to comment
https://linustechtips.com/topic/655480-c-get-text-from-a-url/#findComment-8435753
Share on other sites

Link to post
Share on other sites

Let me know if it helps.  There are other ways, such as;

 

    internal class WebTransfer : IWebTransfer, IDisposable
    {
        private WebClient _client;

        public WebTransfer()
        {
            _client = new WebClient();
        }

        public string DownloadFileAsString(string url)
        {
            return _client.DownloadString(new Uri(url));
        }

        public void Dispose()
        {
            if (_client != null)
            {
                _client.Dispose();
                _client = null;
            }
        }
    }

or perhaps a more simplified version 

string fileContents;

using (WebClient client = new WebClient())
{
    fileContents = client.DownloadString(new Uri(@"http://localhost/directory/file"));
}

 

Link to comment
https://linustechtips.com/topic/655480-c-get-text-from-a-url/#findComment-8435999
Share on other sites

Link to post
Share on other sites

42 minutes ago, savstars said:

Let me know if it helps.  There are other ways, such as;

 


    internal class WebTransfer : IWebTransfer, IDisposable
    {
        private WebClient _client;

        public WebTransfer()
        {
            _client = new WebClient();
        }

        public string DownloadFileAsString(string url)
        {
            return _client.DownloadString(new Uri(url));
        }

        public void Dispose()
        {
            if (_client != null)
            {
                _client.Dispose();
                _client = null;
            }
        }
    }

or perhaps a more simplified version 


string fileContents;

using (WebClient client = new WebClient())
{
    fileContents = client.DownloadString(new Uri(@"http://localhost/directory/file"));
}

 

But will this return the HTML on the webpage? Or the actual Text?

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
https://linustechtips.com/topic/655480-c-get-text-from-a-url/#findComment-8437014
Share on other sites

Link to post
Share on other sites

8 minutes ago, savstars said:

To clarify, are you trying to retrieve the visible text on a web page or just the contents of a text file that is accessible via HTTP?

Text on a Text file that is accessible via HTTP.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
https://linustechtips.com/topic/655480-c-get-text-from-a-url/#findComment-8437497
Share on other sites

Link to post
Share on other sites

37 minutes ago, savstars said:

Then the answer is that it will get the contents of the file.

Thanks for that. After that I get a completely unrelated issue when I try to use that information.

 

It says that the filename or extension is too long.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
https://linustechtips.com/topic/655480-c-get-text-from-a-url/#findComment-8440371
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×