0

Im a newbie in programming..I prepared a ConsoleApp in Visual Studio 2017 using the following code...

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

namespace HttpClientStatus
{
    class Program
    {
        static async Task Main(string[] args)
        {
                var client = new HttpClient();
                var result = await client.GetAsync("http://webcode.me");
                Console.WriteLine(result.StatusCode);


        }
    }
}

The Build ran successfully but after executing the exe file nothing seemed to show up on the screen

The Output at the console was supposed to be like this..

$ dotnet run
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My html page</title>
</head>
<body>

    <p>
        Today is a beautiful day. We go swimming and fishing.
    </p>

    <p>
         Hello there. How are you?
    </p>

</body>
</html>

Kindly help..

Regards...

CrazyFirewall
  • 33
  • 2
  • 7
  • Write `Console.Read();` in the last line. so that you will be able to see that is written. Otherwise you program is terminating after writing – Vivek Nuna Jun 15 '20 at 08:42
  • It's a console application, it has no screen. This code will write only the status code to the console. If you double-click the execute, the console will close before you have a chance to read the status code. If you open a console window and run your application, you'll see the status code. You can also redirect the application's output to a file if you want – Panagiotis Kanavos Jun 15 '20 at 08:43
  • 1
    Does this answer your question? [Why is the console window closing immediately once displayed my output?](https://stackoverflow.com/questions/8868338/why-is-the-console-window-closing-immediately-once-displayed-my-output) – d4zed Jun 15 '20 at 08:43
  • If you want to always pause before exiting add `Console.ReadKey()`, which will wait until you click on any key. *DON'T* use `Read` - that method [only returns when you hit `Enter`](https://learn.microsoft.com/en-us/dotnet/api/system.console.read?view=netcore-3.1#remarks) – Panagiotis Kanavos Jun 15 '20 at 08:46
  • It didnt seem to work, same behaviour.. I have added `Console.ReadKey()` but no result ..i also opened the command prompt and ran it from there but no result. – CrazyFirewall Jun 15 '20 at 09:20

1 Answers1

1

put Console.Read(); after this Console.WriteLine(result.StatusCode); if you don't do it, the code will run completely but won't show anything to you.

Ziba
  • 49
  • 1
  • 15
  • `Read` isn't used for just waiting because it [terminates only when Enter is pressed](https://learn.microsoft.com/en-us/dotnet/api/system.console.read?view=netcore-3.1#remarks). It's far more common to use `ReadKey` – Panagiotis Kanavos Jun 15 '20 at 08:46
  • It didnt seem to work, same behaviour.. I have added Console.ReadKey() but no result ...i tried with Command prompt also – CrazyFirewall Jun 15 '20 at 10:01