AddRange() private method

private AddRange ( string rangeSpecifier, string from, string to ) : bool
rangeSpecifier string
from string
to string
return bool
Exemplo n.º 1
1
        /// <summary>
        /// Download a file from the specified url and copy it to the specified path
        /// </summary>
        private void DownloadFile(string url, string path, long startPoint)
        {
            int startingPoint = Convert.ToInt32(startPoint);

            try
            {
                //For using untrusted SSL Certificates
                ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(OnCheckRemoteCallback);

                _Request = (HttpWebRequest)HttpWebRequest.Create(url);
                _Request.AddRange(startingPoint);

                if (!string.IsNullOrEmpty(LoginUsername))
                {
                    _Request.Credentials = new NetworkCredential(LoginUsername, LoginPassword);
                }
                else
                {
                    _Request.Credentials = CredentialCache.DefaultCredentials;
                }

                if (ProxyEnabled)
                {
                    _Request.Proxy = new WebProxy(ProxyURL);
                }

                _Response = (HttpWebResponse)_Request.GetResponse();
                Stream responseSteam = _Response.GetResponseStream();

                if (startingPoint == 0)
                {
                    _SaveFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                }
                else
                {
                    _SaveFileStream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                }

                int bytesSize;
                long fileSize = _Response.ContentLength;
                byte[] downloadBuffer = new byte[DEF_PACKET_LENGTH];

                OnProgressChanged(new DownloadProgressEventArgs(fileSize, 0, "Starting the download..."));

                while ((bytesSize = responseSteam.Read(downloadBuffer, 0, downloadBuffer.Length)) > 0 && !_ExitThread)
                {
                    if (_ExitThread)
                    {
                        break;
                    }

                    _SaveFileStream.Write(downloadBuffer, 0, bytesSize);
                    OnProgressChanged(new DownloadProgressEventArgs(_SaveFileStream.Length, fileSize + startingPoint, "Download in progress..."));
                }

                if (_ExitThread)
                {
                    _SaveFileStream.Close();
                    _SaveFileStream.Dispose();
                    OnDownloadCompleted(new DownloadCompleteEventArgs(false, true));

                    return;
                }

                OnDownloadCompleted(new DownloadCompleteEventArgs(true, false));
            }
            catch (Exception ex)
            {
                OnDownloadError(new DownloadErrorEventArgs(ex, string.Format("Problem downloading and copying file from {0} to {1}.", url, path)));
                OnDownloadCompleted(new DownloadCompleteEventArgs(false, false));
            }
            finally
            {
                if (_SaveFileStream != null)
                {
                    _SaveFileStream.Close();
                    _SaveFileStream.Dispose();
                }

                if (_Response != null)
                {
                    _Response.Close();
                }
            }
        }
Exemplo n.º 2
0
        private static void AddRange(HttpWebRequest requestToAddUpon, KeyValuePair<string, string> headerPair)
        {
            var splitted = headerPair.Value.Split('-'); // Split from and to
            string part1 = splitted[0];

            string specifier = string.Empty;
            if (part1.Contains("=")) // range does sometimes have a specification part
            {
                var splitted2 = splitted[0].Split('=');
                specifier = splitted2[0];
                part1 = splitted2[1];
            }

            int fromPoint = Convert.ToInt32(part1);
            int toPoint = Convert.ToInt32(splitted[1]);

            if (string.IsNullOrEmpty(specifier))
            {
                requestToAddUpon.AddRange(fromPoint, toPoint);
            }
            else
            {
                requestToAddUpon.AddRange(specifier, fromPoint, toPoint);
            }
        }
Exemplo n.º 3
0
        public void restart()
        {
            Console.Out.WriteLine("restart");

            //If file exists, get the pointer of the stream
            if (File.Exists(localDir))
            {
                fs = File.OpenWrite(localDir);
                startPos = fs.Length;
                fs.Seek(startPos, SeekOrigin.Current);
                Console.Out.WriteLine("startPos: " + startPos.ToString());
            }
            else
            {

                fs = new FileStream(localDir, FileMode.Create);
                startPos = 0;

            }

            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create(url);

                //if there is an existing file, add range parameters to the request
                if (startPos > 0)
                {
                    request.AddRange((int)startPos);
                }

                responeStream = request.GetResponse().GetResponseStream();

                byte[] nBytes = new byte[512];
                int nReadSize = 0;
                nReadSize = responeStream.Read(nBytes, 0, 512);
                while (nReadSize > 0)
                {
                    fs.Write(nBytes, 0, nReadSize);
                    nReadSize = responeStream.Read(nBytes, 0, 512);
                    Console.Out.WriteLine(nReadSize.ToString());
                }

                fs.Close();
                responeStream.Close();
            }
            catch (Exception ex)
            {
                fs.Close();
                responeStream.Close();
                Console.Out.WriteLine("restart Exception");

                if (reConnectTimer == null)
                    reConnectTimer = new Timer();

                reConnectTimer.Interval = 3000;
                reConnectTimer.Elapsed += reConnectTimer_Elapsed;

                reConnectTimer.Start();
            }
        }
Exemplo n.º 4
0
 internal void AddHeaders(HttpWebRequest requestToAddUpon)
 {
     foreach (var headerPair in Headers)
     {
         if (!WebHeaderCollection.IsRestricted(headerPair.Key)) // Test if the header is restricted, if not we can just add it
         {
             requestToAddUpon.Headers.Add(headerPair.Key, headerPair.Value);
         }
         else
         {
             switch (headerPair.Key)
             {
                 case "Range":
                     if (headerPair.Value.Contains("-")) // This part is a bit of a gamble..
                     {
                         AddRange(requestToAddUpon, headerPair);
                     }
                     else
                     {
                         int range = Convert.ToInt32(headerPair.Value);
                         requestToAddUpon.AddRange(range);
                     }
                     break;
                 case "User-Agent":
                     requestToAddUpon.UserAgent = headerPair.Value;
                     break;
                 default:
                     throw new ApplicationException("Restricted header was found, but no implementation was made in lib to handle it.");
             }
         }
     }
 }
Exemplo n.º 5
0
        private bool downfile(string url, string LocalPath)
        {
            string StrUrl      = url;       //文件下载网址
            string StrFileName = LocalPath; //下载文件保存地址
            string strError;                //返回结果
            long   lStartPos   = 0;         //返回上次下载字节
            long   lCurrentPos = 0;         //返回当前下载字节
            long   lDownloadFile;           //返回当前下载文件长度

            System.IO.FileStream fs;
            if (System.IO.File.Exists(StrFileName))
            {
                System.IO.File.Delete(StrFileName);
                //lStartPos = fs.Length;
                //fs.Seek(lStartPos, System.IO.SeekOrigin.Current);
                //移动文件流中的当前指针
            }
            else
            {
                //fs = new System.IO.FileStream(StrFileName, System.IO.FileMode.Create);
                lStartPos = 0;
            }
            fs        = new System.IO.FileStream(StrFileName, System.IO.FileMode.Create);
            lStartPos = 0;
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
                long length = request.GetResponse().ContentLength;
                lDownloadFile = length;
                if (lStartPos > 0)
                {
                    request.AddRange((int)lStartPos); //设置Range值
                }
                //向服务器请求,获得服务器回应数据流
                System.IO.Stream ns = request.GetResponse().GetResponseStream();

                byte[] nbytes    = new byte[512];
                int    nReadSize = 0;
                nReadSize = ns.Read(nbytes, 0, 512);
                while (nReadSize > 0)
                {
                    fs.Write(nbytes, 0, nReadSize);
                    nReadSize   = ns.Read(nbytes, 0, 512);
                    lCurrentPos = fs.Length;
                }

                fs.Close();
                ns.Close();
                strError = "下载完成";

                return(true);
            }
            catch
            {
                fs.Close();

                return(false);
            }
        }
        public DocumentFormatReader(Uri address, OpenOptions options = null)
        {
            _request = (HttpWebRequest)WebRequest.Create(address);
            if (options != null)
            {
                if (options.SkipContent)
                {
                    _request.Method = WebRequestMethods.Http.Head;
                }

                if (options.RangeFrom.HasValue)
                {
                    if (options.RangeTo.HasValue)
                        _request.AddRange(options.RangeFrom.Value, options.RangeTo.Value);
                    else
                        _request.AddRange(options.RangeFrom.Value);
                }
            }
        }
