Ejemplo n.º 1
0
 private void CommonOperationUpload(WebClientExtended client)
 {
     CommonOperation(client);
     client.UploadProgressChanged += (sender, args) =>
     {
         ProgressUpdate(args.ProgressPercentage, args.BytesReceived, args.TotalBytesToReceive, args.BytesSent, args.TotalBytesToSend);
     };
 }
Ejemplo n.º 2
0
        private Task <string> DownloadStringTaskAsync()
        {
            Task <string> task;

            using (var client = new WebClientExtended())
            {
                CommonOperationDownload(client);
                task = client.DownloadStringTaskAsync(RemoteAddress);
            }
            return(task);
        }
Ejemplo n.º 3
0
        private Task DownloadFileTaskAsync()
        {
            Task task;

            using (var client = new WebClientExtended())
            {
                CommonOperationDownload(client);
                task = client.DownloadFileTaskAsync(RemoteAddress, LocalAddress);
            }
            return(task);
        }
Ejemplo n.º 4
0
        private void CommonOperation(WebClientExtended client)
        {
            if (Proxy)
            {
                client.Proxy = new WebProxy();
            }

            if (Credentials != null)
            {
                client.Credentials = Credentials;
            }

            if (_timeoutModified)
            {
                client.Timeout = Timeout;
            }

            if (!string.IsNullOrWhiteSpace(WebHeaderAccept))
            {
                client.Headers["Accept"] = WebHeaderAccept;
            }
        }
Ejemplo n.º 5
0
        public void Download()
        {
            using (var client = new WebClientExtended())
            {
                client.BaseAddress = BaseAddress;
                client.QueryString.Clear();
                client.QueryString = QueryString;
                client.Encoding    = Encoding;

                // set user agent
                if (!string.IsNullOrWhiteSpace(UserAgent))
                {
                    client.Headers["User-Agent"] = UserAgent;

                    // Create web client simulating IE6.
                    //x client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)" + " (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                    //x client.Headers["User-Agent"] = "Googlebot/2.1 (+http://www.googlebot.com/bot.html)";
                }

                // accept-encoding headers
                if (GZip)
                {
                    client.Headers["Accept-Encoding"] = "gzip";
                }

                client.Timeout = Timeout;

                try
                {
                    // header
                    if (CheckHead || DownloadType == WwwClientDownloadType.Head)
                    {
                        client.IsHeadOnly = true;

                        ContentByte = new Collection <byte>(new Collection <byte>(client.DownloadData(client.BaseAddress)));

                        client.IsHeadOnly = false;

                        if (DownloadType != WwwClientDownloadType.Head &&
                            client.ResponseHeaders["content-type"].StartsWith(@"text/", StringComparison.Ordinal))
                        {
                            DownloadType = WwwClientDownloadType.String;
                        }
                    }


                    // check for text/html
                    switch (DownloadType)
                    {
                    case WwwClientDownloadType.Data:
                        ContentByte = new Collection <byte>(new Collection <byte>(client.DownloadData(client.BaseAddress)));
                        break;

                    case WwwClientDownloadType.String:
                        ContentString = client.DownloadString(client.BaseAddress);
                        break;

                    case WwwClientDownloadType.File:
                        client.DownloadFile(client.BaseAddress, ContentPath);
                        break;

                    case WwwClientDownloadType.Head:
                        // already received the data as header
                        break;
                    }

                    // Get response header.
                    ContentEncoding = client.ResponseHeaders["Content-Encoding"];

                    // Get content type
                    ContentType = client.ResponseHeaders["content-type"];
                }
                catch (WebException ex)
                {
                    if (ex.Response != null)
                    {
                        var            response   = (HttpWebResponse)ex.Response;
                        HttpStatusCode statusCode = response.StatusCode;
                        switch (statusCode)
                        {
                        case HttpStatusCode.OK:
                        case HttpStatusCode.Accepted:
                        case HttpStatusCode.Created:
                        case HttpStatusCode.NoContent:
                        case HttpStatusCode.NotFound:
                        case HttpStatusCode.Unauthorized:
                        case HttpStatusCode.Forbidden:
                        case HttpStatusCode.PreconditionFailed:
                        case HttpStatusCode.ServiceUnavailable:
                        case HttpStatusCode.InternalServerError:
                            throw new WebException();
                        }
                    }
                    throw new WebException();
                }
            }
        }