zaczek.net
Home Events Fotos Projects Diplomarbeit   Zitate Download Feedback Lebenslauf
 
Projects  >>  Code
Links
WebDAV
using System;
using System.Net;
using System.IO;

namespace WebDAV
{
	class WebDAV
	{
		/// 
		/// The main entry point for the application.
		/// 
		[STAThread]
		static void Main(string[] args)
		{
			if(args.Length < 3)
			{
				ShowHelp();
				return;
			}

			switch(args[0].ToUpper())
			{
				case "GET":
					MakeGETRequest(args);
					break;
				case "PUT":
					MakePUTRequest(args);
					break;
			}
		}

		public static void ShowHelp()
		{
			Console.WriteLine("use: WebDAV {GET | PUT} {url} {filename} [username] [password] -login");
			Console.WriteLine("  GET - downloads a file");
			Console.WriteLine("  PUT - uploads a file");
			Console.WriteLine("  url - url of the file");
			Console.WriteLine("  filename - local filename");
			Console.WriteLine("  username - (optional) username for Basic Authentication");
			Console.WriteLine("  password - (optional) password for Basic Authentication");
			Console.WriteLine("  -login - (optional) use current login for authentication");
		}

		public static ICredentials GetCredentials(string[] args, int idx, string url)
		{
			if(args.Length >= idx)
			{	
				if(args[3].ToLower() == "-login")
				{
					return CredentialCache.DefaultCredentials;
				}
				else
				{
					string user = "";
					string domain = "";

					if(args[3].IndexOf(@"\") != -1)
					{
						string[] a = args[idx-1].Split('\\');
						user = a[1];
						domain = a[0];
					}
					else
					{
						user = args[idx-1];
					}

					string pwd = args.Length >= idx+1 ? args[idx] : "";

					NetworkCredential nc = null;
					if(domain != "")
					{
						nc = new NetworkCredential(user, pwd, domain);
					}
					else
					{
						nc = new NetworkCredential(user, pwd);
					}
					CredentialCache c = new CredentialCache();					
					c.Add(new Uri(url), "Basic", nc);
					return nc;
				}
			}
			return null;
		}

		public static void MakePUTRequest(string[] args)
		{
			try
			{
				FileInfo fi = new FileInfo(args[2]);

				string url = args[1];

				HttpWebRequest r = (HttpWebRequest)WebRequest.Create(url);
				r.Method = "PUT";
				r.Credentials = GetCredentials(args, 4, url);

				r.ContentType = "application/binary";
				try
				{
					r.ContentType = (string)Microsoft.Win32.
						Registry.ClassesRoot.OpenSubKey(fi.Extension).
						GetValue("Content Type", "application/binary");
				}
				catch(Exception e)
				{
					Console.WriteLine(e.Message);
				}
				r.ContentLength = fi.Length;

				Console.WriteLine("ContentType: " + r.ContentType);
				Console.WriteLine("ContentLength: " + r.ContentLength);

				using(FileStream f = fi.OpenRead())
				{			
					using(Stream s = r.GetRequestStream())
					{
						const int size = 65536;
						byte[] bytes = new byte[size];
						int numBytes;
						while((numBytes = f.Read(bytes, 0, size)) > 0)
						{
							s.Write(bytes, 0, numBytes);
						}
					}
				}

				Console.WriteLine("\nSending request...\n");
				WebResponse resp = null;
				try
				{
					resp = r.GetResponse();
					foreach(string h in resp.Headers.Keys)
					{
						Console.WriteLine("{0}: {1}", h, resp.Headers[h]);
					}
				}
				finally
				{
					if(resp != null) resp.Close();
				}
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
			}
		}

		public static void MakeGETRequest(string[] args)
		{
			try
			{
				string url = args[1];

				HttpWebRequest r = (HttpWebRequest)WebRequest.Create(url);
				r.Method = "GET";
				r.Credentials = GetCredentials(args, 4, url);

				Console.WriteLine("\nSending request...\n");
				WebResponse resp = null;
				try
				{
					resp = r.GetResponse();
					foreach(string h in resp.Headers.Keys)
					{
						Console.WriteLine("{0}: {1}", h, resp.Headers[h]);
					}
					using(FileStream f = File.OpenWrite(args[2]))
					{
						f.SetLength(0);
						using(Stream s = resp.GetResponseStream())
						{
							const int size = 65536;
							byte[] bytes = new byte[size];
							int numBytes;
							while((numBytes = s.Read(bytes, 0, size)) > 0)
							{
								f.Write(bytes, 0, numBytes);
							}
						}
					}
				}
				finally
				{
					if(resp != null) resp.Close();
				}
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
			}
		}
	}
}
zaczek.net 22.11.2008