Wgetheader.cs

From AdonaiMedrado.Pro.Br
Revision as of 22:06, 19 May 2009 by Adonaimedrado (Talk | contribs) (New page: <code lang="csharp"> using System; using System.IO; using System.Net; namespace wgetheader { class Program { public static void Main(string[] args) { if (args.Length < 2...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
using System;
using System.IO;
using System.Net;
 
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);
			}
			catch(Exception ex)
			{
				Console.Error.WriteLine(ex.Message);
			}
		}
	}
}