Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for wgetlite.cs

(view source code of wgetlite.cs as plain text)

  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class WGetLite
  10. 	{
  11. 		static string progver = "1.01";
  12.  
  13. 		private static ManualResetEvent allDone = new ManualResetEvent( false );
  14.  
  15.  
  16. 		static int Main( string[] args )
  17. 		{
  18. 			string url = string.Empty;
  19. 			string enc = "ASCII";
  20. 			string outputFile = string.Empty;
  21. 			int statusCode = 0;
  22. 			bool save = false;
  23. 			bool overwrite = false;
  24. 			bool show = true;
  25.  
  26. 			#region Command Line Parsing
  27.  
  28. 			// Custom error message
  29. 			string msgInvalid = "Invalid command line argument" + ( ( args.Length == 1 ) ? string.Empty : "(s)" );
  30.  
  31. 			// No command line arguments? Display help
  32. 			if ( args.Length == 0 )
  33. 			{
  34. 				return ShowHelp( string.Empty );
  35. 			}
  36.  
  37. 			foreach ( string arg in args )
  38. 			{
  39. 				// Check switches first
  40. 				switch ( arg.ToLower( ).Substring( 0, 2 ) )
  41. 				{
  42. 					case "/?":
  43. 					case "-?":
  44. 					case "/h":
  45. 					case "-h":
  46. 						// Display help
  47. 						return ShowHelp( string.Empty );
  48. 					case "--":
  49. 						if ( arg.ToLower( ) == "--help" )
  50. 						{
  51. 							// Display help
  52. 							return ShowHelp( string.Empty );
  53. 						}
  54. 						else
  55. 						{
  56. 							// Display error message
  57. 							return ShowHelp( msgInvalid );
  58. 						}
  59. 					case "/e":
  60. 						if ( arg.ToLower( ).StartsWith( "/e:" ) || arg.ToLower( ).StartsWith( "/encoding:" ) )
  61. 						{
  62. 							enc = arg.Substring( arg.IndexOf( ":" ) + 1 );
  63. 						}
  64. 						else
  65. 						{
  66. 							return ShowHelp( msgInvalid );
  67. 						}
  68. 						break;
  69. 					case "/o":
  70. 						overwrite = true;
  71. 						break;
  72. 					case "/r":
  73. 						show = false;
  74. 						break;
  75. 					case "/s":
  76. 						show = false;
  77. 						save = true;
  78. 						if ( arg.ToLower( ).StartsWith( "/s:" ) || arg.ToLower( ).StartsWith( "/save:" ) )
  79. 						{
  80. 							outputFile = arg.Substring( arg.IndexOf( ":" ) + 1 );
  81. 						}
  82. 						else
  83. 						{
  84. 							return ShowHelp( msgInvalid );
  85. 						}
  86. 						break;
  87. 					default:
  88. 						url = arg;
  89. 						break;
  90. 				}
  91. 			}
  92.  
  93. 			if ( string.IsNullOrEmpty( url ) )
  94. 			{
  95. 				return ShowHelp( "A URL must be specified" );
  96. 			}
  97.  
  98. 			if ( overwrite && !save )
  99. 			{
  100. 				return ShowHelp( "/Overwrite requires /Save." );
  101. 			}
  102.  
  103. 			if ( save )
  104. 			{
  105. 				if ( File.Exists( @outputFile ) )
  106. 				{
  107. 					if ( !overwrite )
  108. 					{
  109. 						return ShowHelp( "File already exists." );
  110. 					}
  111. 				}
  112. 			}
  113.  
  114. 			#endregion Command Line Parsing
  115.  
  116. 			try
  117. 			{
  118. 				statusCode = (int) GetResponse( url );
  119.  
  120. 				if ( save )
  121. 				{
  122. 					using ( StreamWriter outputStream = new StreamWriter( @outputFile ) )
  123. 					{
  124. 						if ( overwrite )
  125. 						{
  126. 							outputStream.Flush( );
  127. 						}
  128. 						outputStream.Write( Get( url, enc ) );
  129. 						outputStream.Close( );
  130. 					}
  131. 				}
  132. 				else if ( show )
  133. 				{
  134. 					Console.Write( Get( url, enc ) );
  135. 				}
  136.  
  137. 				return ( statusCode == 200 ? 0 : statusCode );
  138. 			}
  139. 			catch ( Exception e )
  140. 			{
  141. 				return ShowHelp( e.Message );
  142. 			}
  143. 		}
  144.  
  145.  
  146. 		public static HttpStatusCode GetResponse( string url )
  147. 		{
  148. 			// Returns the HTTP Status Code for the specified URL, or 0 on errors
  149. 			try
  150. 			{
  151. 				HttpWebRequest req = (HttpWebRequest) WebRequest.Create( url );
  152. 				req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
  153. 				req.AllowAutoRedirect = false;
  154. 				HttpWebResponse resp = (HttpWebResponse) req.GetResponse( );
  155. 				HttpStatusCode stat = resp.StatusCode;
  156. 				req.Abort( );
  157. 				return stat;
  158. 			}
  159. 			catch ( WebException e )
  160. 			{
  161. 				// Handle exceptions caused by the server response, e.g. 404
  162. 				try
  163. 				{
  164. 					HttpWebResponse httpResponse = (HttpWebResponse) e.Response;
  165. 					return httpResponse.StatusCode;
  166. 				}
  167. 				// Handle the "real" exceptions
  168. 				catch
  169. 				{
  170. 					ShowHelp( e.Message );
  171. 					return 0;
  172. 				}
  173. 			}
  174. 			catch ( Exception e )
  175. 			{
  176. 				ShowHelp( e.Message );
  177. 				return 0;
  178. 			}
  179. 		}
  180.  
  181.  
  182. 		// Get( ) based on blog post by Sugree Phatanapherom
  183. 		// http://www.howforge.com/how-to-implement-simple-wget-in-c
  184. 		private static string Get( string url, string encoding )
  185. 		{
  186. 			try
  187. 			{
  188. 				// The next couple of ServicePointManager lines are required for secure connections only
  189.  
  190. 				System.Net.ServicePointManager.Expect100Continue = true;
  191. 				System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
  192. 				System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Ssl3;
  193. 				WebClient webClient = new WebClient( );
  194. 				byte[] response = webClient.DownloadData( url );
  195. 				switch ( encoding.ToUpper( ) )
  196. 				{
  197. 					case "A":
  198. 					case "ANSI":
  199. 					case "ASCII":
  200. 						return Encoding.ASCII.GetString( response );
  201. 					case "U":
  202. 					case "ULE":
  203. 					case "UNICODE":
  204. 					case "UNICODELE":
  205. 					case "UNICODE LE":
  206. 					case "UNICODE (LE)":
  207. 						return Encoding.Unicode.GetString( response );
  208. 					case "UB":
  209. 					case "UBE":
  210. 					case "UNICODEBE":
  211. 					case "UNICODE BE":
  212. 					case "UNICODE (BE)":
  213. 						return Encoding.BigEndianUnicode.GetString( response );
  214. 					case "U7":
  215. 					case "UTF7":
  216. 					case "UTF-7":
  217. 						return Encoding.UTF7.GetString( response );
  218. 					case "U8":
  219. 					case "UTF8":
  220. 					case "UTF-8":
  221. 						return Encoding.UTF8.GetString( response );
  222. 					case "U32":
  223. 					case "UTF32":
  224. 					case "UTF-32":
  225. 						return Encoding.UTF32.GetString( response );
  226. 					default:
  227. 						ShowHelp( "Invalid encoding" );
  228. 						return string.Empty;
  229. 				}
  230. 			}
  231. 			catch ( Exception e )
  232. 			{
  233. 				ShowHelp( e.Message );
  234. 				return string.Empty;
  235. 			}
  236. 		}
  237.  
  238. 		#region Error Handling
  239.  
  240. 		public static int ShowHelp( Exception e )
  241. 		{
  242. 			return ShowHelp( e == null ? null : e.Message );
  243. 		}
  244.  
  245. 		public static int ShowHelp( string errorMessage )
  246. 		{
  247. 			/*
  248. 			WGetLite,  Version 1.01
  249. 			Get web content (text only)
  250.  
  251. 			Usage:  WGETLITE  url  [ /Response | /Save:filename ]  [ /Encoding:encoding ]
  252.  
  253. 			Where:  /Response returns server response code only
  254. 					/Save     saves downloaded content to specified file
  255. 					/Encoding specifies encoding of url; accepted values are:
  256. 			                  ANSI, Unicode, Unicode (BE), UTF-7, UTF-8, UTF-32
  257.  
  258. 			Notes:  On errors the actual HTTP response code is returned as 'errorlevel'.
  259. 			        Usually specifying the encoding won't be necessary.
  260. 			        Switches may be abbreviated, e.g. /E:U8 instead of /Encoding:UTF-8.
  261. 			        Based on a blog post by Sugree Phatanapherom:
  262. 			        http://www.howforge.com/how-to-implement-simple-wget-in-c
  263.  
  264. 			Written by Rob van der Woude
  265. 			https://www.robvanderwoude.com
  266. 			*/
  267.  
  268. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  269. 			{
  270. 				Console.Error.WriteLine( );
  271. 				Console.ForegroundColor = ConsoleColor.Red;
  272. 				Console.Error.Write( "ERROR:  " );
  273. 				Console.ForegroundColor = ConsoleColor.White;
  274. 				Console.Error.WriteLine( errorMessage );
  275. 				Console.ResetColor( );
  276. 			}
  277.  
  278. 			Console.Error.WriteLine( );
  279.  
  280. 			Console.Error.WriteLine( "WGetLite,  Version {0}", progver );
  281.  
  282. 			Console.Error.WriteLine( "Get web content (text only)" );
  283.  
  284. 			Console.Error.WriteLine( );
  285.  
  286. 			Console.Error.Write( "Usage:  " );
  287. 			Console.ForegroundColor = ConsoleColor.White;
  288. 			Console.Error.WriteLine( "WGETLITE  url  [ /Response | /Save:filename ]  [ /Encoding:encoding ]" );
  289. 			Console.ResetColor( );
  290. 			Console.Error.WriteLine( );
  291.  
  292. 			Console.Error.Write( "Where:  " );
  293. 			Console.ForegroundColor = ConsoleColor.White;
  294. 			Console.Error.Write( "/R" );
  295. 			Console.ResetColor( );
  296. 			Console.Error.WriteLine( "esponse returns server response code only" );
  297.  
  298. 			Console.ForegroundColor = ConsoleColor.White;
  299. 			Console.Error.Write( "        /S" );
  300. 			Console.ResetColor( );
  301. 			Console.Error.WriteLine( "ave     saves downloaded content to specified file" );
  302.  
  303. 			Console.ForegroundColor = ConsoleColor.White;
  304. 			Console.Error.Write( "        /E" );
  305. 			Console.ResetColor( );
  306. 			Console.Error.WriteLine( "ncoding specifies encoding of url; accepted values are:" );
  307.  
  308. 			Console.ForegroundColor = ConsoleColor.White;
  309. 			Console.Error.Write( "                  A" );
  310. 			Console.ResetColor( );
  311. 			Console.Error.Write( "SCII, " );
  312.  
  313. 			Console.ForegroundColor = ConsoleColor.White;
  314. 			Console.Error.Write( "U" );
  315. 			Console.ResetColor( );
  316. 			Console.Error.Write( "nicode, " );
  317. 			Console.ForegroundColor = ConsoleColor.White;
  318. 			Console.Error.Write( "U" );
  319. 			Console.ResetColor( );
  320. 			Console.Error.Write( "nicode (" );
  321. 			Console.ForegroundColor = ConsoleColor.White;
  322. 			Console.Error.Write( "BE" );
  323. 			Console.ResetColor( );
  324. 			Console.Error.Write( "), " );
  325. 			Console.ForegroundColor = ConsoleColor.White;
  326. 			Console.Error.Write( "U" );
  327. 			Console.ResetColor( );
  328. 			Console.Error.Write( "TF-" );
  329. 			Console.ForegroundColor = ConsoleColor.White;
  330. 			Console.Error.Write( "7" );
  331. 			Console.ResetColor( );
  332. 			Console.Error.Write( ", " );
  333. 			Console.ForegroundColor = ConsoleColor.White;
  334. 			Console.Error.Write( "U" );
  335. 			Console.ResetColor( );
  336. 			Console.Error.Write( "TF-" );
  337. 			Console.ForegroundColor = ConsoleColor.White;
  338. 			Console.Error.Write( "8" );
  339. 			Console.ResetColor( );
  340. 			Console.Error.Write( ", " );
  341. 			Console.ForegroundColor = ConsoleColor.White;
  342. 			Console.Error.Write( "U" );
  343. 			Console.ResetColor( );
  344. 			Console.Error.Write( "TF-" );
  345. 			Console.ForegroundColor = ConsoleColor.White;
  346. 			Console.Error.WriteLine( "32" );
  347. 			Console.ResetColor( );
  348.  
  349. 			Console.Error.WriteLine( );
  350.  
  351. 			Console.Error.WriteLine( "Notes:  On errors the actual HTTP response code is returned as 'errorlevel'." );
  352.  
  353. 			Console.Error.WriteLine( "        Usually specifying the encoding won't be necessary." );
  354.  
  355. 			Console.Error.Write( "        Switches may be abbreviated, e.g. " );
  356. 			Console.ForegroundColor = ConsoleColor.White;
  357. 			Console.Error.Write( "/E" );
  358. 			Console.ResetColor( );
  359. 			Console.Error.Write( ":" );
  360. 			Console.ForegroundColor = ConsoleColor.White;
  361. 			Console.Error.Write( "U8" );
  362. 			Console.ResetColor( );
  363. 			Console.Error.Write( " instead of " );
  364. 			Console.ForegroundColor = ConsoleColor.White;
  365. 			Console.Error.Write( "/E" );
  366. 			Console.ResetColor( );
  367. 			Console.Error.Write( "ncoding:" );
  368. 			Console.ForegroundColor = ConsoleColor.White;
  369. 			Console.Error.Write( "UTF-8" );
  370. 			Console.Error.WriteLine( "." );
  371. 			Console.ResetColor( );
  372.  
  373. 			Console.Error.WriteLine( "        Based on a blog post by Sugree Phatanapherom:" );
  374.  
  375. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  376. 			Console.Error.WriteLine( "        http://www.howforge.com/how-to-implement-simple-wget-in-c" );
  377. 			Console.ResetColor( );
  378.  
  379. 			Console.Error.WriteLine( );
  380.  
  381. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  382.  
  383. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  384.  
  385. 			return 1;
  386. 		}
  387.  
  388. 		#endregion Error Handling
  389. 	}
  390. }
  391.  

page last uploaded: 2018-12-04, 10:47