/// <summary>
 /// Creates a new tile information instance.
 /// </summary>
 /// <param name="readCompleteHandler">Handler to call when read operations initiated by
 /// <see cref="RequestImageAync"/> are completed. Make sure that the handler calls
 /// <see cref="MarkImageRequestCompleted"/> or <see cref="MarkImageRequestCancelled"/> once it finishes
 /// handling the request.</param>
 public TileInformation(OpenReadCompletedEventHandler readCompleteHandler)
 {
     RequestClient = new WebClient();
     RequestClient.OpenReadCompleted += readCompleteHandler;
     IsRequestCancelled = false;
     imageBuffer        = new byte[BingMapsTiles.InitialImageBufferSize];
 }
 /// <summary>
 /// Creates a new tile information instance.
 /// </summary>
 /// <param name="readCompleteHandler">Handler to call when read operations initiated by 
 /// <see cref="RequestImageAync"/> are completed. Make sure that the handler calls 
 /// <see cref="MarkImageRequestCompleted"/> or <see cref="MarkImageRequestCancelled"/> once it finishes 
 /// handling the request.</param>
 public TileInformation(OpenReadCompletedEventHandler readCompleteHandler)
 {
     RequestClient = new WebClient();
     RequestClient.OpenReadCompleted += readCompleteHandler;
     IsRequestCancelled = false;
     imageBuffer = new byte[BingMapsTiles.InitialImageBufferSize];
 }
Beispiel #3
0
        /// <summary>Opens a readable stream for the data downloaded from a resource, asynchronously.</summary>
        /// <param name="webClient">The WebClient.</param>
        /// <param name="address">The URI for which the stream should be opened.</param>
        /// <returns>A Task that contains the opened stream.</returns>
        public static Task <Stream> OpenReadTask(this WebClient webClient, Uri address)
        {
            // Create the task to be returned
            var tcs = new TaskCompletionSource <Stream>(address);

            // Setup the callback event handler
            OpenReadCompletedEventHandler handler = null;

            handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, () => webClient.OpenReadCompleted -= handler);
            webClient.OpenReadCompleted += handler;

            // Start the async work
            try
            {
                webClient.OpenReadAsync(address, tcs);
            }
            catch (Exception exc)
            {
                // If something goes wrong kicking off the async work,
                // unregister the callback and cancel the created task
                webClient.OpenReadCompleted -= handler;
                tcs.TrySetException(exc);
            }

            // Return the task that represents the async operation
            return(tcs.Task);
        }
        public static Task <Byte[]> DownloadDataTaskAsync(this WebClient client, Uri address)
        {
            var completerionSource = new TaskCompletionSource <byte[]>();
            var token = new object();

            OpenReadCompletedEventHandler handler = null;

            handler = (sender, args) =>
            {
                if (args.UserState != token)
                {
                    return;
                }

                if (args.Error != null)
                {
                    completerionSource.SetException(args.Error);
                }
                else if (args.Cancelled)
                {
                    completerionSource.SetCanceled();
                }
                else
                {
                    completerionSource.SetResult(args.Result.ToArray());
                }

                client.OpenReadCompleted -= handler;
            };

            client.OpenReadCompleted += handler;
            client.OpenReadAsync(address, token);

            return(completerionSource.Task);
        }
        /// <summary>
        /// 开始向此URL发请求(自动将使用异常读取的方式),请求完成后调用readComplted(2参数)委托
        /// </summary>
        /// <param name="url"></param>
        /// <param name="readComplted"></param>
        public static void OpenRead(string url, OpenReadCompletedEventHandler readComplted)
        {
            WebClient wc = new WebClient();

            wc.OpenReadCompleted += readComplted;
            wc.OpenReadAsync(new Uri(url));
        }
        public static Task <Stream> OpenReadTaskAsync(this WebClient client, Uri uri)
        {
            var completionSource = new TaskCompletionSource <Stream>();
            OpenReadCompletedEventHandler handler = null;

            handler = (s, e) =>
            {
                if (e.Error != null)
                {
                    completionSource.TrySetException(e.Error);
                }
                else
                {
                    completionSource.TrySetResult(e.Result);
                }

                // Unsubscribe the handler
                var webClient = e.UserState as WebClient;
                if (handler != null)
                {
                    webClient.OpenReadCompleted -= handler;
                }
            };
            // Subscribe the handler.
            client.OpenReadCompleted += handler;
            client.OpenReadAsync(uri, client);
            return(completionSource.Task);
        }
