Ejemplo n.º 1
0
        internal bool CreateDownload(TransferParameter parameter)
        {
            bool ret = false;

            try
            {
                Socket s = Login();
                if (s != null)
                {
                    int contentLength = GetFileSize(s, GetUnescapeString(_transferUri.AbsolutePath));
                    if (contentLength > 0)
                    {
                        DownloadFileState = new FileState(parameter.TransferUrl, parameter.LocalFile, contentLength, parameter.ChunkCount);
                        if (DownloadFileState != null)
                        {
                            ret = true;
                            AssignFileStateDelegate();
                            DownloadFileState.FireFileDownloadBeginingEvent();
                        }
                    }
                    CleanUp(s, true);
                }
            }
            catch (Exception e)
            {
                Log.Debug("- Got an Excetpion {0}", e.ToString());
                Log.Warn(e);
                ret = false;
            }

            return(ret);
        }
Ejemplo n.º 2
0
        private void CreateDownload(TransferParameter parameter)
        {
            try
            {
                bool allowRanges = false;
                int  contentLength;


                _request = DownloadRequst.GetWebRequest(parameter);
                _request.PreAuthenticate = true;

                _request.Accept            = "*/*";
                _request.AllowAutoRedirect = true;
                _request.Method            = "HEAD";

                Log.Debug("^ GET {0} HTTP/{1}", parameter.TransferUrl, _request.ProtocolVersion);
                Log.Debug("^ Host: {0}", _request.Address.Host);
                Log.Debug("^ Accept: {0}", _request.Accept);
                Log.Debug("^ User-Agent: {0}", _request.UserAgent);
                Log.Debug("^ Referer: {0}", _request.Referer);

                var response = (HttpWebResponse)_request.GetResponse();

                if (response.ContentLength == -1 || response.ContentLength > 0x7fffffff)
                {
                    contentLength = 1024 * 1024 * 5;
                }
                else
                {
                    contentLength = (int)response.ContentLength;
                }

                Log.Debug("v HTTP/{0} {1} OK", response.ProtocolVersion, response.StatusCode);
                for (int i = 0; i < response.Headers.Count; ++i)
                {
                    if (String.Compare(response.Headers.Keys[i], "Accept-Ranges", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        allowRanges = true;
                    }

                    Log.Debug("v {0}: {1}", response.Headers.Keys[i], response.Headers[i]);
                }

                response.Close();
                lock (this)
                {
                    FileUtil.CreateFile(parameter.LocalFile, contentLength);
                }
                DownloadFileState = new FileState(parameter.TransferUrl, parameter.LocalFile, contentLength, (allowRanges ? _chunkCount : (short)1));
                AssignFileStateDelegate();
                DownloadFileState.FireFileDownloadBeginingEvent();
            }
            catch (WebException e)
            {
                Log.Warn("- Got an Excetpion {0} in Create WebException", e.ToString());
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    string a = ((HttpWebResponse)e.Response).StatusCode.ToString();
                    a += ((HttpWebResponse)e.Response).StatusDescription;
                    Log.Warn(a);
                }

                throw new WebException(e.Message);
            }
            catch (Exception e)
            {
                Log.Warn("- Got an Excetpion {0}  in Create Exception", e.ToString());
                throw new Exception(e.Message);
            }
        }