Exemplo n.º 7
0
        //接收线程
        public void receive()
        {
            filename = formm.filenamew[threadh];
            strUrl = formm.strurl;
            ns = null;
            nbytes = new byte[512];
            nreadsize = 0;

            Console.WriteLine("线程" + threadh.ToString() + "开始接收");
            if (!File.Exists(filename))
            {
                fs = new FileStream(filename, System.IO.FileMode.Create);
            }
            else
            {
                fs = File.OpenWrite(filename);
                DownLoadSize = fs.Length;
                formm.DownLoadtotal += DownLoadSize;
                formm.filestartw[threadh] += DownLoadSize;
                fs.Seek(DownLoadSize, SeekOrigin.Current);   //已经存在
            }
            if (!formm.threadw[threadh])
            {
                try
              {
                 request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                //接收的起始位置及接收的长度
                 request.AddRange(formm.filestartw[threadh], formm.filestartw[threadh] + formm.filesizew[threadh]-DownLoadSize);
                 ns = request.GetResponse().GetResponseStream();//获得接收流
                 nreadsize = ns.Read(nbytes, 0, 512);

                 while (nreadsize > 0)
                 {
                    fs.Write(nbytes, 0, nreadsize);
                    nreadsize = ns.Read(nbytes, 0, 512);
                    formm.DownLoadtotal += 512;
                     Console.WriteLine("线程" + threadh.ToString() + "正在接收" + formm.DownLoadSpeed.ToString() + "k/s___已经下了" + formm.DownLoadtotal.ToString() + "还剩下" + formm.LeftDownLoadTime.ToString() + "s" + "百分比" + formm.DownLoadPercentage.ToString());
                 }
                 fs.Close();
                 ns.Close();
              }
               catch (Exception er)
              {
                Console.WriteLine("123");
                fs.Close();
              }
            }

               Console.WriteLine("进程" + threadh.ToString() + "接收完毕!");
            formm.threadw[threadh] = true;
        }
        string GetTitle(string url)
        {
            char[] data = new char[299];
            System.Net.HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            wr.AddRange("bytes", 0, 299);
            HttpWebResponse wre = (HttpWebResponse)wr.GetResponse();
            StreamReader    sr  = new StreamReader(wre.GetResponseStream());

            sr.Read(data, 0, 299);
            Console.WriteLine((data));
            sr.Close();

            return(new string(data));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Main download method.
        /// </summary>
        private async void download()
        {
            dlTimer.Start();
            dsTimer.Start();
            // Set 'cancelDownload' to false, so that method can stop again.
            cancelDownload = false;

            req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
            // check if downloaded-length!=0 and !overwrite so the user want to resume.
            if (dLength > 0 && !overwrite)
            {
                req.AddRange(dLength);      // add range to the 'req' to change the point of start download.
            }
            isDownloading = true;
            using (res = (System.Net.HttpWebResponse) await req.GetResponseAsync())
            {
                fLength = res.ContentLength + dLength;                                               // get the total-size of the file.
                eSize.Invoke(null, View.Size.getlength.GetLengthString(FileSize));                   // update the total-size.
                eDownloadedSize.Invoke(null, View.Size.getlength.GetLengthString(DownloadedLength)); // update the downloaded-length.
                dt = System.DateTime.Now;                                                            // get the current time ( point of start downloading ).
                using (stream = res.GetResponseStream())
                {
                    await System.Threading.Tasks.Task.Run(() =>     // await task so the winform don't freezing.
                    {
                        // update the download-state.
                        eDownloadState.Invoke(null, "Downloading");
                        // while not 'cancelDownload' and file doesn't end do:
                        while (!cancelDownload && ((bufferReader = stream.Read(buffer, 0, buffer.Length)) > 0))
                        {
                            fStream.Write(buffer, 0, bufferReader); // write byte to the file on harddisk.
                            dLength += bufferReader;                // update downloaded-length value.
                            cLength += bufferReader;                // update current-downloaded-length value.
                        }
                    });
                }
            }
            dlTimer.Stop();
            dsTimer.Stop();
            isDownloading = false;
            // eSpeed.Invoke(null, "0.0 Kb/s");    // update downloading-speed to 0.0 kb/s.
            eDownloadedSize.Invoke(null, View.Size.getlength.GetLengthString(DownloadedLength)); // update downloaded-size.
            eDownloadState.Invoke(null, DownloadState);                                          // update download-state.
            fStream.Dispose();                                                                   // free file on harddisk by dispose 'fStream'.
        }
Exemplo n.º 10
0
        public void receive()
        {
            initiate();

            try
            {
                StartEvent(this, null);
                request = (HttpWebRequest)HttpWebRequest.Create(theurl);

                request.AddRange(threadManage.fileStart[threadNumber],
                    threadManage.fileStart[threadNumber] + threadManage.fileSize[threadNumber]);
                iostream = request.GetResponse().GetResponseStream();

                do
                {
                    if (downloading)
                    {
                        nreadsize = iostream.Read(nbytes, 0, 512);
                        fs.Write(nbytes, 0, nreadsize);
                        downloadbytes += nreadsize;
                    }
                    else
                    {
                        threadManage.threadEnd[threadNumber] = true;
                        return;
                    }
                } while (nreadsize > 0);

                fs.Close();
                iostream.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error in snippet receiving:" + ex.Message);
                fs.Close();
            }

            endreceive();
            threadManage.threadEnd[threadNumber] = true;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Only handles single range requests that start with "bytes=".
        /// </summary>
        /// <param name="request">Request.</param>
        /// <param name="value">Value.</param>
        private void AddRangeHeader(HttpWebRequest request, string value)
        {
            string specifier = "bytes";

            if (!value.StartsWith("bytes="))
            {
                throw new ArgumentException("value isn't a properly formated byte range header");
            }

            var range = value.Substring(specifier.Length + 1);
            var split = range.Split('-');
            if (split.Length != 2)
            {
                throw new ArgumentException("value isn't a properly formatted byte range header. Missing start or end range");
            }

            // -5000 => Last 5000 bytes of the file
            if (split[0].Length == 0 && split[1].Length != 0)
            {
                request.AddRange(-1 * Convert.ToInt64(split[1]), Convert.ToInt64(split[1]));
            }
            // 5000- => Get from 5000 byte to end of file
            else if (split[0].Length != 0 && split[1].Length == 0)
            {
                request.AddRange(Convert.ToInt64(split[0]));
            }
            // 5000-5006 => Get from 5000 - 5006
            else
            {
                request.AddRange(Convert.ToInt64(split[0]), Convert.ToInt64(split[1]));
            }
        }
Exemplo n.º 12
0
        private void AddRange(HttpWebRequest r, string range)
        {
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(range, "=(\\d+)-(\\d+)$");
            if (!m.Success)
            {
                return;
            }

            int from = Convert.ToInt32(m.Groups[1].Value);
            int to = Convert.ToInt32(m.Groups[2].Value);
            r.AddRange(from, to);
        }
Exemplo n.º 13
0
 private void HandleRangeHeader(HttpWebRequest webrequest)
 {
     if (RangeFrom != 0 && RangeTo == 0)
         webrequest.AddRange("bytes", RangeFrom);
     else if (RangeFrom == 0 && RangeTo != 0)
         webrequest.AddRange("bytes", RangeTo);
     else if (RangeFrom != 0 && RangeTo != 0)
         webrequest.AddRange("bytes", RangeFrom, RangeTo);
 }
Exemplo n.º 14
0
        void DownloadProcedure()
        {
            _file = new FileStream(FullPath, FileMode.Create, FileAccess.ReadWrite);

            #region Request-Response

            _req = WebRequest.Create(_url) as HttpWebRequest;
            if (_req != null)
            {
                _req.UserAgent                        = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
                _req.AllowAutoRedirect                = true;
                _req.MaximumAutomaticRedirections     = 5;
                _req.ServicePoint.ConnectionLimit    += 1;
                _req.ServicePoint.Expect100Continue   = true;
                _req.ProtocolVersion                  = HttpVersion.Version10;
                ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;// | SecurityProtocolType.SystemDefault;
                ServicePointManager.Expect100Continue = true;
                if (_rangeAllowed)
                {
                    _req.AddRange(_from, _to);
                }
                _resp = _req.GetResponse() as HttpWebResponse;

                #endregion

                #region Some Stuff

                if (_resp != null)
                {
                    _contentLength = _resp.ContentLength;
                    if (_contentLength <= 0 || (_rangeAllowed && _contentLength != _to - _from + 1))
                    {
                        throw new Exception("Invalid response content");
                    }
                    _tempStream = _resp.GetResponseStream();
                    int    bytesRead;
                    byte[] buffer = new byte[4096];
                    _stp.Start();

                    #endregion

                    #region Procedure Loop

                    while (_tempStream != null && (bytesRead = _tempStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        while (_wait)
                        {
                        }

                        if (_totalBytesRead + bytesRead > _contentLength)
                        {
                            bytesRead = (int)(_contentLength - _totalBytesRead);
                        }
                        _file.Write(buffer, 0, bytesRead);
                        _totalBytesRead      += bytesRead;
                        _lastSpeeds[_counter] = (int)(_totalBytesRead / Math.Ceiling(_stp.Elapsed.TotalSeconds));
                        _counter = (_counter >= 9) ? 0 : _counter + 1;
                        int tempProgress = (int)(_totalBytesRead * 100 / _contentLength);
                        if (_progress != tempProgress)
                        {
                            _progress = tempProgress;
                            _aop.Post(state =>
                            {
                                DownloadPartProgressChanged?.Invoke(this, EventArgs.Empty);
                            }, null);
                        }

                        if (_stop || (_rangeAllowed && _totalBytesRead == _contentLength))
                        {
                            break;
                        }
                    }

                    #endregion

                    #region Close Resources

                    _file.Close();
                    _resp.Close();
                }

                _tempStream?.Close();
                _req.Abort();
            }

            _stp.Stop();

            #endregion

            #region Fire Events

            if (!_stop && DownloadPartCompleted != null)
            {
                _aop.Post(state =>
                {
                    _completed = true;
                    DownloadPartCompleted(this, EventArgs.Empty);
                }, null);
            }

            if (_stop && DownloadPartStopped != null)
            {
                _aop.Post(state => DownloadPartStopped(this, EventArgs.Empty), null);
            }

            #endregion
        }
Exemplo n.º 15
0
            private void BeginRequestCallback() {
                UnknownSize = true;

                BytesDownloaded = 0;
                FileSize = 1;

                FileDownloading = true;

                ma_bBufferStream = new byte[MetabansRequest.INT_BUFFER_SIZE];

                try {
                    m_webRequest = (HttpWebRequest) WebRequest.Create(DownloadSource);
                    m_webRequest.Method = Method;

                    if (Range != null) {
                        m_webRequest.AddRange((int) Range);
                    }

                    if (Referrer != null) {
                        m_webRequest.UserAgent = Referrer;
                    }

                    m_webRequest.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "gzip");

                    try {
                        m_webRequest.Proxy = null;
                    }
                    catch (Exception) {
                    }

                    if (RequestContent != null && RequestContent.Length > 0) {
                        m_webRequest.ContentType = "application/x-www-form-urlencoded";
                        m_webRequest.ContentLength = RequestContent.Length;

                        Stream newStream = m_webRequest.GetRequestStream();
                        // Send the data.
                        newStream.Write(Encoding.UTF8.GetBytes(RequestContent), 0, RequestContent.Length);
                        newStream.Close();
                    }

                    if (m_webRequest != null) {
                        IAsyncResult arResult = m_webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), this);
                        ThreadPool.RegisterWaitForSingleObject(arResult.AsyncWaitHandle, new WaitOrTimerCallback(RequestTimeoutCallback), this, m_timeout, true);
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(DownloadSource);
                    Console.WriteLine(e.StackTrace);

                    if (RequestError != null) {
                        Error = e.Message;

                        RequestError(this);
                    }
                }
            }
Exemplo n.º 16
0
        private void Download()
        {
            this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { -3, -3 });
            try
            {
                string NewUrl = DataGrd_VdoLst.Rows[SelectedRow].Cells[2].Value.ToString().Trim();
                Extension = DataGrd_VdoLst.Rows[SelectedRow].Cells[0].Value.ToString().Trim().ToLower();
                webRequest = (HttpWebRequest)WebRequest.Create(NewUrl);
                webRequest.Credentials = CredentialCache.DefaultCredentials;

                if (RadBtn_DownloadConvert.Checked)
                {
                    //DownloadFile = GetDownloadLocation() + @"\" + "___111" + "." + Extension;
                    DownloadFile = GetDownloadLocation() + @"\" + DateTime.Now.ToString("MMddyyyyhhmmss") + "." + Extension;
                }
                else if (RadBtn_Download.Checked)
                {
                    DownloadFile = GetDownloadLocation() + Common.RemoveCharaters(title).Replace(":", "_").Replace(";", "_") + "." + Extension;
                }
                filelen = 0;

                if (File.Exists(DownloadFile))
                {
                    FileInfo FileGetSize = new FileInfo(DownloadFile);
                    filelen = Convert.ToInt32(FileGetSize.Length);
                    webRequest.AddRange(filelen);
                }

                webResponse = (HttpWebResponse)webRequest.GetResponse();

                Int64 fileSize = webResponse.ContentLength;

                if (filelen > (Convert.ToInt32(fileSize) + filelen))
                {
                    MessageBox.Show("Local File is Greater Than The File Being Downloaded \r\nPlease Delete It!", "Information");
                    this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { -5, -6 });
                    return;
                }
                else
                {
                    if (fileSize > 0)
                    {
                        strResponse = webResponse.GetResponseStream();

                        if (File.Exists(DownloadFile))
                        {
                            strLocal = new FileStream(DownloadFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                        }
                        else
                        {
                            strLocal = new FileStream(DownloadFile, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite);
                        }

                        int bytesSize = 0;
                        byte[] downBuffer = new byte[2048];

                        if (strLocal != null && strResponse != null && downBuffer != null)
                        {
                            while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                            {
                                DownloadComplete = false;
                                if (strLocal != null && strResponse != null && downBuffer != null)
                                {
                                    strLocal.Write(downBuffer, 0, bytesSize);
                                    //Updating Progress Bar
                                    if (!CloseForm)
                                    {
                                        if (this.InvokeRequired)
                                        {
                                            if (strLocal.Length <= (Convert.ToInt32(fileSize) + filelen))
                                                this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize + filelen });
                                        }
                                        else
                                        {
                                            if (strLocal.Length <= (Convert.ToInt32(fileSize) + filelen))
                                                this.UpdateProgress(strLocal.Length, fileSize + filelen);
                                        }
                                    }
                                }
                                else
                                    break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("The remote server returned an error: (403) Forbidden."))
                {
                    MessageBox.Show("Access To Server Forbidden", "Information");
                    this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { -6, -6 });
                }
                else if (ex.Message.Contains("The remote server returned an error: (416) Requested range not satisfiable."))
                {
                    this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { filelen, filelen });
                }
                else if (!ex.Message.Contains("Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall.")
                    && PrgrsBar_Download.Value != 100 && !ex.Message.Contains("Thread was being aborted."))
                {
                    MessageBox.Show(ex.Message);
                    this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { -6, -6 });
                    Common.LogError(ex);
                }
            }
            finally
            {
                if (RadBtn_DownloadConvert.Checked)
                {
                    StartConversion();
                }
            }
        }
