/// <summary> /// Get response from the given uri with given credentials /// </summary> /// <param name="uri">uri</param> /// <param name="credentials">credentials</param> /// <param name="waitTimeInMilliSec">wait time in milliseconds</param> /// <returns></returns> public static String GetResponse(String uri, ICredentials credentials = null, int waitTimeInMilliSec = Timeout.Infinite) { if (uri == null || uri.Length == 0) { throw new Exception("Must supply valid URI!"); } WebClient client = new WebClient(); WebRequest.Create(uri); string result = null; if (credentials != null) { client.Credentials = credentials; } EventEx doneEvent = new EventEx(false, EventResetMode.AutoReset); client.DownloadStringCompleted += (s, e) => { result = e.Result; doneEvent.SetEvent(); }; client.DownloadStringAsync(new Uri(uri, UriKind.Absolute)); doneEvent.WaitForEvent(waitTimeInMilliSec); return(result); }
/// <summary> /// Download file from given uri to given filepath /// </summary> /// <param name="uri">uri</param> /// <param name="filepath">filepath</param> /// <param name="waitTimeInMilliSec">wait time in milliseconds</param> public static void DownloadFile(String uri, String filepath, int waitTimeInMilliSec = Timeout.Infinite) { if (uri == null || uri.Length == 0) { throw new Exception("Must supply valid URI!"); } if (filepath == null || filepath.Length == 0) { throw new Exception("Must supply valid filepath!"); } WebClient webClient = new WebClient(); EventEx doneEvent = new EventEx(false, EventResetMode.AutoReset); webClient.OpenReadCompleted += (s, e) => { try { using (FileStream stream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { e.Result.CopyTo(stream); stream.Flush(); stream.Close(); doneEvent.SetEvent(); } } catch (Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); } }; webClient.OpenReadAsync(new Uri(uri, UriKind.Absolute)); doneEvent.WaitForEvent(waitTimeInMilliSec); }
/// <summary> /// Stop the log worker /// </summary> void stop() { if (m_thread.GetStatus() != ThreadStatus.TERMINATED) { m_threadStopEvent.SetEvent(); m_thread.WaitFor(); } }
/// <summary> /// Wait for worker thread to terminate, and if not terminated, then Terminate. /// </summary> /// <param name="waitTimeInMilliSec">the time-out interval, in milliseconds.</param> /// <returns>the terminate result of the thread</returns> public override TerminateResult TerminateWorker(int waitTimeInMilliSec = Timeout.Infinite) { m_terminateEvent.SetEvent(); Resume(); return(TerminateAfter(waitTimeInMilliSec)); }
/// <summary> /// Get response from the given uri with given credentials /// </summary> /// <param name="uri">uri</param> /// <param name="credentials">credentials</param> /// <param name="waitTimeInMilliSec">wait time in milliseconds</param> /// <returns></returns> public static String GetResponse(String uri, ICredentials credentials = null, int waitTimeInMilliSec = Timeout.Infinite) { if (uri == null || uri.Length == 0) throw new Exception("Must supply valid URI!"); WebClient client = new WebClient(); WebRequest.Create(uri); string result = null; if (credentials != null) client.Credentials = credentials; EventEx doneEvent = new EventEx(false, EventResetMode.AutoReset); client.DownloadStringCompleted += (s, e) => { result = e.Result; doneEvent.SetEvent(); }; client.DownloadStringAsync(new Uri(uri, UriKind.Absolute)); doneEvent.WaitForEvent(waitTimeInMilliSec); return result; }
/// <summary> /// Download file from given uri to given filepath /// </summary> /// <param name="uri">uri</param> /// <param name="filepath">filepath</param> /// <param name="waitTimeInMilliSec">wait time in milliseconds</param> public static void DownloadFile(String uri, String filepath, int waitTimeInMilliSec=Timeout.Infinite) { if (uri == null || uri.Length == 0) throw new Exception("Must supply valid URI!"); if (filepath == null || filepath.Length == 0) throw new Exception("Must supply valid filepath!"); WebClient webClient = new WebClient(); EventEx doneEvent = new EventEx(false, EventResetMode.AutoReset); webClient.OpenReadCompleted += (s, e) => { try { using (FileStream stream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { e.Result.CopyTo(stream); stream.Flush(); doneEvent.SetEvent(); } } catch (Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); } }; webClient.OpenReadAsync(new Uri(uri, UriKind.Absolute)); doneEvent.WaitForEvent(waitTimeInMilliSec); }