Beispiel #7
0
        private static Task <Stream> OpenReadTaskInternal(WebClient webClient, CancellationToken token, Uri address)
        {
            var tcs = new TaskCompletionSource <Stream>(address);

            token.Register(webClient.CancelAsync);

            OpenReadCompletedEventHandler handler = null;

            handler = (s, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, () => webClient.OpenReadCompleted -= handler);
            webClient.OpenReadCompleted += handler;

            try
            {
                webClient.OpenReadAsync(address, tcs);
            }
            catch (Exception ex)
            {
                if (log.IsWarnEnabled)
                {
                    log.Warn("WebClient를 이용하여 데이타 읽기용 Stream을 오픈하는데 실패했습니다. address=[{0}]", address.AbsoluteUri);
                    log.Warn(ex);
                }

                webClient.OpenReadCompleted -= handler;
                tcs.TrySetException(ex);
            }

            return(tcs.Task);
        }
Beispiel #8
0
        public static Task <Stream> OpenReadTaskAsync(this WebClient client, Uri uri)
        {
            var tcs = new TaskCompletionSource <Stream>();

            OpenReadCompletedEventHandler openReadEventHandler = null;

            openReadEventHandler = (sender, args) =>
            {
                try
                {
                    tcs.SetResult(args.Result);
                }
                catch (Exception e)
                {
                    tcs.SetException(e);
                }
            };
            client.Headers[HttpRequestHeader.AcceptLanguage] = "en-US";
            client.Headers[HttpRequestHeader.UserAgent]      = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36";

            client.OpenReadCompleted += openReadEventHandler;
            client.OpenReadAsync(uri);

            return(tcs.Task);
        }
Beispiel #9
0
        /// <summary>
        /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// openreadcompletedeventhandler.BeginInvoke(sender, e, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginInvoke(this OpenReadCompletedEventHandler openreadcompletedeventhandler, Object sender, OpenReadCompletedEventArgs e, AsyncCallback callback)
        {
            if (openreadcompletedeventhandler == null)
            {
                throw new ArgumentNullException("openreadcompletedeventhandler");
            }

            return(openreadcompletedeventhandler.BeginInvoke(sender, e, callback, null));
        }
Beispiel #10
0
 /// <summary>
 /// Transport WebClient Request Operator
 /// </summary>
 /// <param name="uri">Request URi</param>
 /// <param name="openReaderEvent">OpenReadComplate Event</param>
 public static void TransportClientRequestOperaotr(string uri, OpenReadCompletedEventHandler openReaderEvent)
 {
     if (!string.IsNullOrEmpty(uri))
     {
         WebClient currentClient = new WebClient();
         currentClient.OpenReadAsync(new Uri(uri, UriKind.RelativeOrAbsolute));
         currentClient.OpenReadCompleted += openReaderEvent;
     }            
 }
Beispiel #11
0
        private void OnDownloadCompleted(OpenReadCompletedEventArgs e)
        {
            OpenReadCompletedEventHandler downloadCompletedHandler = DownloadCompleted;

            if (downloadCompletedHandler != null)
            {
                downloadCompletedHandler(this, e);
            }
        }
Beispiel #12
0
        protected virtual void OnOpenReadCompleted(OpenReadCompletedEventArgs args)
        {
            CompleteAsync();
            OpenReadCompletedEventHandler handler = OpenReadCompleted;

            if (handler != null)
            {
                handler(this, args);
            }
        }
        void _client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            DisconnectEvents();
            OpenReadCompletedEventHandler handler = OpenReadCompleted;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Beispiel #14
0
        protected static void VerificarDisponibilidadeWebService(OpenReadCompletedEventHandler callback)
        {
            IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
            if (appSettings.Contains("urlWebService"))
            {
                var webServiceURL = (string)appSettings["urlWebService"];
                WebServiceUtils.TestaDisponibilidadeWebService(webServiceURL, callback);                  
            }

            timer.Change(TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite);
        }        
Beispiel #15
0
        public WebClient ServiceGet(string api, OpenReadCompletedEventHandler eventHandler)
        {
            WebClient wc  = new WebClient();
            Uri       uri = new Uri(string.Format("{0}/{1}", PartyServiceUrl, api));

            wc.UseDefaultCredentials = false;
            wc.Credentials           = this.Credentials;
            wc.OpenReadCompleted    += eventHandler;
            wc.OpenReadAsync(uri);

            return(wc);
        }