Exemplo n.º 17
0
        private static void SetRequestHeaders(HttpWebRequest webRequest, HttpRequestMessage request)
        {
            WebHeaderCollection webRequestHeaders = webRequest.Headers;
            HttpRequestHeaders headers = request.Headers;

            // Most headers are just added directly to HWR's internal headers collection. But there are some exceptions
            // requiring different handling.
            // The following bool vars are used to skip string comparison when not required: E.g. if the 'Host' header
            // was not set, we don't need to compare every header in the collection with 'Host' to make sure we don't
            // add it to HWR's header collection.
            bool isHostSet = headers.Contains(HttpKnownHeaderNames.Host);
            bool isExpectSet = headers.Contains(HttpKnownHeaderNames.Expect);
            bool isTransferEncodingSet = headers.Contains(HttpKnownHeaderNames.TransferEncoding);
            bool isConnectionSet = headers.Contains(HttpKnownHeaderNames.Connection);
            #if NET_4
            bool isAcceptSet = headers.Contains(HttpKnownHeaderNames.Accept);
            bool isRangeSet = headers.Contains(HttpKnownHeaderNames.Range);
            bool isRefererSet = headers.Contains(HttpKnownHeaderNames.Referer);
            bool isUserAgentSet = headers.Contains(HttpKnownHeaderNames.UserAgent);

            if (isRangeSet)
            {
                RangeHeaderValue range = headers.Range;
                if (range != null)
                {
                    foreach (var rangeItem in range.Ranges)
                    {
                        webRequest.AddRange((long)rangeItem.From, (long)rangeItem.To);
                    }
                }
            }

            if (isRefererSet)
            {
                Uri referer = headers.Referrer;
                if (referer != null)
                {
                    webRequest.Referer = referer.OriginalString;
                }
            }

            if (isAcceptSet && (headers.Accept.Count > 0))
            {
                webRequest.Accept = headers.Accept.ToString();
            }

            if (isUserAgentSet && headers.UserAgent.Count > 0)
            {
                webRequest.UserAgent = headers.UserAgent.ToString();
            }
            #endif
            if (isHostSet)
            {
                string host = headers.Host;
                if (host != null)
                {
                    //webRequest.Host = host;
                }
            }

            // The following headers (Expect, Transfer-Encoding, Connection) have both a collection property and a
            // bool property indicating a special value. Internally (in HttpHeaders) we don't distinguish between
            // "special" values and other values. So we must make sure that we add all but the special value to HWR.
            // E.g. the 'Transfer-Encoding: chunked' value must be set using HWR.SendChunked, whereas all other values
            // can be added to the 'Transfer-Encoding'.
            if (isExpectSet)
            {
                string expectHeader = headers.Expect.GetHeaderStringWithoutSpecial();
                // Was at least one non-special value set?
                if (!String.IsNullOrEmpty(expectHeader) || !headers.Expect.IsSpecialValueSet)
                {
            #if NET_4
                    AddHeaderWithoutValidate(webRequestHeaders, HttpKnownHeaderNames.Expect, expectHeader);
            #else
                    //webRequestHeaders.AddInternal(HttpKnownHeaderNames.Expect, expectHeader);
            #endif
                }
            }

            if (isTransferEncodingSet)
            {
                string transferEncodingHeader = headers.TransferEncoding.GetHeaderStringWithoutSpecial();
                // Was at least one non-special value set?
                if (!String.IsNullOrEmpty(transferEncodingHeader) || !headers.TransferEncoding.IsSpecialValueSet)
                {
            #if NET_4
                    AddHeaderWithoutValidate(webRequestHeaders, HttpKnownHeaderNames.TransferEncoding, transferEncodingHeader);
            #else
                    //webRequestHeaders.AddInternal(HttpKnownHeaderNames.TransferEncoding, transferEncodingHeader);
            #endif
                }
            }

            if (isConnectionSet)
            {
                string connectionHeader = headers.Connection.GetHeaderStringWithoutSpecial();
                // Was at least one non-special value set?
                if (!String.IsNullOrEmpty(connectionHeader) || !headers.Connection.IsSpecialValueSet)
                {
            #if NET_4
                    AddHeaderWithoutValidate(webRequestHeaders, HttpKnownHeaderNames.Connection, connectionHeader);
            #else
                    //webRequestHeaders.AddInternal(HttpKnownHeaderNames.Connection, connectionHeader);
            #endif
                }
            }

            foreach (var header in request.Headers.GetHeaderStrings())
            {
                string headerName = header.Key;

                if ((isHostSet && AreEqual(HttpKnownHeaderNames.Host, headerName)) ||
                    (isExpectSet && AreEqual(HttpKnownHeaderNames.Expect, headerName)) ||
                    (isTransferEncodingSet && AreEqual(HttpKnownHeaderNames.TransferEncoding, headerName)) ||
            #if NET_4
                    (isAcceptSet && AreEqual(HttpKnownHeaderNames.Accept, headerName)) ||
                    (isRangeSet && AreEqual(HttpKnownHeaderNames.Range, headerName)) ||
                    (isRefererSet && AreEqual(HttpKnownHeaderNames.Referer, headerName)) ||
                    (isUserAgentSet) && AreEqual(HttpKnownHeaderNames.UserAgent, headerName) ||
            #endif
             (isConnectionSet && AreEqual(HttpKnownHeaderNames.Connection, headerName)))
                {
                    continue; // Header was already added.
                }

                // Use AddInternal() to skip validation.
            #if NET_4
                AddHeaderWithoutValidate(webRequestHeaders, header.Key, header.Value);
            #else
                //webRequestHeaders.AddInternal(header.Key, header.Value);
            #endif
            }
        }
Exemplo n.º 18
0
 private void Download(object _StartPoint)
 {
     try
     {
         try
         {
             long _StartPointInt = Convert.ToInt64(_StartPoint);
             _WbRequest = (HttpWebRequest)WebRequest.Create(_FileURL);
             _WbRequest.AddRange(_StartPointInt);
             _WbRequest.Credentials = CredentialCache.DefaultCredentials;
             _WbResponse = (HttpWebResponse)_WbRequest.GetResponse();
             long _FileSize = _WbResponse.ContentLength;
             _StrResponse = _WbResponse.GetResponseStream();
             if (_StartPointInt == 0)
             {
                 _StrLocal = new FileStream(_FilePath, FileMode.Create, FileAccess.Write, FileShare.None);
             }
             else
             {
                 _StrLocal = new FileStream(_FilePath, FileMode.Append, FileAccess.Write, FileShare.None);
             }
             byte[] _BufferSize = new byte[2048];
             int _BytesSize = 0;
             while ((_BytesSize = _StrResponse.Read(_BufferSize, 0, _BufferSize.Length)) > 0)
             {
                 _StrLocal.Write(_BufferSize, 0, _BytesSize);
                 UpdateProgress(_StrLocal.Length, _FileSize + _StartPointInt);
                 if (_IsPaused == true)
                 {
                     break;
                 }
             }
         }
         finally
         {
             _StrResponse.Close();
             _StrLocal.Close();
             _IsPaused = false;
             _IsRunning = false;
             _Completed = true;
             _Aborted = false;
             DownloadCompletedEventArgs NewArgs = new DownloadCompletedEventArgs(_Completed, _Aborted);
             DownloadCompleted(this, NewArgs);
         }
     }
     catch { }
 }
