예제 #1
0
        /// <summary>
        /// 封装的参数化Http请求 异步  调用时请向 HttpMethod.EndResopnseMethod事件 注册获得响应后需要执行的方法
        /// </summary>
        /// <param name="httpInfo"></param>
        /// <returns></returns>
        public static String HttpWork_Async(HttpInfo Httpinfo, object[] args)
        {
            //待返回的网页源代码
            String         retString = String.Empty;
            HttpWebRequest request   = null;

            try
            {
                request = Httpinfo.CreatRequest();
                if (request != null)
                {
                    HttpInfo info = (HttpInfo)DeepCopy(Httpinfo);
                    args[1] = info;
                    object[] oj  = { request, DateTime.Now };
                    object[] foj = new object[oj.Length + args.Length];
                    oj.CopyTo(foj, 0);
                    args.CopyTo(foj, oj.Length);

                    request.BeginGetResponse(EndResopnseCallBack, foj);

                    foj  = null;
                    oj   = null;
                    args = null;
                }
            }
            catch
            {
            }
            return(retString);
        }
예제 #2
0
        /// <summary>
        /// 构造HttpInfo方式下载文件 下载到指定目录
        /// </summary>
        /// <param name="DownLoadUrl">下载地址</param>
        /// <param name="SavePath">文件目录</param>
        /// <param name="FileName">保存文件名</param>
        /// <returns></returns>
        public static bool DownLoadFile(HttpInfo info, string FloderPath, String FileName = "")
        {
            Regex filename_reg = new Regex("(&ldquo;)|(\\r)|(\\n)|(&nbsp;)|(\\t)|\\.|\"|/");

            #region 配置文件路径
            //去除非法字符和空格
            FileName = filename_reg.Replace(FileName, String.Empty);
            //文件后缀名 从文件地址后缀中获取
            String filetype = Path.GetExtension(info.RequestUrl);
            //如果文件名中没有后缀名
            if (!String.IsNullOrEmpty(Path.GetExtension(FileName)))
            {
                //拼接后缀名
                FileName = FileName + filetype;
            }
            //获得下载地址中匹配到文件名
            String filename = Path.GetFileName(info.RequestUrl);
            //形成最终保存路径 如果参数中的文件名是空的 则使用自动匹配到的文件路径  反之使用输入的文件名
            String FinalPath = Path.Combine(FloderPath, filename);
            #endregion

            try
            {
                var Myrq = info.CreatRequest();
                if (Myrq != null)
                {
                    using (System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse())
                    {
                        #region  载
                        long     totalBytes = myrp.ContentLength;
                        object[] args       = { (int)totalBytes, 0 };

                        System.IO.Stream st = myrp.GetResponseStream();
                        System.IO.Stream so = new System.IO.FileStream(FinalPath, System.IO.FileMode.Create);
                        try
                        {
                            long   totalDownloadedByte = 0;
                            byte[] by    = new byte[1024];
                            int    osize = st.Read(by, 0, (int)by.Length);
                            while (osize > 0)
                            {
                                int taskosiz = osize;
                                totalDownloadedByte = osize + totalDownloadedByte;
                                so.Write(by, 0, osize);
                                osize = st.Read(by, 0, (int)by.Length);
                            }
                        }
                        catch { }
                        #endregion
                    }
                }
            }
            catch (System.Exception)
            {
                return(false);
            }
            return(true);
        }
