Ejemplo n.º 1
0
        protected byte[] DownloadCore()
        {
            WebRequest  Web;
            WebResponse Rsp;

            System.IO.Stream Stm;

            Web = WebRequest.Create(_url);

            HttpRequest Request = HttpContext.Current.Request;

            string cookies = Request.Headers["Cookie"];

            Web.Headers.Add(HttpRequestHeader.Cookie, cookies);
            ((HttpWebRequest)Web).UserAgent = @"Mozilla/4.0 (compatible; Shotgun Webdownloader 1.0; Windows NT 5.1; )";

            if (_postdata != null)
            {//以post方式提交
                Web.Method = "POST";
                if (_Encoder == null)
                {
                    _Encoder = System.Text.ASCIIEncoding.UTF8;
                }
                ((HttpWebRequest)Web).ServicePoint.Expect100Continue = false;
                //Web.Headers[HttpRequestHeader.ContentLength] = _Encoder.EncodingName;
                Web.Headers[HttpRequestHeader.ContentEncoding] = _Encoder.WebName;
                ((HttpWebRequest)Web).ContentType = @"application/x-www-form-urlencoded";
                StreamWriter pdStm = new StreamWriter(Web.GetRequestStream(), _Encoder);
                pdStm.Write(_postdata);
                pdStm.Close();
                pdStm.Dispose();
            }

            //Web.Timeout = _timeOut;

            try
            {
                Rsp = Web.GetResponse();
            }
            catch (Exception ex)
            {
                if (Request.IsLocal)
                {
                    throw ex;
                }
                return(null);
            }

            int MaxLength, RecvicedLength, r;

            MaxLength      = (int)Rsp.ContentLength;
            RecvicedLength = 0;
            byte[] bin = new byte[MaxLength];

            Stm = Rsp.GetResponseStream();

            do
            {
                r = Stm.Read(bin, RecvicedLength, MaxLength - RecvicedLength);
                RecvicedLength += r;
            }while (RecvicedLength < MaxLength);
            Stm.Close();
            Stm.Dispose();
            Rsp.Close();
            return(bin);
        }