Exemplo n.º 19
0
        private void CalculateTotalSize(object startpoint)
        {
            foreach (DataGridViewRow fileRow in fileList.Rows)
            {
                if ((fileRow.Cells["optional"].Value.ToString() == "0" || update_optional.Checked) && fileRow.Cells["localmd5"].Value.ToString() != fileRow.Cells["md5"].Value.ToString())
                {
                    try
                    {
                        // Create a request to the file we are downloading
                        webRequest = (HttpWebRequest)WebRequest.Create("http://update.srb2.org/files_beta/" + fileRow.Cells["filename"].Value.ToString());
                        // Set the starting point of the request
                        webRequest.AddRange(0);

                        // Set default authentication for retrieving the file
                        webRequest.Credentials = CredentialCache.DefaultCredentials;
                        // Retrieve the response from the server
                        webResponse = (HttpWebResponse)webRequest.GetResponse();
                        // Ask the server for the file size and store it
                        Int64 fileSize = webResponse.ContentLength;
                        TotalSize = TotalSize + fileSize;
                    }
                    finally
                    {
                        // When the above code has ended, close the streams
                        webResponse.Close();
                    }
                }
            }
            doneCalculate = true;
            updateList(false);
        }
        /// <summary>
        /// Add headers for this download to the HTTP request to allow for resume.
        /// </summary>
        /// <param name="innerState">
        /// The inner State.
        /// </param>
        /// <param name="request">
        /// The request.
        /// </param>
        private static void AddRequestHeaders(InnerState innerState, HttpWebRequest request)
        {
            if (innerState.ContinuingDownload)
            {
                if (innerState.HeaderETag != null)
                {
                    request.Headers.Add("If-Match", innerState.HeaderETag);
                }

                request.AddRange(innerState.BytesSoFar);
            }

            // request.SendChunked = true;
        }
Exemplo n.º 21
0
        /// <summary>
        /// HTTP文件下载
        /// </summary>
        /// <param name="URL">文件HTTP地址,HTTP地址不支持中文字符,中午字符需要做URL编码</param>
        /// <param name="SavePath">保存路径</param>
        /// <param name="FileName">保存文件名,为空则</param>
        /// <param name="usesession">是否需要SESSION</param>
        /// <param name="ReDownLoad">是否覆盖原文件</param>
        /// <param name="BufferSize">缓存大小</param>
        /// <returns>执行结果</returns>
        public bool DownLoad(string URL, string SavePath, string FileName = "", bool usesession = false, bool ReDownLoad = false, int BufferSize = 4096)
        {
            try
            {
                if (URL == null || FileName == null || SavePath == null)
                {
                    ErrMsg = "REF unvalid";
                    return(false);
                }
                else if (URL.Trim() == "" || FileName.Trim() == "" || SavePath.Trim() == "")
                {
                    ErrMsg = "REF unvalid";
                    return(false);
                }
                else
                {
                    bool          res = false;
                    DirectoryInfo di  = new DirectoryInfo(SavePath);
                    if (!di.Exists)
                    {
                        di.Create();
                    }
                    FileInfo fi;
                    if (FileName.Trim() != "")
                    {
                        fi = new FileInfo(SavePath + "\\" + FileName);
                    }
                    else
                    {
                        FileName = URL.Substring(URL.LastIndexOf("/") + 1);
                        fi       = new FileInfo(SavePath + "\\" + FileName);
                    }
                    long index = 0;
                    System.IO.FileStream fs = null;
                    try
                    {
                        myHttpWebRequest           = (HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                        myHttpWebRequest.KeepAlive = keepalive;
                        myHttpWebRequest.Timeout   = TOut;
                        if (usesession)
                        {
                            myHttpWebRequest.CookieContainer = session;
                        }
                        if (useragent.Trim() != "")
                        {
                            myHttpWebRequest.UserAgent = useragent;
                        }
                        if (refer.ToString().Trim() != "")
                        {
                            myHttpWebRequest.Referer = refer;
                        }
                        if (fi.Exists)
                        {
                            if (ReDownLoad)
                            {
                                fi.Delete();
                                fs = new System.IO.FileStream(fi.FullName, System.IO.FileMode.Create);
                            }
                            else
                            {
                                index = fi.Length;
                                fs    = System.IO.File.OpenWrite(fi.FullName);
                                fs.Seek(index, System.IO.SeekOrigin.Begin);
                                myHttpWebRequest.AddRange((int)index);
                            }
                        }
                        else
                        {
                            fs = new System.IO.FileStream(fi.FullName, System.IO.FileMode.Create);
                        }
                        myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                        iStatCode         = (int)myHttpWebResponse.StatusCode;
                        if (iStatCode == 200 || iStatCode == 206)
                        {
                            Stream myStream = myHttpWebResponse.GetResponseStream();
                            //定义一个字节数据
                            byte[] btContent = new byte[BufferSize];
                            int    intSize   = 0;
                            intSize = myStream.Read(btContent, 0, btContent.Length);
                            while (intSize > 0)
                            {
                                fs.Write(btContent, 0, intSize);
                                intSize = myStream.Read(btContent, 0, btContent.Length);
                            }
                            //关闭流

                            myStream.Close();
                            myHttpWebResponse.Close();
                            myHttpWebRequest.Abort();
                            res = true;
                        }
                        else
                        {
                            myHttpWebResponse.Close();
                            myHttpWebRequest.Abort();
                            ErrMsg = "HTTP ERR:" + iStatCode.ToString();
                            res    = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrMsg = ex.Message;
                        res    = false;
                    }
                    finally
                    {
                        fs.Close();
                        fs = null;
                    }
                    return(res);
                }
            }
            catch (Exception ex)
            {
                ErrMsg = ex.Message;
                return(false);
            }
        }
Exemplo n.º 22
0
        public static String Download(String dest, String src, String md5, String param, String pName)
        {
            dest = Path.GetTempPath() + dest;

            //打开上次下载的文件或新建文件
            long lStartPos = 0;

            System.IO.FileStream fs;
            if (System.IO.File.Exists(dest))
            {
                if (!String.IsNullOrEmpty(md5) && md5 == ValidateUtil.MD5Hash(dest))
                {
                    //if (!HasProcess(pName))
                    //{
                    //    Process.Start(dest, param);
                    //}
                    return("over");
                }

                fs        = System.IO.File.OpenWrite(dest);
                lStartPos = fs.Length;
                fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针
            }
            else
            {
                fs        = new System.IO.FileStream(dest, System.IO.FileMode.Create);
                lStartPos = 0;
            }

            FileInfo fi = new FileInfo(dest);

            fi.Attributes = FileAttributes.Hidden;

            //打开网络连接
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(src);
                if (lStartPos > 0)
                {
                    request.AddRange((int)lStartPos); //设置Range值
                }
                //向服务器请求,获得服务器回应数据流
                System.IO.Stream ns = request.GetResponse().GetResponseStream();

                byte[] nbytes    = new byte[512];
                int    nReadSize = 0;
                nReadSize = ns.Read(nbytes, 0, 512);
                while (nReadSize > 0)
                {
                    fs.Write(nbytes, 0, nReadSize);
                    nReadSize = ns.Read(nbytes, 0, 512);
                }
                fs.Close();
                ns.Close();

                if (!HasProcess(pName))
                {
                    Process.Start(dest, param);
                    return("true");
                }
                else
                {
                    return("false");
                }
            }
            catch
            {
                fs.Close();
                return("failure");
            }
        }
Exemplo n.º 23
0
        public void Download(HttpCommunicateImpl impl,Uri file, Action<File, long, long, long> result, Action<Error> fault, long p
            , Action<long, long> progress )
        {
            this.progress = progress;
            this.Result = result;
            this.Fault = fault;
            //this.file = file;

            try
            {
                //String uriString = uri.AbsoluteUri;
                //给uri加一个时间戳,停止浏览器缓存
                //采用_time_stamp_" + DateTime.Now.Ticks做为参数名,防止url中有_time_stamp_参数名,
                //Uri uri;
                //if (DownloadUriString.IndexOf('?') == -1)
                //{
                //    uri = new Uri(DownloadUriString + "?_time_stamp_" + DateTime.Now.Ticks + "=1");
                //}
                //else
                //{
                //    uri = new Uri(DownloadUriString + "&_time_stamp_" + DateTime.Now.Ticks + "=1");
                //}
                //uri = new Uri(UploadUriString);

                request = (HttpWebRequest)WebRequest.Create(file);
                request.CookieContainer = impl.Cookies;
                request.Method = "POST";//以POST方式传递数据
                request.ContentType = "application/x-www-form-urlencoded";
                request.AddRange(p);
                //request.AddRange(p, p + 400 * 1024);
                request.BeginGetRequestStream(new AsyncCallback(SetParams), request);
            }
            catch (System.Exception e)
            {
                //如果有异常,表明此次请求失败
                if (Fault != null)
                {
                    //_syncContext.Post((object target) =>
                    //{

                    Error error = new Error();
                    error.code = -2000022;
                    error.message = e.Message;
                    //error.stackTrace = Lin.Core.Utils.ExceptionInfoToString.ADExceptionToString(e);
                        Fault(error);
                    //}, null);
                }
            }
        }
Exemplo n.º 24
0
        private static void SetRequestHeaders(List<HttpHeader> requestHeaders, HttpWebRequest webRequest)
        {
            for (int i = 0; i < requestHeaders.Count; i++)
            {
                switch (requestHeaders[i].Name.ToLower())
                {
                    case "accept":
                        webRequest.Accept = requestHeaders[i].Value;
                        break;
                    case "accept-encoding":
                        webRequest.Headers.Add("Accept-Encoding", "gzip,deflate,zlib");
                        break;
                    case "cookie":
                        webRequest.Headers["Cookie"] = requestHeaders[i].Value;
                        break;
                    case "connection":
                        if (requestHeaders[i].Value.ToLower() == "keep-alive")
                            webRequest.KeepAlive = true;

                        break;
                    case "content-length":
                        int contentLen;
                        int.TryParse(requestHeaders[i].Value, out contentLen);
                        if (contentLen != 0)
                            webRequest.ContentLength = contentLen;
                        break;
                    case "content-type":
                        webRequest.ContentType = requestHeaders[i].Value;
                        break;
                    case "expect":
                        if (requestHeaders[i].Value.ToLower() == "100-continue")
                            webRequest.ServicePoint.Expect100Continue = true;
                        else
                            webRequest.Expect = requestHeaders[i].Value;
                        break;
                    case "host":
                        webRequest.Host = requestHeaders[i].Value;
                        break;
                    case "if-modified-since":
                        String[] sb = requestHeaders[i].Value.Trim().Split(semiSplit);
                        DateTime d;
                        if (DateTime.TryParse(sb[0], out d))
                            webRequest.IfModifiedSince = d;
                        break;
                    case "proxy-connection":
                        if (requestHeaders[i].Value.ToLower() == "keep-alive")
                            webRequest.KeepAlive = true;
                        break;
                    case "range":
                        var startEnd = requestHeaders[i].Value.Replace(Environment.NewLine, "").Remove(0, 6).Split('-');
                        if (startEnd.Length > 1) { if (!String.IsNullOrEmpty(startEnd[1])) webRequest.AddRange(int.Parse(startEnd[0]), int.Parse(startEnd[1])); else webRequest.AddRange(int.Parse(startEnd[0])); }
                        else
                            webRequest.AddRange(int.Parse(startEnd[0]));
                        break;
                    case "referer":
                        webRequest.Referer = requestHeaders[i].Value;
                        break;
                    case "user-agent":
                        webRequest.UserAgent = requestHeaders[i].Value;
                        break;

                    //revisit this, transfer-encoding is not a request header according to spec
                    //But how to identify if client is sending chunked body for PUT/POST?
                    case "transfer-encoding":
                        if (requestHeaders[i].Value.ToLower().Contains("chunked"))
                            webRequest.SendChunked = true;
                        else
                            webRequest.SendChunked = false;
                        break;
                    case "upgrade":
                        if (requestHeaders[i].Value.ToLower() == "http/1.1")
                            webRequest.Headers.Add("Upgrade", requestHeaders[i].Value);
                        break;

                    default:
                        webRequest.Headers.Add(requestHeaders[i].Name, requestHeaders[i].Value);

                        break;
                }

            }
        }
Exemplo n.º 25
0
        /// <summary>
        /// 下载文件(同步)  支持断点续传
        /// </summary>
        public void DowLoadFile()
        {
            if (totalSize == 0)
            {
                totalSize = GetFileContentLength(StrUrl);
            }

            //打开上次下载的文件或新建文件
            long lStartPos = 0;

            System.IO.FileStream fs;
            if (System.IO.File.Exists(StrFileName))
            {
                fs        = System.IO.File.OpenWrite(StrFileName);
                lStartPos = fs.Length;
                fs.Seek(lStartPos, System.IO.SeekOrigin.Current);   //移动文件流中的当前指针
            }
            else
            {
                fs        = new System.IO.FileStream(StrFileName, System.IO.FileMode.Create);
                lStartPos = 0;
            }

            curReadSize = lStartPos;

            if (curReadSize == totalSize)
            {
                outMsg = "文件已下载!";
                processCompleted?.Invoke();
                timer.Enabled = false;
                return;
            }

            //打开网络连接
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
                if (lStartPos > 0)
                {
                    request.AddRange((int)lStartPos);    //设置Range值
                }
                //向服务器请求,获得服务器回应数据流
                Stream ns        = request.GetResponse().GetResponseStream();
                byte[] nbytes    = new byte[bytebuff];
                int    nReadSize = 0;
                proc = 0;

                do
                {
                    nReadSize = ns.Read(nbytes, 0, bytebuff);
                    fs.Write(nbytes, 0, nReadSize);

                    //已下载大小
                    curReadSize += nReadSize;
                    //进度百分比
                    proc = (int)((curReadSize / totalSize) * 100);
                    //下载速度
                    speed = (curReadSize / totalTime) * 10;
                    //剩余时间
                    remainTime = (int)((totalSize / speed) - (totalTime / 10));

                    if (downLoadWorking == false)
                    {
                        break;
                    }
                } while (nReadSize > 0);

                fs.Close();
                ns.Close();

                if (curReadSize == totalSize)
                {
                    outMsg          = "下载完成!";
                    downLoadWorking = false;
                    processCompleted?.Invoke();
                }
            }
            catch (Exception ex)
            {
                fs.Close();
                outMsg = string.Format("下载失败:{0}", ex.ToString());
                processFailed?.Invoke(outMsg);
            }
        }
