日期:2014-05-17  浏览次数:20932 次

关于 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会报错的,另外你每次只读一个字节,效率很差的