コード例 #1
0
        /// <summary>
        /// Download file from website
        /// </summary>
        /// <param name="wc">Current webclient</param>
        /// <param name="uri">Website url</param>
        /// <param name="timeout">Timeout waiting for a response from server, in seconds.</param>
        /// <param name="targetFilePath">The local file path</param>
        public static void DownloadFile(AdvancedWebClient wc, string uri, int timeout, string targetFilePath, string postData)
        {
            if (wc == null)
            {
                wc = new AdvancedWebClient();
            }

            Byte[] buf = null;
            wc.Timeout = timeout;

            if (!string.IsNullOrEmpty(postData))
            {
                wc.PostData = postData;
            }

            int       retriesLeft    = 5;
            Exception innerException = null;

            while ((buf == null || buf.Length == 0) && retriesLeft-- > 0)
            {
                try
                {
                    buf = wc.DownloadData(uri);
                }
                catch (Exception ex)
                {
                    innerException = ex;
                }
            }

            if (buf == null)
            {
                throw new Exception(string.Format("Cannot download file from [{0}].", uri), innerException);
            }
            else
            {
                string targetDir = Path.GetDirectoryName(targetFilePath);
                if (!Directory.Exists(targetDir))
                {
                    Directory.CreateDirectory(targetDir);
                }
                using (var fs = new FileStream(targetFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    fs.Write(buf, 0, buf.Length);
                }
            }
        }
コード例 #2
0
        public static string GetPageSourceCompressed(AdvancedWebClient wc, string uri, int timeout, string postData, Encoding encoding)
        {
            if (wc == null)
            {
                wc = new AdvancedWebClient();
            }

            byte[] buf = null;
            wc.Timeout = timeout;
            if (!string.IsNullOrEmpty(postData))
            {
                wc.PostData = postData;
            }

            int retriesLeft = 5;

            while ((buf == null || buf.Length == 0) && retriesLeft-- > 0)
            {
                try { buf = wc.DownloadData(uri); }
                catch (Exception ex) { string errInfo = ex.ToString(); }
            }

            if (buf == null)
            {
                throw new Exception(string.Format("Cannot download page {0}", uri));
            }

            ////Encoding encoding = Encoding.UTF8;
            //Encoding encoding = Encoding.GetEncoding("gb2312");
            string contentEncoding = wc.ResponseHeaders["Content-Encoding"];

            buf = Decompress(buf);
            string html = encoding.GetString(buf);
            //string test = Encoding.GetEncoding("EUC-KR").GetString(buf);
            Encoding encoding2 = GetEncoding(html);

            if (encoding2 == null || encoding2 != encoding)
            {
                return(html);
            }
            return(encoding2.GetString(buf));
        }
コード例 #3
0
 //Get web page source from the certain URI
 public static string GetPageSource(AdvancedWebClient wc, string uri, int timeout, string postData)
 {
     return(GetPageSource(wc, uri, timeout, postData, null));
 }