Skip to content

Instantly share code, notes, and snippets.

@codler
Created February 27, 2011 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codler/845809 to your computer and use it in GitHub Desktop.
Save codler/845809 to your computer and use it in GitHub Desktop.
wget.exe
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
public class wget
{
static void Main(string[] args)
{
string version = "1.0";
string user_agent = "Wget-windows/" + version;
string output_file = "output.txt";
string url = null;
bool debug = false;
// Abort if no arguments
if (args.Length == 0)
{
Console.WriteLine("Wget for Windows " + version + " Made by Han Lin Yap");
Console.WriteLine("Usage: wget.exe url");
Console.ReadLine();
return;
}
// Parse commandline arguments
Dictionary<string, string> flags = new Dictionary<string, string>();
string no_flag = null;
for (int key = 0; key < args.Length; key++)
{
if (args[key].StartsWith("/"))
{
flags.Add(args[key].Substring(1).ToLower(), args[++key]);
}
else
{
no_flag = args[key];
}
}
url = no_flag;
if (flags.ContainsKey("user-agent"))
{
user_agent = flags["user-agent"];
}
if (flags.ContainsKey("output"))
{
output_file = flags["output"];
}
if (flags.ContainsKey("debug"))
{
debug = Convert.ToBoolean(flags["debug"]);
}
WebClient c = new WebClient();
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(cert);
c.Headers.Add("User-Agent", user_agent);
c.DownloadFile(url, output_file);
Console.WriteLine("Finish: " + url);
Console.WriteLine("Output: " + output_file);
/*
byte[] response = c.DownloadData(args[0]);
Console.Write(Encoding.UTF8.GetString(response));
TextWriter tw = new StreamWriter(output_file);
tw.WriteLine(Encoding.UTF8.GetString(response));
tw.Close();*/
if (debug)
{
Console.ReadLine();
}
}
private static bool cert(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment