Created
February 27, 2011 01:18
-
-
Save codler/845809 to your computer and use it in GitHub Desktop.
wget.exe
This file contains hidden or 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.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