skip to Main Content

I’ve just tried switching over to Linux for the first time in my life and I am struggling a bit with my C# applications there.

I am making an app that is scraping a website every 60 seconds, saves the entire html code in a variable called “original” and then compares it after each run. If there’s been any changes in the html code of the website, it is sending a message to my Telegram chat saying “Hello world”.

I’ve erased my credentials and stuff, but this is what the code looks like. Since HttpClient does not exist in .NET Core 2.2 (which is the only I’ve been able to install on Linux so far (Ubuntu, I’m on AWS EC2 and remote desktop with XFCE). I am completely new to Linux too. I did see .NET Core 3 is out, but I don’t seem to be able to install it (am I doing something wrong?). And I don’t know if HttpClient is included there either.

Anyway; any way to substitute HttpClient with something else to send my PostAsync to Telegram’s API?

using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using System.Collections.Generic;

namespace MyApp
{
    class Program
    {
        private static readonly HttpClient httpclient = new HttpClient();
        private static readonly WebClient client = new WebClient();
        public static string original = "";
        static void Main(string[] args)
        {
            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(60000);
                    string result = client.DownloadString("https://website.com");
                    if (original != result && original != "")
                    {
                        Dictionary<string, string> inputData = new Dictionary<string, string>
                        {
                            { "chat_id", "x" },
                            { "text", "Hello world" }
                        };

                        var request = await httpclient.PostAsync("https://api.telegram.org/botxxxx/sendMessage", new FormUrlEncodedContent(inputData));
                        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Apartment listings were updated.");
                    }
                    else
                    {
                        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " No change in apartment listings.");
                    }

                    original = result;

                }
            });

            Console.Read();
        }
    }
}

Error message:

Program.cs(5,18): error CS0234: The type or namespace name `Http' does not exist in the namespace `System.Net'. Are you missing `System.Net.Http' assembly reference?
Program.cs(21,33): error CS0246: The type or namespace name `HttpClient' could not be found. Are you missing an assembly reference?
Compilation failed: 2 error(s), 0 warnings

I just compile with mcs Program.cs and then run it with mono Program.exe

EDIT:

Solution: I did not have to build it. I could simply run it (without doing ANY changes to above code) with the command: dotnet run

Works just fine!

2

Answers


  1. Httpclient does exist in .net core, but I believe you may need to download a nuget package.

    Microsoft.AspNetCore.Http in particular.

    Login or Signup to reply.
  2. As TheYellowSquares mentioned, HttpClient does exist in .Net Core 2.2 (you can see it here for example https://github.com/dotnet/corefx/blob/1284396e317e5e7146135b0ba5088741705122e6/src/System.Net.Http/src/System/Net/Http/HttpClient.cs) The only thing is that you don’t need to add any specific package.

    The code snippet that you posted should compile without errors on Windows and on Linux. Just use namespace System.Net.Http (that is already in your code snippet) that’s enough.

    — Update

    use dotnet build to build the project (check that you in the project directory)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search