Beispiel #16
0
        public static Task <Stream> TaskBasedOpenReadAsync(this WebClient webClient, Uri uri)
        {
            var tcs = new TaskCompletionSource <Stream>();

            OpenReadCompletedEventHandler callback = null;

            callback = (sender, args) =>
            {
                tcs.SetResult(args.Result);
                webClient.OpenReadCompleted -= callback;
            };

            webClient.OpenReadCompleted += callback;

            webClient.OpenReadAsync(uri);

            return(tcs.Task);
        }
Beispiel #17
0
    public static Task <Stream> OpenReadTaskAsync(this WebClient client, Uri uri)
    {
        var tcs = new TaskCompletionSource <Stream>();
        OpenReadCompletedEventHandler openReadEventHandler = null;

        openReadEventHandler = (sender, args) =>
        {
            try
            {
                tcs.SetResult(args.Result);
            }
            catch (Exception e)
            {
                tcs.SetException(e);
            }
        };
        client.OpenReadCompleted += openReadEventHandler;
        client.OpenReadAsync(uri);

        return(tcs.Task);
    }
    /// <summary>Opens a readable stream for the data downloaded from a resource, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI for which the stream should be opened.</param>
    /// <returns>A Task that contains the opened stream.</returns>
    public static Task <Stream> OpenReadTaskAsync(this WebClient webClient, Uri address)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource <Stream>(address);

        // Setup the callback event handler
        OpenReadCompletedEventHandler handler = null;

        handler = (sender, e) => TaskServices.HandleEapCompletion(tcs, true, e, () => e.Result, () => webClient.OpenReadCompleted -= handler);
        webClient.OpenReadCompleted += handler;

        // Start the async operation.
        try { webClient.OpenReadAsync(address, tcs); }
        catch
        {
            webClient.OpenReadCompleted -= handler;
            throw;
        }

        // Return the task that represents the async operation
        return(tcs.Task);
    }
        /// <summary>Opens a readable stream for the data downloaded from a resource, asynchronously.</summary>
        /// <param name="webClient">The WebClient.</param>
        /// <param name="address">The URI for which the stream should be opened.</param>
        /// <returns>A Task that contains the opened stream.</returns>
        public static Task <Stream> OpenReadTask(this WebClient webClient, Uri address)
        {
            TaskCompletionSource <Stream> tcs     = new TaskCompletionSource <Stream>(address);
            OpenReadCompletedEventHandler handler = null;

            handler = delegate(object sender, OpenReadCompletedEventArgs e) {
                EAPCommon.HandleCompletion <Stream>(tcs, e, () => e.Result, delegate {
                    webClient.OpenReadCompleted -= handler;
                });
            };
            webClient.OpenReadCompleted += handler;
            try
            {
                webClient.OpenReadAsync(address, tcs);
            }
            catch (Exception exception)
            {
                webClient.OpenReadCompleted -= handler;
                tcs.TrySetException(exception);
            }
            return(tcs.Task);
        }
    public static Task <Stream> OpenReadTaskAsync(this WebClient webClient, Uri address)
    {
        TaskCompletionSource <Stream> tcs     = new TaskCompletionSource <Stream>(address);
        OpenReadCompletedEventHandler handler = null;

        handler = delegate(object sender, OpenReadCompletedEventArgs e)
        {
            AsyncCompatLibExtensions.HandleEapCompletion <Stream>(tcs, true, e, () => e.Result, delegate
            {
                webClient.OpenReadCompleted -= handler;
            });
        };
        webClient.OpenReadCompleted += handler;
        try
        {
            webClient.OpenReadAsync(address, tcs);
        }
        catch
        {
            webClient.OpenReadCompleted -= handler;
            throw;
        }
        return(tcs.Task);
    }
Beispiel #21
0
        private static void GetData(Uri uri, bool async, OpenReadCompletedEventHandler handler)
        {
            // First, find the data.xml file, and apply it to the coverloader
            AutoResetEvent evt = async ? null : new AutoResetEvent(false);
            WebClient c = new WebClient();
            c.Headers["Cache-Control"] = "no-cache";

            c.OpenReadCompleted += handler;
            c.OpenReadAsync(uri, evt);

            if (evt != null) evt.WaitOne();
        }
Beispiel #22
0
        public void SignInAsync(FMWW.Http.Client client, string person, string personPassword, object userToken = default(object))
        {
            UploadValuesCompletedEventHandler onUploadValuesCompleted1 = null;

            onUploadValuesCompleted1 = new UploadValuesCompletedEventHandler(
                (o, args) =>
            {
                client.UploadValuesCompleted -= onUploadValuesCompleted1;
                var html = Encoding.UTF8.GetString(args.Result);
                //if (null != GetBtnLogOff(html))
                {
                    // ログイン成功
                    OnSignedIn(new SignedInEventArgs(args.Error, args.Cancelled, userToken));
                }
            });

            UploadValuesCompletedEventHandler onUploadValuesCompleted = null;

            onUploadValuesCompleted = new UploadValuesCompletedEventHandler(
                (o, args) =>
            {
                client.UploadValuesCompleted -= onUploadValuesCompleted;
                var html = Encoding.UTF8.GetString(args.Result);
                if (!Core.PC.Authentication.CanClickLogOff(html))
                {
                    // ログイン失敗
                    return;
                }
                // ログイン成功
                client.UploadValuesCompleted += onUploadValuesCompleted1;
                client.UploadValuesAsync(MainMenu.Url, MainMenuFactory.CreateInstance().Translate());
            });

            OpenReadCompletedEventHandler onOpenReadCompleted = null;

            onOpenReadCompleted = new OpenReadCompletedEventHandler(
                (o, args) =>
            {
                client.OpenReadCompleted -= onOpenReadCompleted;

                using (var reader = new StreamReader(args.Result, ShiftJIS))
                {
                    var html = reader.ReadToEnd();
                    #region HTMLに埋め込まれた、ユーザ名とパスワードを取得
                    Func <string, string, string> f = (ax, id) =>
                    {
                        if (!String.IsNullOrEmpty(ax))
                        {
                            return(ax);
                        }
                        var input = (new FMWW.Http.HTMLParser(html)).Document.getElementById(id);
                        if (input == null)
                        {
                            return(ax);
                        }
                        return(input.getAttribute("value"));
                    };
                    var userName = ClientQueryNames.Aggregate(String.Empty, f);
                    var password = PasswordQueryNames.Aggregate(String.Empty, f);
                    #endregion
                    client.UploadValuesCompleted += onUploadValuesCompleted;
                    client.UploadValuesAsync(UrlToSignIn, Core.PC.Authentication.BuildQueryToSignIn(html, userName, password, person, personPassword));
                }
            });

            client.OpenReadCompleted += onOpenReadCompleted;
            client.OpenReadAsync(UrlToSignIn, onOpenReadCompleted);
        }
Beispiel #23
0
 //Get data from an URI. Call the callback when we're done.
 void HTTPGet(string uri, OpenReadCompletedEventHandler callback)
 {
     WebClient downloader = new WebClient();
     downloader.OpenReadCompleted += callback;
     downloader.OpenReadAsync(new Uri(uri));
 }
Beispiel #24
0
 private void GetEntities(string entityName, OpenReadCompletedEventHandler orcDelegate)
 {
     Uri uri = new Uri(urls[entityName], UriKind.Absolute);
     WebClient webClient = new WebClient();
     webClient.OpenReadCompleted += orcDelegate;
     webClient.OpenReadAsync(uri, entityName);
 }
Beispiel #25
0
 public static void IniciarMonitoramentoWebService(OpenReadCompletedEventHandler callback)
 {
     timer = new Timer(_ => VerificarDisponibilidadeWebService(callback), null, TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite);
 }
 public static void TestaDisponibilidadeWebService(string url, OpenReadCompletedEventHandler callback)
 {
     var client = new WebClient();
     client.OpenReadCompleted += callback;
     client.OpenReadAsync(new Uri(url));           
 }      
Beispiel #27
0
        /// <summary>
        /// Called when [open read completed].
        /// </summary>
        /// <param name="webClient">The web client.</param>
        /// <param name="tcs">The TCS.</param>
        /// <param name="e">The <see cref="OpenReadCompletedEventArgs"/> instance containing the event data.</param>
        /// <param name="openReadCompletedHandler">The open read completed handler.</param>
        /// <param name="downloadProgressChangedHandler">The download progress changed handler.</param>
        private static void OnOpenReadCompleted(
            WebClient webClient,
            TaskCompletionSource<Stream> tcs,
            OpenReadCompletedEventArgs e,
            OpenReadCompletedEventHandler openReadCompletedHandler,
            DownloadProgressChangedEventHandler downloadProgressChangedHandler)
        {
            if (e.UserState != tcs)
                return;

            try
            {
                if (openReadCompletedHandler != null)
                    webClient.OpenReadCompleted -= openReadCompletedHandler;

                if (downloadProgressChangedHandler != null)
                    webClient.DownloadProgressChanged -= downloadProgressChangedHandler;
            }
            finally
            {
                if (e.Error != null)
                    tcs.TrySetException(e.Error);
                else if (e.Cancelled)
                    tcs.TrySetCanceled();
                else
                    tcs.TrySetResult(e.Result);
            }
        }