예제 #3
0
파일: HttpMethod.cs 프로젝트: jx-qyj/Tools
        /// <summary>
        /// 使用HttpInfo下载文件 下载到绝对路径
        /// </summary>
        /// <param name="DownLoadUrl">下载类型</param>
        /// <param name="SavePath">绝对路径</param>
        /// <returns></returns>
        public static bool DownLoadFile_ABPath(HttpInfo info, string SavePath)
        {
            #region 配置文件路径
            String FinalPath = SavePath;
            #endregion

            try
            {
                var Myrq = info.CreatRequest();
                if (Myrq != null)
                {
                    using (System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse())
                    {
                        #region  载
                        long     totalBytes = myrp.ContentLength;
                        object[] args       = { (int)totalBytes, 0 };

                        System.IO.Stream st = myrp.GetResponseStream();
                        System.IO.Stream so = new System.IO.FileStream(FinalPath, System.IO.FileMode.Create);
                        try
                        {
                            long   totalDownloadedByte = 0;
                            byte[] by    = new byte[1024];
                            int    osize = st.Read(by, 0, (int)by.Length);
                            while (osize > 0)
                            {
                                int taskosiz = osize;
                                totalDownloadedByte = osize + totalDownloadedByte;
                                so.Write(by, 0, osize);
                                osize = st.Read(by, 0, (int)by.Length);
                            }
                        }
                        catch { }
                        finally
                        {
                            so.Dispose();
                            so.Close();
                            st.Dispose();
                            st.Close();
                        }
                        #endregion
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (System.Exception)
            {
                return(false);
            }
            return(true);
        }
예제 #4
0
파일: HttpMethod.cs 프로젝트: jx-qyj/Tools
        /// <summary>
        /// 获得重定向的Url地址 基于Httpinfo
        /// </summary>
        /// <param name="tempinfo">配置完毕的HttpInfo对象</param>
        /// <returns>网页源代码</returns>
        public static string GetRedirectUrl(HttpInfo tempinfo)
        {
            tempinfo.AllowAutoRedirect = false;
            var    req   = tempinfo.CreatRequest();
            String reurl = tempinfo.RequestUrl;

            using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
            {
                var stream = resp.GetResponseStream();
                reurl = resp.Headers["Location"].ToString();
                stream.Close();
            }

            if (String.IsNullOrEmpty(reurl))
            {
                return(tempinfo.RequestUrl);
            }
            else
            {
                return(reurl);
            }
        }
예제 #5
0
파일: HttpMethod.cs 프로젝트: jx-qyj/Tools
        /// <summary>
        /// 下载图片 使用HttpInfo
        /// </summary>
        /// <param name="info">配置完毕的HttpInfo对象</param>
        /// <returns>Image对象</returns>
        public static Image DownPic(HttpInfo info)
        {
            Image img = null;

            try
            {
                var request = info.CreatRequest();
                if (request != null)
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        Stream myResponseStream = response.GetResponseStream();
                        var    bytes            = GetMemoryStream(myResponseStream).ToArray();
                        img = ByteToImage(bytes);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(img);
        }
예제 #6
0
파일: HttpMethod.cs 프로젝트: jx-qyj/Tools
        /// <summary>
        /// 封装的Http参数化请求  改变Cookie
        /// </summary>
        /// <param name="Httpinfo"></param>
        /// <returns>返回的源代码</returns>
        public static string HttpWork(ref HttpInfo Httpinfo)
        {
            Encoding        encoding  = Encoding.UTF8;
            String          retString = null;
            HttpWebResponse response  = null;
            MemoryStream    _stream   = new MemoryStream();
            var             request   = Httpinfo.CreatRequest();

            if (request == null && Httpinfo.CheckUrl)
            {
                retString = "Url校验未通过";
            }
            try
            {
                if (request != null)
                {
                    if (Httpinfo.UseTaskTimeOut && !String.IsNullOrEmpty(Httpinfo.Ip))
                    {
                        var a          = request.BeginGetResponse(null, null);
                        var waitResult = a.AsyncWaitHandle.WaitOne(Httpinfo.Timeout + Httpinfo.ReadWriteTimeout);
                        if (waitResult && a.IsCompleted)
                        {
                            response = request.EndGetResponse(a) as HttpWebResponse;
                        }
                        else
                        {
                            response = null;
                        }
                    }
                    else
                    {
                        response = request.GetResponse() as HttpWebResponse;
                    }
                    if (response == null)
                    {
                        retString = String.Empty;
                    }
                    else
                    {
                        //GZIIP处理
                        if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        {
                            //开始读取流并设置编码方式
                            //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                            //.net4.0以下写法
                            _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
                        }
                        else
                        {
                            //开始读取流并设置编码方式
                            //response.GetResponseStream().CopyTo(_stream, 10240);
                            //.net4.0以下写法
                            _stream = GetMemoryStream(response.GetResponseStream());
                        }
                        //获取Byte
                        byte[] RawResponse = _stream.ToArray();
                        _stream.Close();
                        if (Httpinfo.Encoding != null)
                        {
                            encoding = Httpinfo.Encoding;
                        }
                        else
                        {
                            //从这里开始我们要无视编码了
                            Match  meta    = Regex.Match(Encoding.Default.GetString(RawResponse), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
                            string charter = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower() : string.Empty;
                            charter = charter.Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk");
                            if (string.IsNullOrEmpty(response.CharacterSet))
                            {
                                try
                                {
                                    encoding = Encoding.GetEncoding(charter.Trim());
                                }
                                catch
                                {
                                    encoding = Encoding.UTF8;
                                }
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(response.CharacterSet))
                                {
                                    try
                                    {
                                        encoding = Encoding.GetEncoding(charter.Trim());
                                    }
                                    catch
                                    {
                                        encoding = Encoding.UTF8;
                                    }
                                }
                                else if (response.CharacterSet.ToLower().Contains("iso-8859-1"))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(response.CharacterSet);
                                }
                            }
                        }
                        //得到返回的HTML
                        retString = encoding.GetString(RawResponse);


                        if (String.IsNullOrEmpty(Httpinfo.Cookie.BaseCookieStr))
                        {
                            Httpinfo.Cookie.AddCookie(response.Headers[HttpResponseHeader.SetCookie]);
                        }
                        else
                        {
                            Httpinfo.Cookie.BaseCookieStr = response.Headers[HttpResponseHeader.SetCookie];
                        }
                    }
                }
            }
            catch (System.Net.WebException e)
            {
                if (Httpinfo.IgnoreWebException)
                {
                    response = (HttpWebResponse)e.Response;
                    Httpinfo.Cookie.AddCookie(response.Headers[HttpResponseHeader.SetCookie]);
                    using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        retString = sr.ReadToEnd();
                    }
                }
                else
                {
                    return(e.Message);
                }
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                    request = null;
                }
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (_stream != null)
                {
                    _stream.Close();
                    _stream.Dispose();
                    _stream = null;
                }
            }
            return(retString);
        }
예제 #7
0
        /// <summary>
        /// 封装的Http参数化请求
        /// </summary>
        /// <param name="Httpinfo">携带请求参数的HttpInfo参数</param>
        /// <returns>返回的源代码</returns>
        public static string HttpWork(HttpInfo Httpinfo)
        {
            //默认的编码为UTF-8
            Encoding encoding = Encoding.UTF8;
            //待返回的网页源代码
            String          retString = String.Empty;
            HttpWebRequest  request   = null;
            HttpWebResponse response  = null;
            MemoryStream    _stream   = new MemoryStream();

            try
            {
                request = Httpinfo.CreatRequest();
                if (request != null)
                {
                    if (Httpinfo.UseTaskTimeOut && !String.IsNullOrEmpty(Httpinfo.Ip))
                    {
                        var a          = request.BeginGetResponse(null, null);
                        var waitResult = a.AsyncWaitHandle.WaitOne(Httpinfo.Timeout + Httpinfo.WriteTimeout);
                        if (waitResult && a.IsCompleted)
                        {
                            response = request.EndGetResponse(a) as HttpWebResponse;
                        }
                        else
                        {
                            response = null;
                        }
                    }
                    else
                    {
                        response = request.GetResponse() as HttpWebResponse;
                    }

                    if (response == null)
                    {
                        retString = String.Empty;
                    }
                    else
                    {
                        //GZIIP处理
                        if (response.ContentEncoding != null && response.ContentEncoding.ToLower().Contains("gzip"))
                        {
                            //开始读取流并设置编码方式
                            //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                            //.net4.0以下写法
                            _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
                        }
                        else
                        {
                            //开始读取流并设置编码方式
                            //response.GetResponseStream().CopyTo(_stream, 10240);
                            //.net4.0以下写法
                            _stream = GetMemoryStream(response.GetResponseStream());
                        }
                        //获取Byte
                        byte[] RawResponse = _stream.ToArray();
                        _stream.Close();
                        if (Httpinfo.Encoding != null)
                        {
                            encoding = Httpinfo.Encoding;
                        }
                        else
                        {
                            //从这里开始我们要无视编码了
                            Match  meta    = Regex.Match(Encoding.Default.GetString(RawResponse), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
                            string charter = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower() : string.Empty;
                            charter = charter.Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk");
                            if (charter.Length > 2)
                            {
                                encoding = Encoding.GetEncoding(charter.Trim());
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(response.CharacterSet))
                                {
                                    try
                                    {
                                        encoding = Encoding.GetEncoding(charter.Trim());
                                    }
                                    catch
                                    {
                                        encoding = Encoding.UTF8;
                                    }
                                }
                                else if (response.CharacterSet.ToLower().Contains("iso-8859-1"))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(response.CharacterSet);
                                }
                            }
                        }
                        //得到返回的HTML
                        retString = encoding.GetString(RawResponse);
                    }
                }
            }
            catch (System.Net.WebException e)
            {
                //Console.WriteLine(e.Message);
                return(e.Message);
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                    request = null;
                }
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (_stream != null)
                {
                    _stream.Close();
                    _stream.Dispose();
                    _stream = null;
                }
            }
            return(retString);
        }
