Wgetheader.cs

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
using System;
using System.IO;
using System.Net;
using System.Data;
 
namespace wgetheader
{
	class Program
	{
	    public static void Main(string[] args)
	    {
                        if (args.Length < 2)
                        {
                                Console.WriteLine("Usage: wgethead url file"); 
                                return;
                        }
 
			try
			{
				WebClient wc = new WebClient();
				string conteudo = wc.DownloadString(args[0]);
				FileStream f = File.OpenWrite(args[1]);
				byte[] buf;
 
				foreach(string s in wc.ResponseHeaders.AllKeys)
				{
					buf = System.Text.ASCIIEncoding.Default.GetBytes(
						string.Format(">> {0} <<\n{1}\n{2}\n",
					                  s,
					                  wc.ResponseHeaders[s],
					                  new string('-',10)));
					f.Write(buf,0,buf.Length);
				}
				buf = System.Text.ASCIIEncoding.Default.GetBytes(conteudo);
				f.Write(buf,0,buf.Length);
				f.Close();
				f.Dispose();
			}
			catch(Exception ex)
			{
				Console.Error.WriteLine(ex.Message);
			}
		}
	}
}