Created
December 17, 2014 19:52
-
-
Save hanssens/0cf7f577205538d64651 to your computer and use it in GitHub Desktop.
WebHttpClient - Command Line tool for quickly creating a httprequest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace WebHttpClient | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if (!args.Any()) | |
{ | |
PrintHelp(); | |
return; | |
} | |
var stopwatch = Stopwatch.StartNew(); | |
try | |
{ | |
// first argument should be the url | |
var url = args.FirstOrDefault(); | |
if (url == null) throw new Exception("First argument should be a url"); | |
var content = string.Empty; | |
var client = new WebClient(); | |
Console.WriteLine("Opening read mode for: " + url); | |
using (var stream = client.OpenRead(url)) | |
{ | |
using (var reader = new StreamReader(stream)) | |
{ | |
Console.WriteLine("Reading output stream..."); | |
content = reader.ReadToEnd(); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(ex.Message); | |
} | |
finally | |
{ | |
Console.ResetColor(); | |
stopwatch.Stop(); | |
Console.WriteLine("Finished in {0} ms.", stopwatch.ElapsedMilliseconds); | |
} | |
} | |
static void PrintHelp() | |
{ | |
Console.WriteLine("WebHttpClient - a simple powertool by hanssens.com"); | |
Console.WriteLine(); | |
Console.WriteLine("Usage instructions:"); | |
Console.WriteLine("httpwebclient.exe <url>"); | |
Console.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment