首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于 GetHttp 以及System.IO.Stream解决方法

2012-10-20 
关于 GetHttp 以及System.IO.StreamC# codepublic static Listint GetHttp (string httpUrl){Listint

关于 GetHttp 以及System.IO.Stream

C# code
        public static List<int> GetHttp (string httpUrl)        {            List<int> result = new List<int>();             HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create (httpUrl);            myReq.Method = "GET";            try            {                                HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse ();                System.IO.Stream myStream = HttpWResp.GetResponseStream ();                int data = -1;                while ((data = myStream.ReadByte()) != -1)                {                    // 因为不清楚 myStream.Length 是否真实字节流长度. 所以用了动态数组List                    result.Add(data);                }            }            catch (Exception exp)            {                Debug.Log(exp.ToString());            }            return result ;         }


所以疑问: myStream.Length 获得的是真实长度吗? 其实我就是想获得 byte[] 返回值

[解决办法]
真实字节流长度应该通过response.ContentLength获取,我之前测试过,直接使用ResponseStream的Length会报错的,另外你每次只读一个字节,效率很差的

热点排行