/* 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 an HTTP URL and store the data in a file. # a i ca-certificates-mono */ using System; using System.IO; using System.Net.Http; using System.Threading; using System.Threading.Tasks; public class HttpDl { [STAThread] static public void Main( string[] args ) { HttpDl hd = new HttpDl(); hd.run( args ); } private Semaphore sem; private HttpDl() { sem = new Semaphore( 0, 1 ); } private void run( string[] args ) { if( args.Length == 2 ) { thread_run( args[0], args[1] ); sem.WaitOne(); } else { Console.WriteLine( "usage: httpdl url filename" ); } } private async void thread_run( string url, string filename ) { try { await http_download( url, filename ); } catch( HttpRequestException e ) { Console.WriteLine( "\nException Caught!" ); Console.WriteLine( "Message: {0}", e.Message ); } Console.WriteLine( "sem.Release" ); sem.Release(); } private async Task http_download( string url, string filename ) { using( HttpClient client = new HttpClient() ) { using( var file = await client.GetStreamAsync( url ).ConfigureAwait( false ) ) { using( var dst_stream = File.Create( filename ) ) { await file.CopyToAsync( dst_stream ); } } } } }