Ejemplo n.º 1
0
        /// <summary>
        /// 校验参数
        /// </summary>
        /// <returns></returns>
        private bool CheckPara()
        {
            bool boolResult = true;

            if (string.IsNullOrEmpty(strFtpUri))
            {
                Log4NetUtil.Error(this, "CheckPara->FtpUri为空");
                return(false);
            }
            if (string.IsNullOrEmpty(strFtpUserID))
            {
                Log4NetUtil.Error(this, "CheckPara->FtpUserID为空");
                return(false);
            }
            if (string.IsNullOrEmpty(strFtpPassword))
            {
                Log4NetUtil.Error(this, "CheckPara->FtpPassword为空");
                return(false);
            }
            if (intFtpPort == 0 || intFtpPort == int.MaxValue || intFtpPort == int.MinValue)
            {
                Log4NetUtil.Error(this, "CheckPara->intFtpPort异常:" + intFtpPort.ToString());
                return(false);
            }
            return(boolResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 下载多文件
        /// </summary>
        /// <param name="localDic">本地目录(@"D:\test")</param>
        /// <param name="remotePath">远程路径列表</param>
        /// <returns></returns>
        public int DownloadFiles(string localDic, IEnumerable <string> remoteFiles)
        {
            int count = 0;

            if (remoteFiles == null)
            {
                return(0);
            }

            try
            {
                //本地目录不存在,则自动创建
                if (!Directory.Exists(localDic))
                {
                    Directory.CreateDirectory(localDic);
                }

                if (Connect())
                {
                    count = ftpClient.DownloadFiles(localDic, remoteFiles, FtpLocalExists.Overwrite);
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(this, "DownloadFiles->下载文件 异常:" + ex.ToString());
            }
            finally
            {
                DisConnect();
            }

            return(count);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 下载单文件
        /// </summary>
        /// <param name="localDic">本地目录(@"D:\test")</param>
        /// <param name="remotePath">远程路径("/test/abc.txt")</param>
        /// <returns></returns>
        public bool DownloadFile(string localDic, string remotePath)
        {
            bool   boolResult  = false;
            string strFileName = string.Empty;

            try
            {
                //本地目录不存在,则自动创建
                if (!Directory.Exists(localDic))
                {
                    Directory.CreateDirectory(localDic);
                }
                //取下载文件的文件名
                strFileName = Path.GetFileName(remotePath);
                //拼接本地路径
                localDic = Path.Combine(localDic, strFileName);

                if (Connect())
                {
                    boolResult = ftpClient.DownloadFile(localDic, remotePath, FtpLocalExists.Overwrite);
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(this, "DownloadFile->下载文件 异常:" + ex.ToString() + "|*|remotePath:" + remotePath);
            }
            finally
            {
                DisConnect();
            }

            return(boolResult);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="localPath">本地路径(@"D:\abc.txt")</param>
        /// <param name="remoteDic">远端目录("/test")</param>
        /// <returns></returns>
        public bool UploadFile(string localPath, string remoteDic)
        {
            bool     boolResult = false;
            FileInfo fileInfo   = null;

            try
            {
                //本地路径校验
                if (!File.Exists(localPath))
                {
                    Log4NetUtil.Error(this, "UploadFile->本地文件不存在:" + localPath);
                    return(boolResult);
                }
                else
                {
                    fileInfo = new FileInfo(localPath);
                }
                //远端路径校验
                if (string.IsNullOrEmpty(remoteDic))
                {
                    remoteDic = "/";
                }
                if (!remoteDic.StartsWith("/"))
                {
                    remoteDic = "/" + remoteDic;
                }
                if (!remoteDic.EndsWith("/"))
                {
                    remoteDic += "/";
                }

                //拼接远端路径
                remoteDic += fileInfo.Name;

                if (Connect())
                {
                    using (FileStream fs = fileInfo.OpenRead())
                    {
                        //重名覆盖
                        sftpClient.UploadFile(fs, remoteDic, true);
                    }

                    boolResult = true;
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(this, "UploadFile->上传文件 异常:" + ex.ToString() + "|*|localPath:" + localPath);
            }
            finally
            {
                DisConnect();
            }

            return(boolResult);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 上传多文件
        /// </summary>
        /// <param name="localFiles">本地路径列表</param>
        /// <param name="remoteDic">远端目录("/test")</param>
        /// <returns></returns>
        public int UploadFiles(IEnumerable <string> localFiles, string remoteDic)
        {
            int             count     = 0;
            List <FileInfo> listFiles = new List <FileInfo>();

            if (localFiles == null)
            {
                return(0);
            }

            try
            {
                foreach (string file in localFiles)
                {
                    if (!File.Exists(file))
                    {
                        Log4NetUtil.Error(this, "UploadFiles->本地文件不存在:" + file);
                        continue;
                    }
                    listFiles.Add(new FileInfo(file));
                }

                //远端路径校验
                if (string.IsNullOrEmpty(remoteDic))
                {
                    remoteDic = "/";
                }
                if (!remoteDic.StartsWith("/"))
                {
                    remoteDic = "/" + remoteDic;
                }
                if (!remoteDic.EndsWith("/"))
                {
                    remoteDic += "/";
                }

                if (Connect())
                {
                    if (listFiles.Count > 0)
                    {
                        count = ftpClient.UploadFiles(listFiles, remoteDic, FtpExists.Overwrite, true);
                    }
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(this, "UploadFiles->上传文件 异常:" + ex.ToString());
            }
            finally
            {
                DisConnect();
            }

            return(count);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 创建Sftp客户端
 /// </summary>
 private void GetSftpClient()
 {
     if (CheckPara())
     {
         try
         {
             sftpClient = new SftpClient(strFtpUri, intFtpPort, strFtpUserID, strFtpPassword);
         }
         catch (Exception ex)
         {
             Log4NetUtil.Error(this, "GetSftpClient->创建Sftp客户端异常:" + ex.ToString());
         }
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 创建ftp客户端
 /// </summary>
 private void GetFtpClient()
 {
     if (CheckPara())
     {
         try
         {
             ftpClient = new FtpClient(strFtpUri, intFtpPort, strFtpUserID, strFtpPassword);
             ftpClient.RetryAttempts = intRetryTimes;
         }
         catch (Exception ex)
         {
             Log4NetUtil.Error(this, "GetFtpClient->创建ftp客户端异常:" + ex.ToString());
         }
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// 取得文件列表
        /// </summary>
        /// <param name="remoteDic">远程目录</param>
        /// <param name="type">类型:file-文件,dic-目录</param>
        /// <returns></returns>
        public List <string> ListDirectory(string remoteDic, string type = "file")
        {
            List <string> list = new List <string>();

            type = type.ToLower();

            try
            {
                if (Connect())
                {
                    var files = sftpClient.ListDirectory(remoteDic);
                    foreach (var file in files)
                    {
                        if (type == "file")
                        {
                            if (file.IsRegularFile)
                            {
                                list.Add(file.Name);
                            }
                        }
                        else if (type == "dic")
                        {
                            if (file.IsDirectory)
                            {
                                list.Add(file.Name);
                            }
                        }
                        else
                        {
                            list.Add(file.Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(this, "ListDirectory->取得文件列表 异常:" + ex.ToString());
            }
            finally
            {
                DisConnect();
            }

            return(list);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 取得文件或目录列表
        /// </summary>
        /// <param name="remoteDic">远程目录</param>
        /// <param name="type">类型:file-文件,dic-目录</param>
        /// <returns></returns>
        public List <string> ListDirectory(string remoteDic, string type = "file")
        {
            List <string> list = new List <string>();

            type = type.ToLower();

            try
            {
                if (Connect())
                {
                    FtpListItem[] files = ftpClient.GetListing(remoteDic);
                    foreach (FtpListItem file in files)
                    {
                        if (type == "file")
                        {
                            if (file.Type == FtpFileSystemObjectType.File)
                            {
                                list.Add(file.Name);
                            }
                        }
                        else if (type == "dic")
                        {
                            if (file.Type == FtpFileSystemObjectType.Directory)
                            {
                                list.Add(file.Name);
                            }
                        }
                        else
                        {
                            list.Add(file.Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(this, "ListDirectory->取得文件或目录列表 异常:" + ex.ToString());
            }
            finally
            {
                DisConnect();
            }

            return(list);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 异步发送邮件回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            MailMessage mailMsg = (MailMessage)e.UserState;

            if (e.Cancelled)
            {
                //发送被取消
                Log4NetUtil.Error(typeof(MailUtil), "发送邮件被取消:标题->" + mailMsg.Subject);
                return;
            }

            if (e.Error != null)
            {
                //出现错误
                Log4NetUtil.Error(typeof(MailUtil), "发送邮件出错:标题->" + mailMsg.Subject + ";异常信息->" + e.Error.ToString());
                return;
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 判断文件或目录是否存在
        /// </summary>
        /// <param name="remotePath">远程路径("/test/abc.txt","/test")</param>
        /// <returns></returns>
        public bool IsExists(string remotePath)
        {
            bool boolResult = false;

            try
            {
                if (Connect())
                {
                    boolResult = sftpClient.Exists(remotePath);
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(this, "IsExists->判断文件或目录是否存在 异常:" + ex.ToString() + "|*|remotePath:" + remotePath);
            }
            finally
            {
                DisConnect();
            }

            return(boolResult);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 新建目录
        /// </summary>
        /// <param name="remoteDic">远程目录("/test")</param>
        /// <returns></returns>
        public bool MakeDir(string remoteDic)
        {
            bool boolResult = false;

            try
            {
                if (Connect())
                {
                    sftpClient.CreateDirectory(remoteDic);

                    boolResult = true;
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(this, "MakeDir->新建目录 异常:" + ex.ToString() + "|*|remoteDic:" + remoteDic);
            }
            finally
            {
                DisConnect();
            }

            return(boolResult);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 删除文件或目录
        /// </summary>
        /// <param name="remotePath">远程路径("/test/abc.txt","/test")</param>
        /// <returns></returns>
        public bool Delete(string remotePath)
        {
            bool boolResult = false;

            try
            {
                if (Connect())
                {
                    sftpClient.Delete(remotePath);

                    boolResult = true;
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(this, "Delete->删除文件或目录 异常:" + ex.ToString() + "|*|remotePath:" + remotePath);
            }
            finally
            {
                DisConnect();
            }

            return(boolResult);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 异/同步发送邮件
        /// 注:如果是页面调用异步方法,则需要设置页面 Page 的属性 Async="true"
        /// </summary>
        /// <param name="strMailFromAddress">发件人邮箱地址</param>
        /// <param name="strMailDisplayName">发件人显示名称</param>
        /// <param name="strMailFromPwd">发件人邮箱密码</param>
        /// <param name="arrMailToAddress">收件人邮箱地址</param>
        /// <param name="arrMailCcAddress">抄送人邮箱地址(可为 null)</param>
        /// <param name="arrMailBccAddress">密送人邮箱地址(可为 null)</param>
        /// <param name="strSmtpServer">Smtp服务器地址("mail.finchina.com")</param>
        /// <param name="strSubject">邮件标题</param>
        /// <param name="strBody">邮件正文(可为 空)</param>
        /// <param name="arrMailAttachment">附件(可为 null)</param>
        /// <param name="boolIsBodyHtml">邮件正文是否HTML格式(false|true)</param>
        /// <param name="mailPriority">邮件优先级(MailPriority.Low|MailPriority.Normal|MailPriority.High)</param>
        /// <param name="mailEncoding">邮件标题和正文编码(可为 null;为 null时默认System.Text.Encoding.UTF8)</param>
        /// <param name="Async">是否以异步发送邮件(true|false)</param>
        public static bool SendMail(string strMailFromAddress, string strMailDisplayName, string strMailFromPwd, string[] arrMailToAddress, string[] arrMailCcAddress, string[] arrMailBccAddress, string strSmtpServer, string strSubject, string strBody, string[] arrMailAttachment, bool boolIsBodyHtml, MailPriority mailPriority, Encoding mailEncoding, bool Async)
        {
            bool bool_Result = false;

            #region 参数验证

            //发件人地址不能为空
            if (string.IsNullOrEmpty(strMailFromAddress))
            {
                Log4NetUtil.Error(typeof(MailUtil), "发件人地址不能为空");
                return(bool_Result);
            }

            //发件人显示名称为空时,默认显示发件人地址
            if (string.IsNullOrEmpty(strMailDisplayName))
            {
                strMailDisplayName = strMailFromAddress;
            }

            //发件人密码不能为空
            if (string.IsNullOrEmpty(strMailFromPwd))
            {
                Log4NetUtil.Error(typeof(MailUtil), "发件人密码不能为空");
                return(bool_Result);
            }

            //收件人地址不能为空
            if (arrMailToAddress == null)
            {
                Log4NetUtil.Error(typeof(MailUtil), "收件人地址不能为空");
                return(bool_Result);
            }

            //Smtp服务器地址不能为空
            if (string.IsNullOrEmpty(strSmtpServer))
            {
                Log4NetUtil.Error(typeof(MailUtil), "Smtp服务器地址不能为空");
                return(bool_Result);
            }

            //邮件标题不能为空
            if (string.IsNullOrEmpty(strSubject))
            {
                Log4NetUtil.Error(typeof(MailUtil), "邮件标题不能为空");
                return(bool_Result);
            }

            //默认邮件编码为 UTF8
            if (mailEncoding == null)
            {
                mailEncoding = System.Text.Encoding.UTF8;
            }

            //默认邮件正文不是HTML格式
            //if (boolIsBodyHtml == null)
            //{
            //    boolIsBodyHtml = false;
            //}

            //默认邮件优先级为 正常
            //if (mailPriority == null)
            //{
            //    mailPriority = MailPriority.Normal;
            //}

            //默认以异步方式发送邮件
            //if (Async == null)
            //{
            //    Async = true;
            //}
            #endregion

            #region MailMessage

            MailMessage mailMsg = new MailMessage();

            //发件人地址
            try
            {
                mailMsg.From = new MailAddress(strMailFromAddress, strMailDisplayName, mailEncoding);
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(typeof(MailUtil), ex.ToString());
                return(bool_Result);
            }

            //收件人地址
            for (int i = 0; i < arrMailToAddress.Length; i++)
            {
                mailMsg.To.Add(arrMailToAddress[i]);
            }

            //抄送人地址
            if (arrMailCcAddress != null)
            {
                for (int i = 0; i < arrMailCcAddress.Length; i++)
                {
                    mailMsg.CC.Add(arrMailCcAddress[i]);
                }
            }

            //密送人地址
            if (arrMailBccAddress != null)
            {
                for (int i = 0; i < arrMailBccAddress.Length; i++)
                {
                    mailMsg.Bcc.Add(arrMailBccAddress[i]);
                }
            }

            //邮件标题
            mailMsg.Subject = strSubject;

            //邮件标题编码
            mailMsg.SubjectEncoding = mailEncoding;

            //邮件正文
            mailMsg.Body = strBody;

            //邮件正文编码
            mailMsg.BodyEncoding = mailEncoding;

            //含有附件
            if (arrMailAttachment != null)
            {
                for (int i = 0; i < arrMailAttachment.Length; i++)
                {
                    mailMsg.Attachments.Add(new Attachment(arrMailAttachment[i]));
                }
            }

            //邮件优先级
            mailMsg.Priority = mailPriority;

            //邮件正文是否是HTML格式
            mailMsg.IsBodyHtml = boolIsBodyHtml;

            #endregion

            #region SmtpClient

            SmtpClient smtpClient = new SmtpClient();

            //验证发件人用户名和密码
            smtpClient.Credentials = new System.Net.NetworkCredential(strMailFromAddress, strMailFromPwd);

            //Smtp服务器地址
            smtpClient.Host = strSmtpServer;

            object userState = mailMsg;

            try
            {
                if (Async)
                {
                    //绑定回调函数
                    smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

                    //异步发送邮件
                    smtpClient.SendAsync(mailMsg, userState);
                    bool_Result = true;
                }
                else
                {
                    //同步发送邮件
                    smtpClient.Send(mailMsg);
                    bool_Result = true;
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(typeof(MailUtil), ex.ToString());
                return(false);
            }

            return(bool_Result);

            #endregion
        }
Ejemplo n.º 15
0
        /// <summary>
        /// HttpGet请求(同步,短连接)
        /// </summary>
        /// <param name="strUrl">Url</param>
        /// <param name="strContentType">ContentType(默认:text/html;charset=UTF-8)</param>
        /// <param name="strAccept">Accept(默认:application/json,text/javascript,*/*;q=0.01)</param>
        /// <param name="strUserAgent">UserAgent(默认:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36)</param>
        /// <param name="cookieCollection">CookieCollection</param>
        /// <param name="intTimeOut">TimeOut(默认:100秒)</param>
        /// <param name="strEncoding">Encoding(默认:utf-8)</param>
        /// <returns></returns>
        public static string HttpGet(string strUrl, string strContentType, string strAccept, string strUserAgent, CookieCollection cookieCollection, int intTimeOut, string strEncoding)
        {
            //响应字符串
            string strResult = string.Empty;

            if (string.IsNullOrEmpty(strUrl))
            {
                return(strResult);
            }
            if (string.IsNullOrEmpty(strEncoding))
            {
                //默认响应编码为 utf-8
                strEncoding = "utf-8";
            }

            HttpWebRequest request = null;

            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                return(strResult);
            }

            if (request != null)
            {
                //GET请求
                request.Method = "GET";
                //短连接
                request.KeepAlive = false;

                if (string.IsNullOrEmpty(strContentType))
                {
                    request.ContentType = @"text/html;charset=UTF-8";
                }
                else
                {
                    request.ContentType = strContentType;
                }

                if (string.IsNullOrEmpty(strAccept))
                {
                    //request.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Accept = @"application/json,text/javascript,*/*;q=0.01";
                }
                else
                {
                    request.Accept = strAccept;
                }

                if (string.IsNullOrEmpty(strUserAgent))
                {
                    request.UserAgent = @"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36";
                }
                else
                {
                    request.UserAgent = strUserAgent;
                }

                if (cookieCollection != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookieCollection);
                }

                //超时时间,单位:毫秒(最小10秒)
                if (intTimeOut < 10)
                {
                    request.Timeout          = 10 * 1000;
                    request.ReadWriteTimeout = 10 * 1000;
                }
                else
                {
                    request.Timeout          = intTimeOut * 1000;
                    request.ReadWriteTimeout = intTimeOut * 1000;
                }

                //AcceptEncoding
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

                //代理方式
                //WebProxy proxy = new WebProxy("127.0.0.1", 8000);//指定的ip和端口
                //request.Proxy = proxy;
                //request.AllowAutoRedirect = true;

                //取得响应数据
                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (Exception ex)
                {
                    Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                    request.Abort();
                    return(strResult);
                }

                if (response != null)
                {
                    byte[] buffer = new byte[4096];
                    int    size   = 0;
                    try
                    {
                        if (!string.IsNullOrEmpty(response.ContentEncoding))
                        {
                            if (response.ContentEncoding.ToLower().Contains("gzip"))
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (GZipStream stream = new GZipStream(rpsStream, CompressionMode.Decompress))
                                        {
                                            using (MemoryStream memoryStream = new MemoryStream())
                                            {
                                                while ((size = stream.Read(buffer, 0, buffer.Length)) > 0)
                                                {
                                                    memoryStream.Write(buffer, 0, size);
                                                }
                                                strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                            }
                                        }
                                    }
                                }
                            }
                            else if (response.ContentEncoding.ToLower().Contains("deflate"))
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (DeflateStream stream = new DeflateStream(rpsStream, CompressionMode.Decompress))
                                        {
                                            using (MemoryStream memoryStream = new MemoryStream())
                                            {
                                                while ((size = stream.Read(buffer, 0, buffer.Length)) > 0)
                                                {
                                                    memoryStream.Write(buffer, 0, size);
                                                }
                                                strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (MemoryStream memoryStream = new MemoryStream())
                                        {
                                            while ((size = rpsStream.Read(buffer, 0, buffer.Length)) > 0)
                                            {
                                                memoryStream.Write(buffer, 0, size);
                                            }
                                            strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            using (Stream rpsStream = response.GetResponseStream())
                            {
                                if (rpsStream != null)
                                {
                                    using (MemoryStream memoryStream = new MemoryStream())
                                    {
                                        while ((size = rpsStream.Read(buffer, 0, buffer.Length)) > 0)
                                        {
                                            memoryStream.Write(buffer, 0, size);
                                        }
                                        strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                        request.Abort();
                        return(string.Empty);
                    }
                    finally
                    {
                        if (response != null)
                        {
                            response.Close();
                        }
                        if (request != null)
                        {
                            request.Abort();
                        }
                    }
                }
            }
            return(strResult);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// HttpPost请求(同步,短连接)
        /// </summary>
        /// <param name="strUrl">Url</param>
        /// <param name="strPostData">Post数据</param>
        /// <param name="strContentType">ContentType(默认:application/x-www-form-urlencoded)</param>
        /// <param name="strUserAgent">UserAgent(默认:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36)</param>
        /// <param name="strAccept">Accept(默认:application/json,text/javascript,*/*;q=0.01)</param>
        /// <param name="cookieCollection">CookieCollection</param>
        /// <param name="intTimeOut">TimeOut(默认:30秒)</param>
        /// <param name="strEncoding">Encoding(默认:utf-8)</param>
        /// <returns></returns>
        public static string HttpPost(string strUrl, string strPostData, string strContentType, string strUserAgent, string strAccept, CookieCollection cookieCollection, int intTimeOut, string strEncoding)
        {
            //响应字符串
            string strResult = string.Empty;

            if (string.IsNullOrEmpty(strUrl))
            {
                return(strResult);
            }
            if (string.IsNullOrEmpty(strEncoding))
            {
                //默认响应编码为 utf-8
                strEncoding = "utf-8";
            }

            HttpWebRequest request = null;

            try
            {
                if (strUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = WebRequest.Create(strUrl) as HttpWebRequest;
                    request.ProtocolVersion = HttpVersion.Version10;
                }
                else
                {
                    request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                }
            }
            catch (Exception ex)
            {
                Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                return(strResult);
            }

            if (request != null)
            {
                //POST请求
                request.Method    = "POST";
                request.KeepAlive = false;
                //request.ServicePoint.Expect100Continue = false;

                if (string.IsNullOrEmpty(strContentType))
                {
                    request.ContentType = @"application/x-www-form-urlencoded";
                }
                else
                {
                    request.ContentType = strContentType;
                }

                if (string.IsNullOrEmpty(strUserAgent))
                {
                    request.UserAgent = @"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36";
                }
                else
                {
                    request.UserAgent = strUserAgent;
                }

                if (string.IsNullOrEmpty(strAccept))
                {
                    //Requester.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Accept = @"application/json,text/javascript,*/*;q=0.01";
                }
                else
                {
                    request.Accept = strAccept;
                }

                if (cookieCollection != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookieCollection);
                }

                //超时时间,单位:毫秒(默认30秒)
                if (intTimeOut < 30)
                {
                    request.Timeout          = 30 * 1000;
                    request.ReadWriteTimeout = 30 * 1000;
                }
                else
                {
                    request.Timeout          = intTimeOut * 1000;
                    request.ReadWriteTimeout = intTimeOut * 1000;
                }

                //AcceptEncoding
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

                //代理方式
                //WebProxy proxy = new WebProxy("127.0.0.1", 8000);//指定的ip和端口
                //request.Proxy = proxy;
                //request.AllowAutoRedirect = true;

                //Post数据
                if (!string.IsNullOrEmpty(strPostData))
                {
                    //参数编码
                    //byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
                    byte[] data = Encoding.GetEncoding(strEncoding).GetBytes(strPostData);

                    using (Stream writeStream = request.GetRequestStream())
                    {
                        if (writeStream != null)
                        {
                            writeStream.Write(data, 0, data.Length);
                        }
                    }
                }

                //取得响应数据
                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (Exception ex)
                {
                    Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                    request.Abort();
                    return(strResult);
                }

                if (response != null)
                {
                    byte[] buffer = new byte[4096];
                    int    size   = 0;
                    try
                    {
                        if (!string.IsNullOrEmpty(response.ContentEncoding))
                        {
                            if (response.ContentEncoding.ToLower().Contains("gzip"))
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (GZipStream stream = new GZipStream(rpsStream, CompressionMode.Decompress))
                                        {
                                            using (MemoryStream memoryStream = new MemoryStream())
                                            {
                                                while ((size = stream.Read(buffer, 0, buffer.Length)) > 0)
                                                {
                                                    memoryStream.Write(buffer, 0, size);
                                                }
                                                strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                            }
                                        }
                                    }
                                }
                            }
                            else if (response.ContentEncoding.ToLower().Contains("deflate"))
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (DeflateStream stream = new DeflateStream(rpsStream, CompressionMode.Decompress))
                                        {
                                            using (MemoryStream memoryStream = new MemoryStream())
                                            {
                                                while ((size = stream.Read(buffer, 0, buffer.Length)) > 0)
                                                {
                                                    memoryStream.Write(buffer, 0, size);
                                                }
                                                strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                using (Stream rpsStream = response.GetResponseStream())
                                {
                                    if (rpsStream != null)
                                    {
                                        using (MemoryStream memoryStream = new MemoryStream())
                                        {
                                            while ((size = rpsStream.Read(buffer, 0, buffer.Length)) > 0)
                                            {
                                                memoryStream.Write(buffer, 0, size);
                                            }
                                            strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            using (Stream rpsStream = response.GetResponseStream())
                            {
                                if (rpsStream != null)
                                {
                                    using (MemoryStream memoryStream = new MemoryStream())
                                    {
                                        while ((size = rpsStream.Read(buffer, 0, buffer.Length)) > 0)
                                        {
                                            memoryStream.Write(buffer, 0, size);
                                        }
                                        strResult = Encoding.GetEncoding(strEncoding).GetString(memoryStream.ToArray());
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log4NetUtil.Error(typeof(HttpUtil), ex.ToString());
                        request.Abort();
                        return(string.Empty);
                    }
                    finally
                    {
                        if (response != null)
                        {
                            response.Close();
                        }
                        if (request != null)
                        {
                            request.Abort();
                        }
                    }
                }
            }
            return(strResult);
        }