Beispiel #1
0
        public static void DownloadCaptchaImage(string website, Action <ImageResource, string> onSuccess)
        {
            using (WebClient client = new WebClient())
            {
                ResourceManager resource = Core.Instance.ResourceManager;
                resource.DownloadString(website + "captcha/captcha.ashx",
                                        delegate(IResourceObject res)
                {
                    StringResource str = (StringResource)res;
                    // download the image

                    resource.DownloadImage(website + "captcha/getcaptcha.ashx?CaptchaID=" + str.String,
                                           delegate(IResourceObject nres)
                    {
                        if (onSuccess != null)
                        {
                            onSuccess((ImageResource)nres, str.String);
                        }
                    }, null);
                }, null);
            }
        }
        private void update()
        {
            lock (locker)
            {
                try
                {
                    startQueued();

                    for (int i = 0; i < downloading.Count; i++)
                    {
                        var down = downloading[i];
                        switch (down.dtype)
                        {
                        case LoadType.File:
                        {
                            Task            task = down.Task;
                            IResourceObject res  = null;

                            if (task.Status == TaskStatus.RanToCompletion)
                            {
                                switch (down.type)
                                {
                                case ResourceType.Image:
#if ANDROID
                                    Task <Bitmap> t = (Task <Bitmap>)task;
                                    res = new ImageResource(t.Result);
#elif IOS
                                    Task <UIImage> t = (Task <UIImage>)task;
                                    res = new ImageResource(t.Result);
#else
                                    throw new NotImplementedException();
#endif
                                    if (down.store)
                                    {
                                        resources.Add(down.path.OriginalString, res);
                                    }

                                    downloading.RemoveAt(i);
                                    i--;
                                    for (int j = 0; j < down.Callbacks.Count; j++)
                                    {
                                        Delegate del = (Delegate)down.Callbacks[j];
                                        del.DynamicInvoke(res);
                                        //down.Callbacks[j](res);
                                    }
                                    break;
                                }
                            }
                        }
                        break;

                        case LoadType.Download:
                        {
                            down.client.Dispose();
                            var task = (Task <byte[]>)down.Task;
                            if (task.Status == TaskStatus.RanToCompletion)
                            {
                                // success
                                IResourceObject res = null;
                                switch (down.type)
                                {
                                case ResourceType.Image:
                                    res = new ImageResource(task.Result);
                                    if (task.Result.Length < 10)
                                    {
                                        // decode error
                                        string error = WebUtil.GetASCIIString(task.Result);
                                        continue;
                                    }

                                    break;

                                case ResourceType.String:
                                    res = new StringResource(task.Result);
                                    break;
                                }

                                if (down.store)
                                {
                                    resources.Add(down.path.OriginalString, res);
                                }

                                downloading.RemoveAt(i);
                                i--;

                                for (int j = 0; j < down.Callbacks.Count; j++)
                                {
                                    Delegate del = (Delegate)down.Callbacks[j];
                                    del.DynamicInvoke(res);
                                    //down.Callbacks[j](res);
                                }
                            }
                            else if (task.Status == TaskStatus.Canceled ||
                                     task.Status == TaskStatus.Faulted)
                            {
                                // failure
                                downloading.RemoveAt(i);
                                i--;

                                for (int j = 0; j < down.failureCallbacks.Count; j++)
                                {
                                    down.failureCallbacks[j]();
                                }
                            }
                        }
                        break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                    if (ex is WebException)
                    {
                        WebException web = (WebException)ex;
                        using (Stream stream = web.Response.GetResponseStream())
                        {
                            StreamReader reader = new StreamReader(stream);
                            string       data   = reader.ReadToEnd();
                        }
                    }
                }
            }
        }