예제 #8
0
        /// <summary>
        /// 封装的Http参数化请求
        /// </summary>
        /// <param name="Httpinfo">携带请求参数的HttpInfo参数</param>
        /// <returns></returns>
        public static string HttpWork(HttpInfo Httpinfo)
        {
            //默认的编码为UTF-8
            Encoding encoding = Encoding.UTF8;
            //待返回的网页源代码
            String retString = String.Empty;

            try
            {
                var request = Httpinfo.CreatRequest();
                if (request != null)
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        MemoryStream _stream = new MemoryStream();
                        //GZIIP处理
                        if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        {
                            //开始读取流并设置编码方式
                            //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                            //.net4.0以下写法
                            _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
                        }
                        else
                        {
                            //开始读取流并设置编码方式
                            //response.GetResponseStream().CopyTo(_stream, 10240);
                            //.net4.0以下写法
                            _stream = GetMemoryStream(response.GetResponseStream());
                        }
                        //获取Byte
                        byte[] RawResponse = _stream.ToArray();
                        _stream.Close();
                        if (Httpinfo.Encoding != null)
                        {
                            encoding = Httpinfo.Encoding;
                        }
                        else
                        {
                            //从这里开始我们要无视编码了
                            Match  meta    = Regex.Match(Encoding.Default.GetString(RawResponse), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
                            string charter = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower() : string.Empty;
                            charter = charter.Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk");
                            if (charter.Length > 2)
                            {
                                encoding = Encoding.GetEncoding(charter.Trim());
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(response.CharacterSet))
                                {
                                    try
                                    {
                                        encoding = Encoding.GetEncoding(charter.Trim());
                                    }
                                    catch
                                    {
                                        encoding = Encoding.UTF8;
                                    }
                                }
                                else if (response.CharacterSet.ToLower().Contains("iso-8859-1"))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(response.CharacterSet);
                                }
                            }
                        }
                        //得到返回的HTML
                        retString = encoding.GetString(RawResponse);
                    }
                }
            }

            catch (System.Net.WebException e)
            {
                //Console.WriteLine(e.Message);
                return(e.Message);
            }
            return(retString);
        }