Skip to content

Instantly share code, notes, and snippets.

@hanssens
Created December 17, 2014 19:52
Show Gist options
  • Save hanssens/0cf7f577205538d64651 to your computer and use it in GitHub Desktop.
Save hanssens/0cf7f577205538d64651 to your computer and use it in GitHub Desktop.
WebHttpClient - Command Line tool for quickly creating a httprequest
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