Esempio n. 1
0
        public static FileInfo Download(string url, string saveDir)
        {
            FileInfo       file = null;
            WebUtils       wu   = new WebUtils();
            HttpWebRequest req  = wu.GetWebRequest(url, "GET");

            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();

            if (CTYPE_ZIP.Equals(rsp.ContentType))
            {
                if (string.IsNullOrEmpty(saveDir))
                {
                    file = new FileInfo(Path.GetTempFileName() + ".zip");
                }
                else
                {
                    file = new FileInfo(Path.Combine(saveDir, Path.GetRandomFileName() + ".zip"));
                }

                using (Stream rspStream = rsp.GetResponseStream())
                {
                    byte[] bytes = new byte[1024];
                    int    len   = 0;

                    using (FileStream fileStream = new FileStream(file.FullName, FileMode.OpenOrCreate))
                    {
                        while ((len = rspStream.Read(bytes, 0, bytes.Length)) > 0)
                        {
                            fileStream.Write(bytes, 0, len);
                        }
                    }
                }
            }
            else
            {
                string json = wu.GetResponseAsString(rsp, Encoding.GetEncoding(rsp.CharacterSet));
                if (!string.IsNullOrEmpty(json))
                {
                    IDictionary dict    = Top.Api.Util.TopUtils.ParseJson(json);
                    string      errCode = dict["code"] as string;
                    string      errMsg  = dict["message"] as string;
                    throw new Top.Api.TopException(errCode, errMsg);
                }
            }
            return(file);
        }
Esempio n. 2
0
        public static FileInfo Download(string url, string saveDir)
        {
            FileInfo file = null;
            WebUtils wu = new WebUtils();
            HttpWebRequest req = wu.GetWebRequest(url, "GET");
            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
            if (CTYPE_ZIP.Equals(rsp.ContentType))
            {
                if (string.IsNullOrEmpty(saveDir))
                {
                    file = new FileInfo(Path.GetTempFileName() + ".zip");
                }
                else
                {
                    file = new FileInfo(Path.Combine(saveDir, Path.GetRandomFileName() + ".zip"));
                }

                using (System.IO.Stream rspStream = rsp.GetResponseStream())
                {
                    byte[] bytes = new byte[1024];
                    int len = 0;

                    using (FileStream fileStream = new FileStream(file.FullName, FileMode.OpenOrCreate))
                    {
                        while ((len = rspStream.Read(bytes, 0, bytes.Length)) > 0)
                        {
                            fileStream.Write(bytes, 0, len);
                        }
                    }
                }
            }
            else
            {
                string json = wu.GetResponseAsString(rsp, Encoding.GetEncoding(rsp.CharacterSet));
                if (!string.IsNullOrEmpty(json))
                {
                    IDictionary dict = Top.Api.Util.TopUtils.ParseJson(json);
                    string errCode = dict["code"] as string;
                    string errMsg = dict["message"] as string;
                    throw new Top.Api.TopException(errCode, errMsg);
                }
            }
            return file;
        }
Esempio n. 3
0
        /// <summary>
        /// 通过HTTP GET方式下载文件到指定的目录。
        /// </summary>
        /// <param name="url">需要下载的URL</param>
        /// <param name="destDir">需要下载到的目录</param>
        /// <returns>下载后的文件</returns>
        public static string Download(string url, string destDir)
        {
            string file = null;

            try
            {
                WebUtils        wu  = new WebUtils();
                HttpWebRequest  req = wu.GetWebRequest(url, "GET", null);
                HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
                if (CTYPE_OCTET.Equals(rsp.ContentType))
                {
                    file = Path.Combine(destDir, GetFileName(rsp.Headers["Content-Disposition"]));
                    using (System.IO.Stream rspStream = rsp.GetResponseStream())
                    {
                        int    len = 0;
                        byte[] buf = new byte[8192];
                        using (FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate))
                        {
                            while ((len = rspStream.Read(buf, 0, buf.Length)) > 0)
                            {
                                fileStream.Write(buf, 0, len);
                            }
                        }
                    }
                }
                else
                {
                    throw new TopException(wu.GetResponseAsString(rsp, Encoding.UTF8));
                }
            }
            catch (WebException e)
            {
                throw new TopException("isv.file-already-download", e.Message);
            }
            return(file);
        }
Esempio n. 4
0
        /// <summary>
        /// 通过HTTP GET方式下载文件到指定的目录。
        /// </summary>
        /// <param name="url">需要下载的URL</param>
        /// <param name="destDir">需要下载到的目录</param>
        /// <returns>下载后的文件</returns>
        public static string Download(string url, string destDir)
        {
            string file = null;

            try
            {
                WebUtils wu = new WebUtils();
                HttpWebRequest req = wu.GetWebRequest(url, "GET");
                HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
                if (CTYPE_OCTET.Equals(rsp.ContentType))
                {
                    file = Path.Combine(destDir, GetFileName(rsp.Headers["Content-Disposition"]));
                    using (System.IO.Stream rspStream = rsp.GetResponseStream())
                    {
                        int len = 0;
                        byte[] buf = new byte[8192];
                        using (FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate))
                        {
                            while ((len = rspStream.Read(buf, 0, buf.Length)) > 0)
                            {
                                fileStream.Write(buf, 0, len);
                            }
                        }
                    }
                }
                else
                {
                    throw new TopException(wu.GetResponseAsString(rsp, Encoding.UTF8));
                }
            }
            catch (WebException e)
            {
                throw new TopException("isv.file-already-download", e.Message);
            }
            return file;
        }