public static ResultCode GetString(Uri uri, NetworkCredential credential = null, int timeout = 4 * 60 * 1000) { Trace.Assert(uri != null); ResultCode resCode = ResultCode.UNKNOWN; credential = credential ?? CredentialCache.DefaultNetworkCredentials; var web = new WebClientWithTimeOut(timeout); web.Credentials = credential; web.DownloadProgressChanged += (s, e) => { if (e == null) { return; } Console.Error.WriteLine("downloading: " + e.BytesReceived + " " + e.TotalBytesToReceive); Console.Error.Flush(); }; web.DownloadStringCompleted += (s, e) => { if (e.Cancelled) { resCode = ResultCode.CANCELLED; Console.Error.WriteLine("cancelled"); Console.Error.Flush(); } else if (e.Error != null) { resCode = ResultCode.ERROR; Console.Error.WriteLine(e.Error.Message); Console.Error.WriteLine(e.Error.StackTrace); Console.Error.Flush(); } else { resCode = ResultCode.COMPLETED; Console.Out.WriteLine(e.Result); Console.Out.Flush(); } }; try { web.DownloadStringTaskAsync(uri).Wait(); } catch (Exception exc) { resCode = ResultCode.ERROR; Console.Out.WriteLine(exc.StackTrace); Console.Out.Flush(); PrintExceptions(exc, 0); } web.Dispose(); return(resCode); }
public static ResultCode GetFile(Uri uri, string prefix, string dstFileName, NetworkCredential credential = null, int timeout = 4 * 60 * 1000) { Trace.Assert(uri != null); ResultCode resCode = ResultCode.UNKNOWN; credential = credential ?? CredentialCache.DefaultNetworkCredentials; string tmpFileName; do { tmpFileName = Path.Combine( prefix, Path.GetRandomFileName() + ".wgettmp"); } while (File.Exists(tmpFileName)); var web = new WebClientWithTimeOut(timeout); web.Credentials = credential; web.DownloadProgressChanged += (s, e) => { if (e == null) { return; } Console.Error.WriteLine("downloading: " + e.BytesReceived + " " + e.TotalBytesToReceive); Console.Error.Flush(); }; web.DownloadFileCompleted += (s, e) => { if (e.Cancelled) { resCode = ResultCode.CANCELLED; Console.Error.WriteLine("cancelled"); Console.Error.Flush(); } else if (e.Error != null) { resCode = ResultCode.ERROR; Console.Error.WriteLine(e.Error.Message); Console.Error.WriteLine(e.Error.StackTrace); Console.Error.Flush(); } else { resCode = ResultCode.COMPLETED; } }; try { web.DownloadFileTaskAsync(uri, tmpFileName).Wait(); } catch (Exception exc) { resCode = ResultCode.ERROR; Console.Error.WriteLine(exc.StackTrace); Console.Error.Flush(); PrintExceptions(exc, 0); } if (resCode == ResultCode.COMPLETED) { if (dstFileName == null) { if (web.ResponseHeaders.Get("Content-Disposition") != null) { string _tmp = web.ResponseHeaders.Get("Content-Disposition"); _tmp = _tmp.Substring(_tmp.IndexOf("filename=") + 9).Replace("\"", ""); dstFileName = _tmp.Split(';')[0]; } else { dstFileName = Path.GetFileName(uri.AbsolutePath); } dstFileName = Path.Combine(prefix, dstFileName); } try { if (File.Exists(dstFileName)) { File.Delete(dstFileName); } File.Move(tmpFileName, dstFileName); } catch (Exception exc) { resCode = ResultCode.ERROR; Console.Error.WriteLine(exc.StackTrace); Console.Error.Flush(); PrintExceptions(exc, 0); } } if (File.Exists(tmpFileName)) { try { File.Delete(tmpFileName); } catch (Exception) { } } Console.Out.WriteLine(resCode.ToString() + " " + dstFileName); Console.Out.Flush(); web.Dispose(); return(resCode); }