none
Print text file contents on a console using HTTPClient RRS feed

  • Question

  • 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:

    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) + "...");
    	    }
    	}
        }
    }

     

    I don't have a website, but I am developing it with Angular.js, and Bootstrap! Am also learning: Nodejs, and MVC


    Thursday, October 13, 2016 7:51 PM

All replies

  • Hi Common,

    Do you want to get content from text file which is in your console application? If so, I think there is no need to use HttpClient, you could try below code:

                string uri = AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\Test.txt";
                string result   = File.ReadAllText(uri,Encoding.Default);
                Console.WriteLine(result);

    HttpClient is used to retrieve from http or https url. If you want to retrieve content, I think you could try WebClient.

                Uri uri = new Uri(AppDomain.CurrentDomain.BaseDirectory+@"\App_Data\Test.txt");
                WebClient client = new WebClient();
                string result = System.Text.Encoding.Default.GetString(client.DownloadData(uri));
                Console.WriteLine(result);

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, October 14, 2016 5:35 AM
  • Edward Z

    Thanks for your time, en support!

    I was maybe planning on converting this code or maybe creating a link to be able to convert it into HTTPClient! How do you do this or what changes need to be done?


    I don't have a website, but I am developing it with Angular.js, and Bootstrap! Am also learning: Nodejs, and MVC

    Friday, October 14, 2016 12:33 PM
  • Edward Z

    So, what changes need to be done? I was planning on hosting my file on the web for my client applications to make calls through a username, and password! Is that possible to make these sought of modifications on your app? I tested the Webclient and added the following, but it keeps on saying:

    An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
    
    Additional information: Could not find a part of the path 'c:\users\acer\documents\visual studio 2012\Projects\ConsoleDownloadafile\ConsoleDownloadafile\bin\Debug\App_Data\Text1.txt'.


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



    Friday, October 14, 2016 12:37 PM
  • Hi Common,

    >> I was planning on hosting my file on the web for my client applications to make calls through a username, and password

    How did you host your text file on the web? Did you create an mvc web site, and want to access the content of text file which is locate in App_Data? In my option, App_Data folder is used as data-storage web application, it is a hiddenSegment section. I would suggest you create a new folder which named Data.

    Could you access your text file from Web browser? If you could, then you could try below code with the URL in your web browser.

    Uri uri = new Uri(@"http://localhost/WCFRESTJson/Data/TextFile1.txt");
    WebClient client = new WebClient();
    string result = System.Text.Encoding.Default.GetString(client.DownloadData(uri));
    Console.WriteLine(result);

    Based on your description, it seems you also want to add username and password. If so, I would suggest you access file content from MVC method instead of access the file directly. Because, we could control the request method by username and password, but for static file, it would not.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, October 17, 2016 2:21 AM
  • EdwardZ

    Can I send you the attachment of the file that I am actually having trouble with? Do you have a weblink that I can contact you with?



    Wednesday, October 19, 2016 9:54 AM
  • Hi Common,

    I could not think out your issue is related with this text file. I think it is just a simple content. What error you got?

    I think you could share us your project through OneDrive and share us link here. A detailed steps to reproduce your issue would be helpful.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 19, 2016 10:49 AM
  • Edward Z

    There is a developer I hired to build for me a webAPI, but when I test the files he sends, they don't work! He seems to me like someone I paid to do a job, who high-jacked my project for what I paid for, and at the same time, he refused to give me the complete files of the project! So, I paid him 150 dollars, and out of it, I got nothing! He gets to keep the real files, and the money, while I end up with nothing!  He cheated me that the files were hosted on TeamFoundation only to later find out that the files or the actual WebAPI was hosted on Azure on his free account! The files he gives me don't even have the solution files or the setup! The files he gives me are the Global.asax, packages.config, Web.config files, and a bunch of bin, and App_Data folders. There is no computational or the files to do the checks of who is making the calls or anything! I also want to send you the files privately where nobody else will receive these files! This is my project I paid for, and I don't want anyone getting them for free! Do you have a weblink or website or place I can send you? I really can't send them here trust me!


    I don't have a website, but I am developing it with Angular.js, and Bootstrap! Am also learning: Nodejs, and MVC

    Thursday, October 20, 2016 7:05 AM
  • Hi,

    You can visit on this link.

    http://www.c-sharpcorner.com/article/printing-text-file-in-C-Sharp/

    Thursday, October 20, 2016 12:51 PM
  • Hi Common,

    For reading file content from console application to Web API project, I share a simple project to you.

    Reference:https://1drv.ms/u/s!AsyeWQ2z51P3hAW0xe7_wf9isOnx

    For getting started with Web api, I suggest you refer the link below:

    #Getting Started with ASP.NET Web API 2 (C#)
    https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    Hope it will help.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, October 21, 2016 7:26 AM
  • Edward Z

    Can you check my attached DictionaryAPI zip file please?


    I don't have a website, but I am developing it with Angular.js, and Bootstrap! Am also learning: Nodejs, and MVC

    Monday, October 24, 2016 12:09 PM
  • Hi Common,

    Have you checked my project? Did you have any issue about it?

    I did not see your zip file.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 24, 2016 12:21 PM
  • Hi Common,

    This is forum support, and we discuss through forum channel. I did not receive and could not receive any email due to security policy.

    Do you have any trouble about my project? For your issue, I would suggest you check below link to learn get started Web api, and then check my project whether it meets your requirement.

    #Getting Started with ASP.NET Web API 2 (C#)
    https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 25, 2016 8:49 AM