Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.1 KiB

  1. using System;
  2. using System.Net;
  3. using System.IO;
  4. using System.Security.Cryptography.X509Certificates;
  5. using System.Text;
  6. using System.Xml;
  7. namespace UDDI.Tools
  8. {
  9. class UDDISend
  10. {
  11. public enum AuthenticationType
  12. {
  13. Uninitialized = 0,
  14. WindowsAuthentication = 1,
  15. UDDIAuthentication = 2,
  16. ClientCertificateAuthentication = 3
  17. }
  18. public const int BlockSize = 4096;
  19. public static AuthenticationType AuthType = AuthenticationType.Uninitialized;
  20. public static string Url = null;
  21. public static string MessageFilename = null;
  22. public static string CertificateFilename = null;
  23. public static void Main( string[] args )
  24. {
  25. Console.WriteLine( "Microsoft (R) UDDI Send Utility" );
  26. Console.WriteLine( "Copyright (C) Microsoft Corp. 2002. All rights reserved.\r\n" );
  27. try
  28. {
  29. ProcessCommandLine( args );
  30. //
  31. // Retrieve the input data from the specified file
  32. //
  33. Console.Write( "Loading '" + MessageFilename + "'... ");
  34. FileStream f = new FileStream( MessageFilename, FileMode.Open, FileAccess.Read );
  35. System.IO.BinaryReader br = new BinaryReader( f );
  36. byte[] cbInput = new byte[ BlockSize ];
  37. int n = 0;
  38. int nTotal = 0;
  39. HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( Url );
  40. req.Timeout = -1;
  41. if( AuthenticationType.WindowsAuthentication == AuthType )
  42. {
  43. req.Credentials = CredentialCache.DefaultCredentials;
  44. req.PreAuthenticate = true;
  45. }
  46. else if( AuthenticationType.ClientCertificateAuthentication == AuthType )
  47. {
  48. req.ClientCertificates.Add( X509Certificate.CreateFromCertFile( CertificateFilename ) );
  49. }
  50. //
  51. // Populate the request data from the input file
  52. //
  53. req.Method = "POST";
  54. req.ContentType = "text/xml; charset=\"utf-8\"";
  55. req.ContentLength = f.Length;
  56. req.Headers.Add( "SOAPAction", "\"\"" );
  57. n = br.Read( cbInput, 0, BlockSize );
  58. while( n > 0 )
  59. {
  60. nTotal += n;
  61. req.GetRequestStream().Write( cbInput, 0, n );
  62. n = br.Read( cbInput, 0, BlockSize );
  63. }
  64. Console.WriteLine( "done." );
  65. Console.Write( "Transmitting... " );
  66. Stream strmResponse;
  67. HttpWebResponse result;
  68. try
  69. {
  70. result = (HttpWebResponse) req.GetResponse();
  71. Console.WriteLine( "done." );
  72. }
  73. catch( WebException we )
  74. {
  75. Console.WriteLine( "error." );
  76. result = (HttpWebResponse) we.Response;
  77. Console.WriteLine( "\r\nException: {0}", we.ToString() );
  78. }
  79. //
  80. // Write the results to the standard output
  81. //
  82. Console.Write( "\r\nResponse stream received\r\nStatus code: " + result.StatusCode );
  83. Console.Write( "\r\nStatus Description: " + result.StatusDescription );
  84. strmResponse = result.GetResponseStream();
  85. Byte[] cbRead = new Byte[ 512 ];
  86. BinaryReader br1 = new BinaryReader( strmResponse );
  87. int nBytesRead = br1.Read( cbRead, 0, 512 );
  88. Console.WriteLine("\r\n\r\nXML:\r\n");
  89. FileStream file = new FileStream( "output.xml", System.IO.FileMode.Create );
  90. BinaryWriter strm = new BinaryWriter( file, System.Text.Encoding.UTF8 );
  91. while( nBytesRead > 0 )
  92. {
  93. Console.Write( System.Text.Encoding.UTF8.GetString( cbRead, 0, nBytesRead ) );
  94. strm.Write( cbRead, 0, nBytesRead );
  95. nBytesRead = br1.Read( cbRead, 0, 512 );
  96. }
  97. strm.Close();
  98. Console.WriteLine("");
  99. }
  100. catch( CommandLineException e )
  101. {
  102. if( null != e.Message && e.Message.Length > 0 )
  103. {
  104. Console.WriteLine( e.Message );
  105. Console.WriteLine();
  106. }
  107. else
  108. {
  109. DisplayUsage();
  110. }
  111. }
  112. catch( Exception e )
  113. {
  114. Console.WriteLine ("Exception: {0}", e.ToString());
  115. }
  116. return;
  117. }
  118. internal static void ProcessCommandLine( string[] args )
  119. {
  120. int i = 0;
  121. if( 0 == args.Length )
  122. throw new CommandLineException();
  123. while( i < args.Length )
  124. {
  125. if( "-" == args[ i ].Substring( 0, 1 ) || "/" == args[ i ].Substring( 0, 1 ) )
  126. {
  127. switch( args[ i ].Substring( 1 ).ToLower() )
  128. {
  129. case "w":
  130. AuthType = AuthenticationType.WindowsAuthentication;
  131. Console.WriteLine( "Including Windows Authentication credentials\r\n" );
  132. break;
  133. case "u":
  134. AuthType = AuthenticationType.UDDIAuthentication;
  135. Console.WriteLine( "Using UDDI Authentication -- no credentials included\r\n" );
  136. break;
  137. case "c":
  138. if( i + 1 >= args.Length )
  139. throw new CommandLineException( "Missing required parameter 'CertificateFilename'." );
  140. i ++;
  141. if( !File.Exists( args[ i ] ) )
  142. throw new CommandLineException( "Certificate file '" + args[ i ] + "' does not exist." );
  143. AuthType = AuthenticationType.ClientCertificateAuthentication;
  144. CertificateFilename = args[ i ];
  145. Console.WriteLine( "Using client certificate '" + CertificateFilename + "' for authentication.\r\n" );
  146. break;
  147. case "?":
  148. throw new CommandLineException();
  149. default:
  150. throw new CommandLineException( "Invalid switch." );
  151. }
  152. }
  153. else
  154. {
  155. if( null == Url )
  156. Url = args[ i ];
  157. else if( null == MessageFilename )
  158. MessageFilename = args[ i ];
  159. else
  160. throw new CommandLineException( "Too many command line parameters." );
  161. }
  162. i ++;
  163. }
  164. if( null == Url )
  165. throw new CommandLineException( "Missing required parameter 'URL'." );
  166. if( null == MessageFilename )
  167. throw new CommandLineException( "Missing required parameter 'InputFile'." );
  168. if( AuthenticationType.Uninitialized == AuthType )
  169. {
  170. AuthType = AuthenticationType.UDDIAuthentication;
  171. Console.WriteLine( "Using UDDI Authentication -- no credentials included\r\n" );
  172. }
  173. }
  174. public static void DisplayUsage()
  175. {
  176. Console.WriteLine( "Sends a UDDI message to a specific URL." );
  177. Console.WriteLine( "\r\nUsage:" );
  178. Console.WriteLine( "\tsend [switches] URL InputFile" );
  179. Console.WriteLine( "\r\nSwitches:" );
  180. Console.WriteLine( "\t-w Windows authentication" );
  181. Console.WriteLine( "\t-u UDDI authentication (default)" );
  182. Console.WriteLine( "\t-c <certfile> Client certificate authentication" );
  183. Console.WriteLine( "\r\nExamples:" );
  184. Console.WriteLine( "\tsend -w http://uddi.microsoft.com/inquire c:\\somefile.xml" );
  185. Console.WriteLine( "\tsend -u https://test.uddi.microsoft.com/publish c:\\somefile.xml" );
  186. Console.WriteLine( "\tsend -c uddi.cer https://uddi.microsoft.com/operator c:\\somefile.xml" );
  187. }
  188. /// ****************************************************************
  189. /// public class CommandLineException
  190. /// ----------------------------------------------------------------
  191. /// <summary>
  192. /// Exception class for errors encountered while parsing the
  193. /// command-line.
  194. /// </summary>
  195. /// ****************************************************************
  196. ///
  197. public class CommandLineException : ApplicationException
  198. {
  199. /// ************************************************************
  200. /// public CommandLineException [constructor]
  201. /// ------------------------------------------------------------
  202. /// <summary>
  203. /// CommandLineException constructor.
  204. /// </summary>
  205. /// ************************************************************
  206. ///
  207. public CommandLineException()
  208. : base( "" )
  209. {
  210. }
  211. /// ************************************************************
  212. /// public CommandLineException [constructor]
  213. /// ------------------------------------------------------------
  214. /// <summary>
  215. /// CommandLineException constructor.
  216. /// </summary>
  217. /// ------------------------------------------------------------
  218. /// <param name="message">
  219. /// Exception message.
  220. /// </param>
  221. /// ************************************************************
  222. ///
  223. public CommandLineException( string message )
  224. : base( message )
  225. {
  226. }
  227. }
  228. }
  229. }