2 // tlsmulti.cs: Multi-sessions TLS/SSL Test Program with async HttpWebRequest
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004-2005 Novell (http://www.novell.com)
12 using System.Collections;
13 using System.Globalization;
16 using System.Net.Sockets;
17 using System.Reflection;
18 using System.Security.Cryptography.X509Certificates;
20 using System.Threading;
22 using Mono.Security.Protocol.Tls;
26 static ArrayList handleList = new ArrayList ();
29 private HttpWebRequest request;
30 private ManualResetEvent handle;
32 public State (int id, HttpWebRequest req)
36 handle = new ManualResetEvent (false);
37 handleList.Add (handle);
44 public HttpWebRequest Request {
45 get { return request; }
48 public void Complete ()
53 static public void WaitAll ()
55 if (handleList.Count > 0) {
56 WaitHandle[] handles = (WaitHandle[]) handleList.ToArray (typeof (WaitHandle));
57 WaitHandle.WaitAll (handles);
63 public class MultiTest {
67 public static void Main (string[] args)
69 if (args.Length == 0) {
70 Console.WriteLine ("usage: mono tlsmulti.exe url1 [url ...]");
72 } else if (args.Length > 64) {
73 Console.WriteLine ("WaitHandle has a limit of 64 handles so you cannot process {0} URLs.", args.Length);
77 alone = (args.Length == 1);
78 ServicePointManager.CertificatePolicy = new TestCertificatePolicy ();
81 foreach (string url in args) {
82 Console.WriteLine ("GET #{0} at {1}", id, url);
83 HttpWebRequest wreq = (HttpWebRequest) WebRequest.Create (url);
84 State s = new State (id++, wreq);
85 wreq.BeginGetResponse (new AsyncCallback (ResponseCallback), s);
91 private static void ResponseCallback (IAsyncResult result)
93 State state = ((State) result.AsyncState);
94 Console.WriteLine ("END #{0}", state.Id);
95 HttpWebResponse response = (HttpWebResponse) state.Request.EndGetResponse (result);
97 Stream stream = response.GetResponseStream ();
98 StreamReader sr = new StreamReader (stream, Encoding.UTF8);
99 string data = sr.ReadToEnd ();
102 Console.WriteLine (data);
106 public class TestCertificatePolicy : ICertificatePolicy {
108 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
110 // whatever the reason we do not stop the SSL connection