コード例 #1
0
        //public static WebList Http(string _type, string _url, List<TItem> _header, object _conmand = null, Encode _Encode = Encode.UTF8)
        static WebList Http(string _type, string _url, List <TItem> _header, object _conmand, Encode _Encode)
        {
            Encoding       _encoding = Get_Encoding(_Encode);
            HttpWebRequest req       = Http_Core(_type, _url, _encoding, _header, _conmand);

            if (req != null)
            {
                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                    {
                        return(Http_DownSteam(response, _encoding));
                    }
                }
                catch (WebException err)
                {
                    var rsp = err.Response as HttpWebResponse;
                    if (rsp != null)
                    {
                        WebList _web = Http_DownSteam(rsp, _encoding);
                        _web.Err = err.Message;
                        rsp.Close(); rsp.Dispose();
                        return(_web);
                    }
                }
            }
            return(null);
        }
コード例 #2
0
 /// <summary>
 /// 将字节转为文本类型
 /// </summary>
 /// <param name="_byte">字节</param>
 /// <returns>返回文本</returns>
 public static string ToStringX(this WebList _web)
 {
     if (_web != null && _web.Byte != null)
     {
         return(_web.Encoding.GetString(_web.Byte));
     }
     return(null);
 }
コード例 #3
0
        /// <summary>
        /// 将字节转为json
        /// </summary>
        /// <typeparam name="T">JSON模型</typeparam>
        /// <param name="_byte">字节</param>
        /// <returns>返回json</returns>
        //public static T ToJson<T>(this WebList _web)
        //{
        //    if (_web != null && _web.Byte != null)
        //    {
        //        return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(_web.Encoding.GetString(_web.Byte));
        //    }
        //    return default(T);
        //}

        /// <summary>
        /// 将字节转为图片
        /// </summary>
        /// <param name="_byte">字节</param>
        /// <returns>返回图片</returns>
        public static System.Drawing.Image ToImage(this WebList _web)
        {
            if (_web != null && _web.Byte != null)
            {
                using (MemoryStream ms = new MemoryStream(_web.Byte))
                {
                    System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                    return(img);
                }
            }
            return(null);
        }
コード例 #4
0
        //public static WebList Http_NoData(string _type, string _url, List<TItem> _header, object _conmand = null, Encode _Encode = Encode.UTF8)
        public static WebList Http_NoData(string _type, string _url, List <TItem> _header, object _conmand, Encode _Encode)
        {
            Encoding       _encoding = Get_Encoding(_Encode);
            HttpWebRequest req       = Http_Core(_type, _url, _encoding, _header, _conmand);

            if (req != null)
            {
                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                    {
                        WebList _web = new WebList
                        {
                            StatusCode  = (int)response.StatusCode,
                            Type        = response.ContentType,
                            AbsoluteUri = response.ResponseUri.AbsoluteUri
                        };
                        string header = "";
                        foreach (string str in response.Headers.AllKeys)
                        {
                            if (str == "Set-Cookie")
                            {
                                _web.SetCookie = response.Headers[str];
                            }
                            else if (str == "Location")
                            {
                                _web.Location = response.Headers[str];
                            }
                            header = header + str + ":" + response.Headers[str] + "\r\n";
                        }
                        string cookie = "";
                        foreach (Cookie str in response.Cookies)
                        {
                            cookie = cookie + str.Name + "=" + str.Value + ";";
                        }

                        _web.Header = header;
                        _web.Cookie = cookie;
                        return(_web);
                    }
                }
                catch (WebException err)
                {
                    var rsp = err.Response as HttpWebResponse;
                    if (rsp != null)
                    {
                        WebList _web = new WebList
                        {
                            StatusCode  = (int)rsp.StatusCode,
                            Type        = rsp.ContentType,
                            AbsoluteUri = rsp.ResponseUri.AbsoluteUri
                        };
                        string header = "";
                        foreach (string str in rsp.Headers.AllKeys)
                        {
                            if (str == "Set-Cookie")
                            {
                                _web.SetCookie = rsp.Headers[str];
                            }
                            else if (str == "Location")
                            {
                                _web.Location = rsp.Headers[str];
                            }
                            header = header + str + ":" + rsp.Headers[str] + "\r\n";
                        }
                        string cookie = "";
                        foreach (Cookie str in rsp.Cookies)
                        {
                            cookie = cookie + str.Name + "=" + str.Value + ";";
                        }

                        _web.Header = header;
                        _web.Cookie = cookie;
                        return(_web);
                    }
                }
            }
            return(null);
        }
コード例 #5
0
        /// <summary>
        /// 数据流下载
        /// </summary>
        /// <param name="response"></param>
        /// <param name="_web"></param>
        /// <param name="encoding"></param>
        static WebList Http_DownSteam(HttpWebResponse response, Encoding encoding)
        {
            WebList _web = new WebList
            {
                Encoding    = encoding,
                StatusCode  = (int)response.StatusCode,
                Type        = response.ContentType,
                AbsoluteUri = response.ResponseUri.AbsoluteUri
            };
            string header = "";

            foreach (string str in response.Headers.AllKeys)
            {
                if (str == "Set-Cookie")
                {
                    _web.SetCookie = response.Headers[str];
                }
                else if (str == "Location")
                {
                    _web.Location = response.Headers[str];
                }
                header = header + str + ":" + response.Headers[str] + "\r\n";
            }
            string cookie = "";

            foreach (Cookie str in response.Cookies)
            {
                cookie = cookie + str.Name + "=" + str.Value + ";";
            }

            _web.Header = header;
            _web.Cookie = cookie;

            #region  载流
            using (Stream stream = response.GetResponseStream())
            {
                using (MemoryStream file = new MemoryStream())
                {
                    int    _value = 0;
                    byte[] _cache = new byte[1024];
                    int    osize  = stream.Read(_cache, 0, 1024);

                    while (osize > 0)
                    {
                        _value += osize;

                        file.Write(_cache, 0, osize);
                        osize = stream.Read(_cache, 0, 1024);
                    }

                    file.Seek(0, SeekOrigin.Begin);
                    byte[] _byte = new byte[_value];
                    file.Read(_byte, 0, _value);

                    file.Seek(0, SeekOrigin.Begin);
                    string fileclass = GetFileClass(file);
                    if (fileclass == "31139")
                    {
                        //_web.OriginalSize = _byte.Length;
                        _web.Byte = Decompress(_byte);
                    }
                    else
                    {
                        _web.Byte = _byte;
                    }
                }
            }
            #endregion
            return(_web);
        }