/* ANY-LICENSE V1.0 ---------------- You can use these files under any license approved by the Open Source Initiative, preferrably one of the popular licenses, as long as the license you choose is compatible to the dependencies of these files. See http://www.opensource.org/licenses/ for a list of approved licenses. Author: Martin Furter Project: Modified: 2019 Download the content from HTTP URLs and print the data on the console. # a i ca-certificates-mono */ using System; using System.IO; using System.Net.Http; using System.Threading; using System.Threading.Tasks; public class HttpGet { [STAThread] static public void Main( string[] args ) { HttpGet hg = new HttpGet(); hg.run( args ); } private Semaphore sem; private HttpGet() { sem = new Semaphore( 0, 1 ); } private void run( string[] urls ) { thread_run( urls ); sem.WaitOne(); } private async void thread_run( string[] urls ) { foreach( string url in urls ) { try { string responseBody = await http_get( url ); Console.WriteLine( "{0}", responseBody ); } catch( HttpRequestException e ) { Console.WriteLine( "\nException Caught!" ); Console.WriteLine( "Message: {0}", e.Message ); } } Console.WriteLine( "sem.Release" ); sem.Release(); } private async Task http_get( string url ) { Console.WriteLine( "URL: {0}", url ); using( HttpClient client = new HttpClient() ) { Console.WriteLine( "GetAsync" ); HttpResponseMessage response = await client.GetAsync( url ); Console.WriteLine( "EnsureSuccessStatusCode" ); response.EnsureSuccessStatusCode(); Console.WriteLine( "ReadAsStringAsync" ); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine( "" ); // Above three lines can be replaced with // new helper method below // string responseBody = await client.GetStringAsync( url ); return responseBody; } } }