// Uri baseAddress;
// string baseString;
// NameValueCollection queryString;
-// bool is_busy, async;
-// Thread async_thread;
+ bool is_busy, async;
+ Thread async_thread;
// Encoding encoding = Encoding.Default;
// IWebProxy proxy;
//
// }
//#endif
//
-//#if NET_2_0
-// public bool IsBusy {
-// get { return is_busy; }
-// }
-//#else
-// bool IsBusy {
-// get { return is_busy; }
-// }
-//#endif
-//
-// // Methods
-//
-// void CheckBusy ()
-// {
-// if (IsBusy)
-// throw new NotSupportedException ("WebClient does not support conccurent I/O operations.");
-// }
-//
-// void SetBusy ()
-// {
-// lock (this) {
-// CheckBusy ();
-// is_busy = true;
-// }
-// }
-//
+ public bool IsBusy {
+ get { return is_busy; }
+ }
+
+ // Methods
+
+ void CheckBusy ()
+ {
+ if (IsBusy)
+ throw new NotSupportedException ("WebClient does not support conccurent I/O operations.");
+ }
+
+ void SetBusy ()
+ {
+ lock (this) {
+ CheckBusy ();
+ is_busy = true;
+ }
+ }
+
// // DownloadData
//
// public byte [] DownloadData (string address)
//
// public event DownloadDataCompletedEventHandler DownloadDataCompleted;
// public event AsyncCompletedEventHandler DownloadFileCompleted;
-// public event DownloadProgressChangedEventHandler DownloadProgressChanged;
+ public event DownloadProgressChangedEventHandler DownloadProgressChanged;
// public event DownloadStringCompletedEventHandler DownloadStringCompleted;
-// public event OpenReadCompletedEventHandler OpenReadCompleted;
+ public event OpenReadCompletedEventHandler OpenReadCompleted;
// public event OpenWriteCompletedEventHandler OpenWriteCompleted;
// public event UploadDataCompletedEventHandler UploadDataCompleted;
// public event UploadFileCompletedEventHandler UploadFileCompleted;
// return new Uri (baseAddress, path + query, (query != null));
// }
//
-// WebRequest SetupRequest (Uri uri)
-// {
-// WebRequest request = WebRequest.Create (uri);
-//#if NET_2_0
+ WebRequest SetupRequest (Uri uri)
+ {
+ WebRequest request = WebRequest.Create (uri);
// if (Proxy != null)
// request.Proxy = Proxy;
-//#endif
// request.Credentials = credentials;
-//
-// // Special headers. These are properties of HttpWebRequest.
-// // What do we do with other requests differnt from HttpWebRequest?
+
+ // Special headers. These are properties of HttpWebRequest.
+ // What do we do with other requests differnt from HttpWebRequest?
// if (headers != null && headers.Count != 0 && (request is HttpWebRequest)) {
// HttpWebRequest req = (HttpWebRequest) request;
// string expect = headers ["Expect"];
// }
//
// responseHeaders = null;
-// return request;
-// }
-//
+ return request;
+ }
+
// WebRequest SetupRequest (Uri uri, string method)
// {
// WebRequest request = SetupRequest (uri);
// request.Method = DetermineMethod (uri, method);
// return request;
// }
-//
-// Stream ProcessResponse (WebResponse response)
-// {
+
+ Stream ProcessResponse (WebResponse response)
+ {
// responseHeaders = response.Headers;
-// return response.GetResponseStream ();
-// }
-//
+ return response.GetResponseStream ();
+ }
+
// byte [] ReadAll (Stream stream, int length, object userToken)
// {
// MemoryStream ms = null;
// }
// }
//
-// void CompleteAsync ()
-// {
-// lock (this){
-// is_busy = false;
-// async_thread = null;
-// }
-// }
-//
+ void CompleteAsync ()
+ {
+ lock (this){
+ is_busy = false;
+ async_thread = null;
+ }
+ }
+
// // DownloadDataAsync
//
// public void DownloadDataAsync (Uri address)
// }
// }
//
-// // OpenReadAsync
-//
-// public void OpenReadAsync (Uri address)
-// {
-// OpenReadAsync (address, null);
-// }
-//
-// public void OpenReadAsync (Uri address, object userToken)
-// {
-// if (address == null)
-// throw new ArgumentNullException ("address");
-//
-// lock (this) {
-// SetBusy ();
-// async = true;
-//
-// async_thread = new Thread (delegate (object state) {
-// object [] args = (object []) state;
-// WebRequest request = null;
-// try {
-// request = SetupRequest ((Uri) args [0]);
-// WebResponse response = request.GetResponse ();
-// Stream stream = ProcessResponse (response);
-// OnOpenReadCompleted (
-// new OpenReadCompletedEventArgs (stream, null, false, args [1]));
-// } catch (ThreadInterruptedException){
-// if (request != null)
-// request.Abort ();
-//
-// OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, null, true, args [1]));
-// } catch (Exception e){
-// OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, e, false, args [1]));
-// } });
-// object [] cb_args = new object [] {address, userToken};
-// async_thread.Start (cb_args);
-// }
-// }
-//
+ // OpenReadAsync
+
+ public void OpenReadAsync (Uri address)
+ {
+ OpenReadAsync (address, null);
+ }
+
+ public void OpenReadAsync (Uri address, object userToken)
+ {
+ if (address == null)
+ throw new ArgumentNullException ("address");
+
+ lock (this) {
+ SetBusy ();
+ async = true;
+
+ async_thread = new Thread (delegate (object state) {
+ object [] args = (object []) state;
+ WebRequest request = null;
+ try {
+ request = SetupRequest ((Uri) args [0]);
+ //WebResponse response = request.GetResponse ();
+ IAsyncResult asyncresult = request.BeginGetResponse (null, userToken);
+ asyncresult.AsyncWaitHandle.WaitOne ();
+ WebResponse response = request.EndGetResponse (asyncresult);
+ Stream stream = ProcessResponse (response);
+ OnOpenReadCompleted (
+ new OpenReadCompletedEventArgs (stream, null, false, args [1]));
+ } catch (ThreadInterruptedException){
+ if (request != null)
+ request.Abort ();
+
+ OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, null, true, args [1]));
+ } catch (Exception e){
+ OnOpenReadCompleted (new OpenReadCompletedEventArgs (null, e, false, args [1]));
+ } });
+ object [] cb_args = new object [] {address, userToken};
+ async_thread.Start (cb_args);
+ }
+ }
+
// // OpenWriteAsync
//
// public void OpenWriteAsync (Uri address)
// DownloadFileCompleted (this, args);
// }
//
-// protected virtual void OnDownloadProgressChanged (DownloadProgressChangedEventArgs e)
-// {
-// if (DownloadProgressChanged != null)
-// DownloadProgressChanged (this, e);
-// }
-//
+ protected virtual void OnDownloadProgressChanged (DownloadProgressChangedEventArgs e)
+ {
+ if (DownloadProgressChanged != null)
+ DownloadProgressChanged (this, e);
+ }
+
// protected virtual void OnDownloadStringCompleted (DownloadStringCompletedEventArgs args)
// {
// CompleteAsync ();
// DownloadStringCompleted (this, args);
// }
//
-// protected virtual void OnOpenReadCompleted (OpenReadCompletedEventArgs args)
-// {
-// CompleteAsync ();
-// if (OpenReadCompleted != null)
-// OpenReadCompleted (this, args);
-// }
-//
+ protected virtual void OnOpenReadCompleted (OpenReadCompletedEventArgs args)
+ {
+ CompleteAsync ();
+ if (OpenReadCompleted != null)
+ OpenReadCompleted (this, args);
+ }
+
// protected virtual void OnOpenWriteCompleted (OpenWriteCompletedEventArgs args)
// {
// CompleteAsync ();