首页 新闻 搜索 专区 学院

HttpWebRequest的GetRequestStream和GetResponseStream方法!!!

0
悬赏园豆:40 [已关闭问题] 关闭于 2015-10-13 18:39

Stream writer = request.GetRequestStream()

GetRequestStream()总提示System.NotSupportedException 类型的异常

 

WebResponse response = webrequest.GetResponse();
 Stream s = response.GetResponseStream();

GetResponseStream()总提示System.NotSupportedException 类型的异常

flyfish1986的主页 flyfish1986 | 初学一级 | 园豆:6
提问于:2013-07-15 17:51
< >
分享
所有回答(1)
2
        public static byte[] Get(string url, Action<int> onPercentChange = null) {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Timeout = kRequestTimeout;
            request.Method = "GET";
            request.Referer = url;
            request.CookieContainer = _cookie;

            return Download(request, onPercentChange);
        }

        private static byte[] Download(HttpWebRequest request, Action<int> onPercentChange) {

            
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            long length = response.ContentLength;
            int bytePerTime = (int)(length / 100 + 1);

            byte[] receiveBytes = new byte[bytePerTime * 100];

            BinaryReader br = new BinaryReader(response.GetResponseStream());

            

            for (int k = 0; k < 100; k++) {
                byte[] buffer = br.ReadBytes(bytePerTime);
                Array.Copy(buffer, 0, receiveBytes, bytePerTime * k, buffer.Length);
                if (onPercentChange != null)
                    onPercentChange(k);
            }

            br.Close();
            response.Close();

            return receiveBytes;
        }
悟了 | 园豆:521 (小虾三级) | 2013-07-15 18:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册