public AsyncDownloadArgs(object userArgs, DateTime st, HttpWebRequest wr, bool isTime, StreamDownloadSettings <R> settings, byte[] postData)
     : base(userArgs)
 {
     this.StartTime = st;
     this.WR        = wr;
     this.TimedOut  = isTime;
     this.Settings  = settings;
     this.PostData  = postData;
 }
        public void DownloadAsync(SettingsBase userSettings, object userArgs)
        {
            if (!mDisposedValue)
            {
                mUserArgs = userArgs;
                StreamDownloadSettings <T> ss = new StreamDownloadSettings <T>(userSettings);
                DateTime       startTime      = System.DateTime.Now;
                HttpWebRequest wr             = this.GetWebRequest(ss);
                byte[]         postDataBytes  = null;
                if (userSettings.PostDataInternal != string.Empty)
                {
                    postDataBytes = this.StringToAscii(userSettings.PostDataInternal);
                }
                mActualDownload = wr;

                try
                {
                    if (postDataBytes != null)
                    {
                        AsyncDownloadArgs <T> asyncArgs = new AsyncDownloadArgs <T>(userArgs, startTime, wr, false, ss, postDataBytes)
                        {
                            Timeout = this.Timeout
                        };
                        IAsyncResult res = wr.BeginGetRequestStream(new AsyncCallback(this.RequestStreamDownloadCompleted), asyncArgs);
                        try
                        {
                            System.Threading.ThreadPool.RegisterWaitForSingleObject(res.AsyncWaitHandle, new System.Threading.WaitOrTimerCallback(this.ResponseDownloadTimeout), asyncArgs, (this.Timeout), true);
                        }
                        catch (NotSupportedException ex)
                        {
                        }
                    }
                    else
                    {
                        this.DownloadAsyncResponse(wr, ss, startTime, userArgs);
                    }
                }
                catch (Exception ex)
                {
                    System.Net.WebException dlException = this.GetOrCreateWebException(ex, null);
                    if (AsyncDownloadCompleted != null)
                    {
                        this.AsyncDownloadCompleted(this, new StreamDownloadCompletedEventArgs <T>(userArgs, new DefaultResponse <Stream>(new ConnectionInfo(dlException, this.Timeout, 0, startTime, System.DateTime.Now), null), ss));
                    }
                }
            }
        }
        private HttpWebRequest GetWebRequest(StreamDownloadSettings <T> ss)
        {
            string         url = ss.UserSettings.GetUrlInternal();
            HttpWebRequest wr  = (HttpWebRequest)HttpWebRequest.Create(url);

            this.SetProxyAndTimeout(wr, ss.UserSettings);
            wr.Method = ss.UserSettings.MethodInternal.ToString();
            if (ss.UserSettings.CookiesInternal != null)
            {
                wr.CookieContainer = ss.UserSettings.CookiesInternal;
            }
            if (ss.UserSettings.ContentTypeInternal != string.Empty)
            {
                wr.ContentType = ss.UserSettings.ContentTypeInternal;
            }
            this.AddHeaders(wr, ss.UserSettings.GetAdditionalHeadersInternal);
            return(wr);
        }
        private void DownloadAsyncResponse(HttpWebRequest wr, StreamDownloadSettings <T> ss, DateTime startTime, object userArgs)
        {
            try
            {
                AsyncDownloadArgs <T> asyncArgs = new AsyncDownloadArgs <T>(userArgs, System.DateTime.Now, wr, false, ss, null);

                IAsyncResult res = wr.BeginGetResponse(new AsyncCallback(this.ResponseDownloadCompleted), asyncArgs);
                try
                {
                    //System.Threading.ThreadPool.RegisterWaitForSingleObject(res.AsyncWaitHandle, new System.Threading.WaitOrTimerCallback(this.ResponseDownloadTimeout), asyncArgs, (this.Timeout), true);
                }
                catch (NotSupportedException ex)
                {
                }
            }
            catch (Exception ex)
            {
                System.Net.WebException dlException = this.GetOrCreateWebException(ex, null);
                if (AsyncDownloadCompleted != null)
                {
                    this.AsyncDownloadCompleted(this, new StreamDownloadCompletedEventArgs <T>(userArgs, new DefaultResponse <Stream>(new ConnectionInfo(dlException, this.Timeout, 0, startTime, System.DateTime.Now), null), ss));
                }
            }
        }
        public Response <System.IO.Stream> Download(SettingsBase userSettings)
        {
            if (!mDisposedValue)
            {
                StreamDownloadSettings <T> ss = new StreamDownloadSettings <T>(userSettings);
                DateTime       startTime      = System.DateTime.Now;
                HttpWebRequest wr             = this.GetWebRequest(ss);
                byte[]         postDataBytes  = null;
                if (userSettings.PostDataInternal != string.Empty)
                {
                    postDataBytes    = System.Text.Encoding.ASCII.GetBytes(userSettings.PostDataInternal);
                    wr.ContentLength = postDataBytes.Length;
                }
                mActualDownload = wr;

                System.IO.MemoryStream  memStream   = null;
                System.Net.WebException dlException = null;
                int size = 0;
                List <KeyValuePair <HttpResponseHeader, string> > headers = new List <KeyValuePair <HttpResponseHeader, string> >();
                DateTime endTime = System.DateTime.Now;

                try
                {
                    if (postDataBytes != null)
                    {
                        using (System.IO.Stream s = wr.GetRequestStream())
                        {
                            s.Write(postDataBytes, 0, postDataBytes.Length);
                        }
                    }

                    using (HttpWebResponse resp = (HttpWebResponse)wr.GetResponse())
                    {
                        foreach (var header in resp.Headers.Keys)
                        {
                            headers.Add(new KeyValuePair <HttpResponseHeader, string>());
                        }
                        if (userSettings.DownloadResponseStreamInternal)
                        {
                            System.IO.Stream s = resp.GetResponseStream();
                            endTime   = System.DateTime.Now;
                            memStream = MyHelper.CopyStream(s);
                            s.Dispose();
                        }
                    }

                    if (memStream != null && memStream.CanSeek)
                    {
                        int.TryParse(memStream.Length.ToString(), out size);
                    }
                }
                catch (Exception ex)
                {
                    dlException = this.GetOrCreateWebException(ex, null);
                }
                finally
                {
                    mActualDownload = null;
                }


                return(new DefaultResponse <System.IO.Stream>(new ConnectionInfo(dlException, this.Timeout, size, startTime, endTime, headers.ToArray()), memStream));
            }
            else
            {
                return(null);
            }
        }
 internal StreamDownloadCompletedEventArgs(object userArgs, Response <Stream> response, StreamDownloadSettings <T> settings)
     : base(userArgs, response, settings)
 {
 }