nqioweryuadfge
Member
- Joined
- Oct 13, 2016
- Messages
- 9
- Programming Experience
- 1-3
How do you print a Text file on a console application with formatting like below in HTTPClient in C# Language?
Here is how my textFile looks like:
Hi|Fine!
Here is all my code:
Here is how my textFile looks like:
Hi|Fine!
Here is all my code:
C#:
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static void Main()
{
Task t = new Task(DownloadPageAsync);
t.Start();
Console.WriteLine("Downloading page...");
Console.ReadLine();
}
static async void DownloadPageAsync()
{
// The file is located on the App_Data folder
string page = "/App_Data/Text1.Txt";
// ... Use HttpClient.
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(page))
using (HttpContent content = response.Content)
{
// ... Read the string.
string result = await content.ReadAsStringAsync();
// ... Display the result.
if (result != null &&
result.Length >= 50)
{
Console.WriteLine(result.Substring(0, 50) + "...");
}
}
}
}