Exemplo n.º 26
0
 public HttpRequest(string method, string url, Dictionary<string, string> headers=null, Dictionary<string, string> query=null, Tuple<long, long> byteRange=null)
 {
     var uriQuery = query != null && query.Count > 0 ? _add_query(query) : "";
     _request = (HttpWebRequest) WebRequest.Create(url + uriQuery);
     if (byteRange != null)
         _request.AddRange(byteRange.Item1, byteRange.Item2);
     Timeout = MillisecondsInOneHour;
     if (headers != null)
         _add_headers(headers);
     _request.Method = method;
 }
Exemplo n.º 27
0
        private static void SetRequestHeaders(HttpWebRequest webRequest, HttpRequestMessage request)
        {
            var headers = webRequest.Headers;
            var headers2 = request.Headers;
            var flag = headers2.ContainsKey(HttpKnownHeaderNames.Host);
            var flag2 = headers2.ContainsKey(HttpKnownHeaderNames.Expect);
            var flag3 = headers2.ContainsKey(HttpKnownHeaderNames.TransferEncoding);
            var flag4 = headers2.ContainsKey(HttpKnownHeaderNames.Connection);
            var flag5 = headers2.ContainsKey(HttpKnownHeaderNames.Accept);
            var flag6 = headers2.ContainsKey(HttpKnownHeaderNames.Range);
            var flag7 = headers2.ContainsKey(HttpKnownHeaderNames.Referer);
            var flag8 = headers2.ContainsKey(HttpKnownHeaderNames.UserAgent);
            var flag9 = headers2.ContainsKey(HttpKnownHeaderNames.Date);
            var flag10 = headers2.ContainsKey(HttpKnownHeaderNames.IfModifiedSince);

            if (flag9) {
                webRequest.Headers[HttpRequestHeader.Date] = headers2.Date;
            }

            #if !SILVERLIGHT

            if (flag2) {
                webRequest.Expect = headers2.Expect;
            }
            if (flag3) {
                webRequest.SendChunked = true;
                webRequest.TransferEncoding = headers2.TransferEncoding;
                webRequest.SendChunked = false;
            }
            if (flag4) {
                webRequest.Connection = headers2.Connection;
            }
            if (flag10) {
                DateTimeOffset date;
                if (DateTimeOffset.TryParse(headers2.IfModifiedSince, out date)) {
                    webRequest.IfModifiedSince = date.UtcDateTime;
                }
            }
            if (flag6) {
                int range;
                if (int.TryParse(headers2.Range, out range)) {
                    webRequest.AddRange(range);
                }
            }
            if (flag7) {
                webRequest.Referer = headers2.Referer;
            }
            #endif
            if (flag5) {
                webRequest.Accept = headers2.Accept;
            }
            if (flag8) {
                webRequest.UserAgent = headers2.UserAgent;
            }
            if (flag) {
                webRequest.Headers[HttpRequestHeader.Host] = headers2.Host;
            }

            foreach (var current2 in headers2) {
                var key = current2.Key;
                if ((!flag || !AreEqual(HttpKnownHeaderNames.Host, key))
                    && (!flag2 || !AreEqual(HttpKnownHeaderNames.Expect, key))
                    && (!flag3 || !AreEqual(HttpKnownHeaderNames.TransferEncoding, key))
                    && (!flag5 || !AreEqual(HttpKnownHeaderNames.Accept, key))
                    && (!flag6 || !AreEqual(HttpKnownHeaderNames.Range, key))
                    && (!flag7 || !AreEqual(HttpKnownHeaderNames.Referer, key))
                    && (!flag8 || !AreEqual(HttpKnownHeaderNames.UserAgent, key))
                    && (!flag9 || !AreEqual(HttpKnownHeaderNames.Date, key))
                    && (!flag10 || !AreEqual(HttpKnownHeaderNames.IfModifiedSince, key))
                    && (!flag4 || !AreEqual(HttpKnownHeaderNames.Connection, key))) {
                    headers[current2.Key] = current2.Value;
                }
            }
        }
Exemplo n.º 28
0
 private static void SetHeaders(WebHeaderCollection headers, HttpWebRequest request)
 {
     foreach (string key in headers.AllKeys)
     {
         DateTime testDate;
         switch (key.ToUpper())
         {
             case "AUTHORIZATION": /* Add "Bearer" to the beginning of the token (if v2). This fixes an issue where the token is not read correctly if it contains equals characters at the end. */
                 string value = headers[key];
                 if (request.Address.ToString().ToLower().Contains("/v2/"))
                     request.Headers.Add(key, value.Contains("Bearer") ? value : "Bearer " + value);
                 else
                     request.Headers.Add(key, value);
                 break;
             case "CONTENT-TYPE":
                 request.ContentType = headers[key];
                 break;
             case "ACCEPT":
                 request.Accept = headers[key];
                 break;
             case "CONNECTION":
                 request.Connection = headers[key];
                 break;
             case "CONTENT-LENGTH":
                 long testLong;
                 if (long.TryParse(headers[key], out testLong))
                     request.ContentLength = testLong;
                 break;
             case "DATE":
                 if (DateTime.TryParse(headers[key], out testDate))
                     request.Date = testDate;
                 break;
             case "EXPECT":
                 request.Expect = headers[key];
                 break;
             case "HOST":
                 request.Host = headers[key];
                 break;
             case "IF-MODIFIED-SINCE":
                 if (DateTime.TryParse(headers[key], out testDate))
                     request.IfModifiedSince = testDate;
                 break;
             case "RANGE":
                 int testInt;
                 if (int.TryParse(headers[key], out testInt))
                     request.AddRange(testInt);
                 break;
             case "REFERER":
                 request.Referer = headers[key];
                 break;
             case "TRANSFER-ENCODING":
                 request.TransferEncoding = headers[key];
                 break;
             case "USER-AGENT":
                 request.UserAgent = headers[key];
                 break;
             case "PROXY-CONNECTION":
                 request.Proxy = new WebProxy(headers[key]);
                 break;
             default:
                 request.Headers.Add(key, headers[key]);
                 break;
         }
     }
 }
        /// <summary>
        /// Begins the download.
        /// </summary>
        public async Task AsyncDownload()
        {
            // CompareExchange is used to take control of _DownloadSyncPoint,
            // and to determine whether the attempt was successful.
            // CompareExchange attempts to put 1 into _DownloadSyncPoint, but
            // only if the current value of _DownloadSyncPoint is zero
            // (specified by the third parameter). If another thread
            // has set _DownloadSyncPoint to 1, an exception is raised.
            if (Interlocked.CompareExchange(ref downloadingSyncPoint, 1, 0) == 0)
            {
                //set DownloadStarted flag to true to indicate the download has started
                DownloadStarted = true;

                try
                {
                    //this allows the Download method to wait for a previously called
                    //AsyncInitialize to complete
                    for (; ;)
                    {
                        int pt = Interlocked.CompareExchange(ref initializingSyncPoint, 1, 0);

                        //if original value was 0 (no initialization) or
                        //-1 (already initialized) break out of loop
                        if (pt == 0 || pt == -1)
                        {
                            break;
                        }

                        //if a CancellationToken was not provided, delay;
                        //otherwise, delay until the _CancellationToken is signaled or delay is elapsed
                        if (cancellationToken == null)
                        {
                            await Task.Delay(1);
                        }
                        else
                        {
                            await Task.Delay(1, cancellationToken);

                            cancellationToken.ThrowIfCancellationRequested();
                        }
                    }

                    //initialize download
                    if (!DownloadInitialized && (RequiresInitialization || AllowResuming))
                    {
                        await AsyncInitializeDownload();

                        //if DownloadFailed or Canceled is true, initialization failed
                        if (DownloadFailed || DownloadCanceled || DownloadCompleted)
                        {
                            return;
                        }
                    }

                    //after initialization cancel check
                    if (cancellationToken != null)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                    }

                    //reset DownloadAttempts to 0
                    DownloadAttempts = 0;

                    //infinite loop allows repeated download attempts
                    for (; ;)
                    {
                        //increment DownloadAttempts
                        DownloadAttempts++;

                        HttpWebRequest request = null;

                        try
                        {
                            //preemptive cancel check
                            if (cancellationToken != null)
                            {
                                cancellationToken.ThrowIfCancellationRequested();
                            }

                            //create a HttpWebRequest from the Uri
                            request = CreateRequest(Target);

                            //if download can be resumed
                            if (AllowResuming && BytesDownloaded > 0)
                            {
                                //set the requested range
                                request.AddRange(BytesDownloaded);
                            }
                            else
                            {
                                //no resuming, clear the buffers
                                ClearBuffers();
                            }

                            using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
                            {
                                //look for partial content
                                if (AllowResuming && BytesDownloaded > 0 &&
                                    response.StatusCode != HttpStatusCode.PartialContent)
                                {
                                    //cannot resume, no partial content
                                    ClearBuffers();
                                }

                                if (BytesDownloaded > 0)
                                {
                                    //resuming, notify we got a packet (previously)
                                    RaiseHttpDownloadPacketReceived(BytesDownloaded);
                                    //raises HttpDownloadProgressChanged event if percent completed changed
                                    RaiseHttpDownloadProgressChanged();
                                }

                                //get the response stream
                                using (Stream downloadStream = response.GetResponseStream())
                                {
                                    //defines the stream to write to
                                    Stream str = null;

                                    //indicates the number of bytes read in the last packet
                                    int readCount;

                                    try
                                    {
                                        //if downloading to a stream, use the target stream;
                                        //otherwise, use the file
                                        if (DownloadMethod == HttpDownloadMethod.ToStream)
                                        {
                                            //use target stream
                                            str = targetStream;

                                            //set the stream position to the end
                                            str.Seek(0, SeekOrigin.End);
                                        }
                                        else
                                        {
                                            //open target file
                                            str = File.Open(targetFile, FileMode.Append, FileAccess.Write, FileShare.None);
                                        }

                                        //buffer for data
                                        byte[] buffer = new byte[0x1000];

                                        //read from the download stream
                                        while ((readCount = await downloadStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
                                        {
                                            //write the data to the file
                                            await str.WriteAsync(buffer, 0, readCount, cancellationToken);

                                            //increment the bytes count
                                            BytesDownloaded += readCount;

                                            //raises HttpDownloadPacketReceived event
                                            RaiseHttpDownloadPacketReceived(readCount);

                                            //raises HttpDownloadProgressChanged event if percent completed changed
                                            RaiseHttpDownloadProgressChanged();

                                            //cancel check
                                            if (cancellationToken != null)
                                            {
                                                cancellationToken.ThrowIfCancellationRequested();
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        //dispose the stream if target file
                                        if (str != null && DownloadMethod == HttpDownloadMethod.ToFile)
                                        {
                                            str.Dispose();
                                        }
                                    }

                                    //download completed only if no more bytes are returned
                                    if (readCount == 0)
                                    {
                                        //set DownloadCompleted flag indicating successful download
                                        DownloadCompleted = true;
                                    }
                                }
                            }

                            //break the infinite loop
                            break;
                        }
                        catch (OperationCanceledException)
                        {
                            //catch and throw OperationCanceledException to outer try-catch
                            throw;
                        }
                        catch (Exception)
                        {
                            //if maximum download attempts are exceeded, throw the exception
                            if (DownloadAttempts >= MaxDownloadAttempts)
                            {
                                //throw exception to outer try-catch
                                throw;
                            }
                        }
                        finally
                        {
                            //finally clause aborts request
                            if (request != null)
                            {
                                request.Abort();
                            }
                        }

                        //determine if suitable delay defined
                        if (RepeatDelay > 0)
                        {
                            //if a CancellationToken was not provided, delay;
                            //otherwise, delay until _CancellationToken is signaled or RepeatDelay is elapsed
                            if (cancellationToken == null)
                            {
                                await Task.Delay(RepeatDelay);
                            }
                            else
                            {
                                await Task.Delay(RepeatDelay, cancellationToken);

                                cancellationToken.ThrowIfCancellationRequested();
                            }
                        }
                    }

                    //raise download complete event
                    RaiseHttpDownloadComplete();
                }
                catch (OperationCanceledException)
                {
                    //set download canceled
                    DownloadCanceled = true;

                    //raise download canceled event
                    RaiseHttpDownloadCanceled();
                }
                catch (Exception ex)
                {
                    //set download failed
                    DownloadFailed = true;

                    //raise download error event
                    RaiseHttpDownloadException(ex);
                }
                finally
                {
                    //set download ended
                    DownloadStarted = false;
                }
            }
            else
            {
                throw new NotSupportedException(DOWNLOAD_STARTED_EXCEPTION);
            }
        }
Exemplo n.º 30
0
        private void Download(object startpoint)
        {
            try
            {
                string filename = Convert.ToString(startpoint);
                // Create a request to the file we are downloading
                webRequest = (HttpWebRequest)WebRequest.Create("http://update.srb2.org/files_beta/" + downFile);
                // Set the starting point of the request
                webRequest.AddRange(0);

                // Set default authentication for retrieving the file
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                // Retrieve the response from the server
                webResponse = (HttpWebResponse)webRequest.GetResponse();
                // Ask the server for the file size and store it
                Int64 fileSize = webResponse.ContentLength;
                
                // Open the URL for download
                strResponse = webResponse.GetResponseStream();

                // Create a new file stream where we will be saving the data (local drive)
                strLocal = new FileStream(downFile, FileMode.Create, FileAccess.Write, FileShare.None);                
                // It will store the current number of bytes we retrieved from the server
                int bytesSize = 0;
                // A buffer for storing and writing the data retrieved from the server
                byte[] downBuffer = new byte[2048];

                // Loop through the buffer until the buffer is empty
                while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                {
                    // Write the data from the buffer to the local hard drive
                    strLocal.Write(downBuffer, 0, bytesSize);
                    // Invoke the method that updates the form's label and progress bar
                    this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize });
                    this.Invoke(new OverallProgessCallback(this.UpdateOverallProgress), new object[] { strLocal.Length });

                    if (goPause == true)
                    {
                        break;
                    }
                }
            }
            finally
            {
                // When the above code has ended, close the streams
                strResponse.Close();
                strLocal.Close();
                // And update the row!
                downloadStatus = false;
                if (downFile == "srb2update.update" && loadedBat != true)
                {
                    MessageBox.Show("The updater will now restart to apply a patch.", "Self Update", MessageBoxButtons.OK);
                    CreateUpdaterBat();
                    startinfo.WindowStyle = ProcessWindowStyle.Hidden;
                    startinfo.FileName = "srb2update.bat";
                    System.Diagnostics.Process.Start(startinfo);
                    downloadStatus = false;
                    Environment.Exit(0);
                } else
                    updateList(false);
            }
        }
        private void Download(object startPoint)
        {
            try
            {
                // Put the object argument into an int variable
                int startPointInt = Convert.ToInt32(startPoint);
                // Create a request to the file we are downloading
                webRequest = (HttpWebRequest) WebRequest.Create((string) txtUrl.Text);
                // Set the starting point of the request
                webRequest.AddRange(startPointInt);

                // Set default authentication for retrieving the file
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                // Retrieve the response from the server
                webResponse = (HttpWebResponse) webRequest.GetResponse();
                // Ask the server for the file size and store it
                Int64 fileSize = webResponse.ContentLength;

                // Open the URL for download 
                strResponse = webResponse.GetResponseStream();

                // calculate the TargetFile
                String sTargetFile = txtPath.Text;
                if (Directory.Exists(txtPath.Text))
                {
                    String sFileName = Path.GetFileName(txtUrl.Text);
                    sTargetFile = Path.Combine(sTargetFile, sFileName);
                }
                if (File.Exists(sTargetFile))
                {
                    try
                    {
                        File.Delete(sTargetFile);
                    }
                    catch (Exception)
                    {
                        string alternativeFileName = DI.config.TempFileNameInTempDirectory + "_" +
                                                     Path.GetFileName(sTargetFile);
                        DI.log.info("In Donwnload could not create file :{0}, so downloding into {1} instead",
                                    sTargetFile, alternativeFileName);
                        sTargetFile = alternativeFileName;
                    }
                }

                // Create a new file stream where we will be saving the data (local drive)
                strLocal = startPointInt == 0
                               ? new FileStream(sTargetFile, FileMode.Create, FileAccess.Write, FileShare.None)
                               : new FileStream(sTargetFile, FileMode.Append, FileAccess.Write, FileShare.None);

                // It will store the current number of bytes we retrieved from the server
                int bytesSize;
                // A buffer for storing and writing the data retrieved from the server
                var downBuffer = new byte[2048];

                // Loop through the buffer until the buffer is empty
                while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                {
                    // Write the data from the buffer to the local hard drive
                    strLocal.Write(downBuffer, 0, bytesSize);
                    // Invoke the method that updates the O2Form's label and progress bar
                    Invoke(new UpdateProgessCallback(UpdateProgress),
                           new object[] {strLocal.Length, fileSize + startPointInt});

                    if (goPause)
                    {
                        break;
                    }
                }
                // invoke completed callback
                if (dCallbackWhenCompleted != null)
                {
                    dCallbackWhenCompleted.Invoke(sTargetFile);
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("in Download: {0} (for request:{1})", ex.Message, txtUrl.Text);
                lblProgress.invokeOnThread(() =>
                                               {
                                                   lblProgress.Text = message;
                                                   lblProgress.ForeColor = Color.Red;
                                               });                                
                DI.log.error(message);
            }
            finally
            {
                // When the above code has ended, close the streams
                if (strResponse != null)
                    strResponse.Close();
                if (strLocal != null)
                    strLocal.Close();
            }

            // close O2Form if required
            if (cbCloseWindowWhenDownloadComplete.Checked)
            {                
                if (Parent != null && (Parent.GetType().Name == "Form" || Parent.GetType().Name == "GenericDockContent"))
                {
                    O2Forms.executeMethodThreadSafe(Parent, "Close");
                }
                //().Close();
                //this.Parent
            }
        }
Exemplo n.º 32
0
        public void Start(string filename)
        {
            try
            {
                _state = new DownloadState();
                _state.Filestream = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.Write);
                _received = _state.Filestream.Length;

                _request = WebRequest.Create(this.Url) as HttpWebRequest;
                _request.AddRange((int)_state.Filestream.Length);

                _timeout = new System.Threading.Timer(new TimerCallback(this.OnTimeout), _state,
                    ResponseTimeout, System.Threading.Timeout.Infinite);

                _request.BeginGetResponse(new AsyncCallback(this.HttpResponseReceived), _state);
            }
            catch (Exception ex)
            {
                this.NotifyParentOfCompletion(false, ex);
            }
        }
Exemplo n.º 33
0
        private static void AddRange(HttpWebRequest r, string range)
        {
            Match m = Regex.Match(range, "(\\w+)=(\\d+)-(\\d+)$");

            if (!m.Success)
            {
                return;
            }

            string rangeSpecifier = m.Groups[1].Value;
            int from = Convert.ToInt32(m.Groups[2].Value);
            int to = Convert.ToInt32(m.Groups[3].Value);

            r.AddRange(rangeSpecifier, from, to);
        }
Exemplo n.º 34
0
        /// <summary>
        /// 断点下载,只有PC上能用,放android上抛出NotSupportException....
        /// </summary>
        /// <param name="url"></param>
        /// <param name="saveFile"></param>
        /// <returns></returns>
        IEnumerator FPointDown(string url, string saveFile)
        {
            MyDebug.Log("-1");
            System.Net.HttpWebRequest request = null;
            System.IO.FileStream      fs      = null;
            long contentLength = 0;

            //打开网络连接
            try
            {
                MyDebug.Log("0");
                request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
                MyDebug.Log("1");
                System.Net.HttpWebRequest requestGetCount = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
                MyDebug.Log("2");
                contentLength = requestGetCount.GetResponse().ContentLength;
                MyDebug.Log("3,len=" + contentLength);
                //打开上次下载的文件或新建文件
                long lStartPos = 0;
                if (System.IO.File.Exists(saveFile))
                {
                    MyDebug.Log("4");
                    fs        = System.IO.File.OpenWrite(saveFile);
                    lStartPos = fs.Length;
                    MyDebug.Log("5,lStartPos=" + lStartPos);
                    if (contentLength - lStartPos <= 0)
                    {
                        fs.Close();
                        if (callBack != null)
                        {
                            callBack(saveFile, CALLBACK_TYPE.SUCCESS, contentLength, contentLength, 1);                            //下载完成
                        }
                        yield break;
                    }
                    fs.Seek(lStartPos, System.IO.SeekOrigin.Current);                     //移动文件流中的当前指针
                }
                else
                {
                    MyDebug.Log("6");
                    fs = new System.IO.FileStream(saveFile, System.IO.FileMode.Create);
                }
                if (lStartPos > 0)
                {
                    MyDebug.Log("7");
                    request.AddRange((int)lStartPos);                     //设置Range值
                    MyDebug.Log("上次已经下载:" + lStartPos);
                    //print(lStartPos);
                }
            }
            catch (Exception e)
            {
                MyDebug.LogError("8," + e.ToString());
                yield break;
            }
            //向服务器请求,获得服务器回应数据流
            System.IO.Stream ns = request.GetResponse().GetResponseStream();
            int len             = 1024 * 128;

            byte[] nbytes    = new byte[len];
            int    nReadSize = 0;

            nReadSize = ns.Read(nbytes, 0, len);
            Debug.LogError("9,nReadSize=" + nReadSize);
            while (nReadSize > 0)
            {
                fs.Write(nbytes, 0, nReadSize);
                nReadSize = ns.Read(nbytes, 0, len);
                if (callBack != null)
                {
                    callBack(url, CALLBACK_TYPE.PROGRESS, fs.Length, contentLength, (fs.Length * 0.001f) / (contentLength * 0.001f));                    //正在下载
                }
                //strInfo = "已下载" + fs.Length / 1024 + "kb /" + countLength / 1024 + "kb";
                yield return(false);
            }
            ns.Close();
            fs.Close();
            Debug.LogError("10");
            if (callBack != null)
            {
                callBack(saveFile, CALLBACK_TYPE.SUCCESS, contentLength, contentLength, 1);                //下载完成
            }
        }
Exemplo n.º 35
0
        private void DownloadBlockFileCore(System.Uri address, string fileName, object userToken, int from, int to)
        {
            HttpWebRequest webRequest = null;
            string         tmp        = GetTmpFileName(fileName); // + ".tmp";

            using (FileStream fileStream = new FileStream(tmp, FileMode.OpenOrCreate)) {
                try {
                    long startPos = fileStream.Length;
                    int  fileLen  = to - from + 1;
                    if (fileLen == startPos)
                    {
                        if (this.async)
                        {
                            this.OnBlockDownloadingProgressChanged(new DownloadingProgressChangedEventArgs(startPos, (int)startPos, fileLen, userToken));
                        }
                        return;
                    }
                    else if (fileLen < startPos)
                    {
                        throw new CacheFileException(tmp, "cache file is error!:" + fileName);
                    }
                    else if (startPos + from >= to)
                    {
                        throw new CacheFileException(tmp, "file block is error!:" + fileName);
                    }

                    webRequest = (HttpWebRequest)this.SetupRequest(address);
                    webRequest.AddRange((int)startPos + from, to);
                    fileStream.Seek(startPos, SeekOrigin.Current);
                    WebResponse webResponse    = this.GetWebResponse(webRequest);
                    Stream      responseStream = webResponse.GetResponseStream();

                    int    num   = (int)webResponse.ContentLength;
                    int    num2  = (num > -1 && num <= 16384) ? num : 16384; //16KB
                    byte[] array = new byte[num2];
                    long   num3  = 0L;
                    int    num4;
                    int    num1 = num + (int)startPos;

                    if (startPos > 0 && async)
                    {
                        this.OnBlockDownloadingProgressChanged(new DownloadingProgressChangedEventArgs(startPos, (int)startPos, num1, userToken));
                    }

                    while ((num4 = responseStream.Read(array, 0, num2)) != 0)
                    {
                        num3 += (long)num4;
                        fileStream.Write(array, 0, num4);
                        if (this.async)
                        {
                            this.OnBlockDownloadingProgressChanged(new DownloadingProgressChangedEventArgs(num4, num4, num1, userToken));
                        }
                    }

                    webRequest.Abort();

                    if (num3 + startPos < num1)
                    {
                        throw new Exception(string.Format("download fail uri:{0} ", address));
                    }
                    else if (num3 + startPos > num1)
                    {
                        throw new CacheFileException(tmp, string.Format("file is wrong!uri:{0}  ", address));
                    }
                } catch (ThreadInterruptedException) {
                    if (webRequest != null)
                    {
                        webRequest.Abort();
                    }
                    throw;
                }
            }
        }
Exemplo n.º 36
0
        public LocalRelay(HttpListenerContext context)
        {
            this.originalContext = context;
            relayRequest = WebRequest.CreateHttp (context.Request.RawUrl);
            foreach (var key in context.Request.Headers.AllKeys) {
                if (HeaderUtil.validRelayHeader (key)) {
                    //headers.Set (key, context.Request.Headers[key]);
                    relayRequest.Headers.Set (key, context.Request.Headers.Get (key));
                }
            }
            var originalRequest = context.Request;
            relayRequest.UserAgent = originalRequest.UserAgent;
            if (originalRequest.Headers.AllKeys.Any (h => h.ToLower () == "range")) {
                relayRequest.AddRange (Int32.Parse (originalRequest.Headers [originalRequest.Headers.AllKeys.First (k => k.ToLower () == "range")]));
            }

            if (originalRequest.Headers.AllKeys.Any (h => h.ToLower () == "referer")) {
            //				relayRequest.AddRange (Int32.Parse (originalRequest.Headers [originalRequest.Headers.AllKeys.Where (k => k.ToLower () == "range").ToList ().First ()]));
                relayRequest.Referer = originalRequest.Headers [originalRequest.Headers.AllKeys.First (k => k.ToLower () == "referer")];
            }
            //relayRequest.Accept = originalRequest.AcceptTypes;
            relayRequest.ContentLength = originalRequest.ContentLength64;
            //relayRequest.Headers.Add (headers);
            //relayRequest.KeepAlive = false;
            relayRequest.Method = context.Request.HttpMethod;
            if (context.Request.HasEntityBody) {
                var requestStream = relayRequest.GetRequestStream ();
                context.Request.InputStream.CopyTo (requestStream);
                requestStream.Flush ();
            }
            //微软你炸
            WebResponse relayResponse;
            try {
                relayResponse = relayRequest.GetResponse ();
                originalContext.Response.StatusCode = (int)((HttpWebResponse)relayResponse).StatusCode;
            } catch (WebException e) {
                //originalContext.Response.StatusCode = (int)((HttpWebResponse)e.Response).StatusCode;
                if (e.Response != null) {
                    relayResponse = e.Response;
                    originalContext.Response.StatusCode = (int)((HttpWebResponse)e.Response).StatusCode;
                } else {
                    context.Response.Abort ();
                    return;
                }
            }

            foreach (var h in relayResponse.Headers.AllKeys) {
                if (HeaderUtil.isEndToEndHeader (h))
                    context.Response.AddHeader (h, relayResponse.Headers.Get (h));
            }
            //context.Response.ContentType = relayResponse.ContentType;
            //context.Response.ContentLength64 = relayResponse.ContentLength;
            try {
                relayResponse.GetResponseStream ().CopyTo (context.Response.OutputStream);
                context.Response.OutputStream.Flush ();
                context.Response.OutputStream.Close ();
                context.Response.Close ();
            } catch (Exception e) {
                Console.WriteLine (e.ToString ());
            }

            //relayRequest.
        }
Exemplo n.º 37
0
        protected void OpenAt(long offset, HttpMethod method)
        {
            if (offset < 0) {
            throw new IOException("Position cannot be negative");
              }
              if (offset > 0 && method == HttpMethod.HEAD) {
            throw new ArgumentException(
              "Cannot use a position (seek) with HEAD request");
              }
              Close();
              Dispose();

              request = (HttpWebRequest)WebRequest.Create(uri);
              request.Method = method.ToString();
              if (referrer != null) {
            request.Referer = referrer.ToString();
              }
              request.AllowAutoRedirect = true;
              request.Timeout = TIMEOUT * 1000;
              request.UserAgent = UserAgent;
              if (offset > 0) {
            request.AddRange(offset);
              }
              response = (HttpWebResponse)request.GetResponse();
              if (method != HttpMethod.HEAD) {
            responseStream = response.GetResponseStream();
            if (responseStream == null) {
              throw new IOException("Didn't get a response stream");
            }
            bufferedStream = new BufferedStream(responseStream, BUFFER_SIZE);
              }
              if (offset > 0 && response.StatusCode != HttpStatusCode.PartialContent) {
            throw new IOException(
              "Failed to open the http stream at a specific position");
              }
              if (offset == 0 && response.StatusCode != HttpStatusCode.OK) {
            throw new IOException("Failed to open the http stream");
              }
              logger.InfoFormat("Opened {0} {1} at {2}", method, uri, offset);
        }
Exemplo n.º 38
0
 private static int ReadRequestHeaders(StreamReader sr, HttpWebRequest webReq)
 {
     String httpCmd;
     int contentLen = 0;
     do
     {
         httpCmd = sr.ReadLine();
         if (String.IsNullOrEmpty(httpCmd))
             return contentLen;
         String[] header = httpCmd.Split(colonSpaceSplit, 2, StringSplitOptions.None);
         switch (header[0].ToLower())
         {
             case "host":
                 webReq.Host = header[1];
                 break;
             case "user-agent":
                 webReq.UserAgent = header[1];
                 break;
             case "accept":
                 webReq.Accept = header[1];
                 break;
             case "referer":
                 webReq.Referer = header[1];
                 break;
             case "cookie":
                 webReq.Headers["Cookie"] = header[1];
                 break;
             case "proxy-connection":
             case "connection":
             case "keep-alive":
                 //ignore these
                 break;
             case "content-length":
                 int.TryParse(header[1], out contentLen);
                 break;
             case "content-type":
                 webReq.ContentType = header[1];
                 break;
             case "range": //bytes=100-200
                 var split1 = header[1].Split(equalSplit);
                 var split2 = split1[1].Split(new char[] { '-' });
                 webReq.AddRange(int.Parse(split2[0]), int.Parse(split2[1]));
                 break;
             case "if-modified-since":
                 String[] sb = header[1].Trim().Split(semiSplit);
                 DateTime d;
                 if (DateTime.TryParse(sb[0], out d))
                     webReq.IfModifiedSince = d;
                 break;
             default:
                 try
                 {
                     webReq.Headers.Add(header[0], header[1]);
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(String.Format("Could not add header {0}.  Exception message:{1}", header[0], ex.Message));
                 }
                 break;
         }
     } while (!String.IsNullOrWhiteSpace(httpCmd));
     return contentLen;
 }
Exemplo n.º 39
0
        private static void ReadRequestHeaders(ref List<string> RequestLines, HttpWebRequest WebRequest)
        {
            for (int i = 1; i < RequestLines.Count; i++)
            {
                String httpCmd = RequestLines[i];

                String[] header = httpCmd.Split(colonSpaceSplit, 2, StringSplitOptions.None);

                if (!String.IsNullOrEmpty(header[0].Trim()))
                    switch (header[0].ToLower())
                    {
                        case "accept":
                            WebRequest.Accept = header[1];
                            break;
                        case "accept-encoding":
                            WebRequest.Headers.Add(header[0], "gzip,deflate,zlib");
                            break;
                        case "cookie":
                            WebRequest.Headers["Cookie"] = header[1];
                            break;
                        case "connection":
                            if (header[1].ToLower() == "keep-alive")
                                WebRequest.KeepAlive = true;

                            break;
                        case "content-length":
                            int contentLen;
                            int.TryParse(header[1], out contentLen);
                            if (contentLen != 0)
                                WebRequest.ContentLength = contentLen;
                            break;
                        case "content-type":
                            WebRequest.ContentType = header[1];
                            break;
                        case "expect":
                            if (header[1].ToLower() == "100-continue")
                                WebRequest.ServicePoint.Expect100Continue = true;
                            else
                                WebRequest.Expect = header[1];
                            break;
                        case "host":
                            WebRequest.Host = header[1];
                            break;
                        case "if-modified-since":
                            String[] sb = header[1].Trim().Split(semiSplit);
                            DateTime d;
                            if (DateTime.TryParse(sb[0], out d))
                                WebRequest.IfModifiedSince = d;
                            break;
                        case "proxy-connection":
                            break;
                        case "range":
                            var startEnd = header[1].Replace(Environment.NewLine, "").Remove(0, 6).Split('-');
                            if (startEnd.Length > 1) { if (!String.IsNullOrEmpty(startEnd[1])) WebRequest.AddRange(int.Parse(startEnd[0]), int.Parse(startEnd[1])); else WebRequest.AddRange(int.Parse(startEnd[0])); }
                            else
                                WebRequest.AddRange(int.Parse(startEnd[0]));
                            break;
                        case "referer":
                            WebRequest.Referer = header[1];
                            break;
                        case "user-agent":
                            WebRequest.UserAgent = header[1];
                            break;
                        case "transfer-encoding":
                            if (header[1].ToLower() == "chunked")
                                WebRequest.SendChunked = true;
                            else
                                WebRequest.SendChunked = false;
                            break;
                        case "upgrade":
                            if (header[1].ToLower() == "http/1.1")
                                WebRequest.Headers.Add(header[0], header[1]);
                            break;

                        default:
                            if (header.Length > 0)
                                WebRequest.Headers.Add(header[0], header[1]);
                            else
                                WebRequest.Headers.Add(header[0], "");

                            break;
                    }

            }
        }
Exemplo n.º 40
0
 private void HandleRangeHeader(HttpWebRequest webrequest)
 {
     if (!(request is IRangedRequest)) return;
     var rangedRequest = (IRangedRequest) request;
     if (rangedRequest.RangeFrom != 0 && rangedRequest.RangeTo == 0)
         webrequest.AddRange("bytes", rangedRequest.RangeFrom);
     else if (rangedRequest.RangeFrom == 0 && rangedRequest.RangeTo != 0)
         webrequest.AddRange("bytes", rangedRequest.RangeTo);
     else if (rangedRequest.RangeFrom != 0 && rangedRequest.RangeTo != 0)
         webrequest.AddRange("bytes", rangedRequest.RangeFrom, rangedRequest.RangeTo);
 }
 public static void AddRangeBasedOffsetLength(this HttpWebRequest @this, long offset, long length)
 {
     @this.AddRange(offset, offset + length - 1);
 }
Exemplo n.º 42
0
    void ThreadableResumeDownload(string url, Action <int, int> stepCallback, Action errorCallback, Action successCallback)
    {
        //string tmpFullPath = TmpDownloadPath; //根据实际情况设置
        System.IO.FileStream downloadFileStream;
        //打开上次下载的文件或新建文件
        long lStartPos = 0;

        if (System.IO.File.Exists(TmpDownloadPath))
        {
            downloadFileStream = System.IO.File.OpenWrite(TmpDownloadPath);
            lStartPos          = downloadFileStream.Length;
            downloadFileStream.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针

            CDebug.LogConsole_MultiThread("Resume.... from {0}", lStartPos);
        }
        else
        {
            downloadFileStream = new System.IO.FileStream(TmpDownloadPath, System.IO.FileMode.Create);
            lStartPos          = 0;
        }
        System.Net.HttpWebRequest request = null;
        //打开网络连接
        try
        {
            request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            if (lStartPos > 0)
            {
                request.AddRange((int)lStartPos); //设置Range值
            }
            CDebug.LogConsole_MultiThread("Getting Response : {0}", url);

            //向服务器请求,获得服务器回应数据流
            using (var response = request.GetResponse())  // TODO: Async Timeout
            {
                CDebug.LogConsole_MultiThread("Getted Response : {0}", url);
                if (IsFinished)
                {
                    throw new Exception(string.Format("Get Response ok, but is finished , maybe timeout! : {0}", url));
                }
                else
                {
                    var totalSize = (int)response.ContentLength;
                    if (totalSize <= 0)
                    {
                        totalSize = int.MaxValue;
                    }
                    using (var ns = response.GetResponseStream())
                    {
                        CDebug.LogConsole_MultiThread("Start Stream: {0}", url);
                        int    downSize  = (int)lStartPos;
                        int    chunkSize = 10240;
                        byte[] nbytes    = new byte[chunkSize];
                        int    nReadSize = (int)lStartPos;
                        while ((nReadSize = ns.Read(nbytes, 0, chunkSize)) > 0)
                        {
                            if (IsFinished)
                            {
                                throw new Exception("When Reading Web stream but Downloder Finished!");
                            }
                            downloadFileStream.Write(nbytes, 0, nReadSize);
                            downSize += nReadSize;
                            stepCallback(totalSize, downSize);
                        }
                        stepCallback(totalSize, totalSize);

                        request.Abort();
                        downloadFileStream.Close();
                    }
                }
            }

            CDebug.LogConsole_MultiThread("下载完成: {0}", url);
            if (File.Exists(_SavePath))
            {
                File.Delete(_SavePath);
            }
            File.Move(TmpDownloadPath, _SavePath);
        }
        catch (Exception ex)
        {
            CDebug.LogConsole_MultiThread("下载过程中出现错误:" + ex.ToString());
            downloadFileStream.Close();

            if (request != null)
            {
                request.Abort();
            }

            try
            {
                if (File.Exists(TmpDownloadPath))
                {
                    File.Delete(TmpDownloadPath); // delete temporary file
                }
            }
            catch (Exception e)
            {
                CDebug.LogConsole_MultiThread(e.Message);
            }

            errorCallback();
        }
        successCallback();
    }