Esempio n. 1
0
 public static bool TryUpload(string url, IEnumerable <HttpPostValue> blobs, HttpClientOptions options, HttpClientResponse response)
 {
     if (options == null || response == null)
     {
         return(false);
     }
     response.AsynchronousMode = false;
     return(HttpClient.InternalTryUpload(url, blobs, options, response, null));
 }
Esempio n. 2
0
 public static bool TryDownload(string url, HttpClientOptions options, HttpClientResponse response)
 {
     if (options == null || response == null)
     {
         return(false);
     }
     response.AsynchronousMode = false;
     return(HttpClient.InternalTryDownload(url, options, response, null));
 }
Esempio n. 3
0
 public static bool TryDownloadAsync(string url, HttpClientOptions options, HttpClientResponse response, HttpClientAsyncCallback callback)
 {
     if (options == null || response == null)
     {
         return(false);
     }
     response.AsynchronousMode = true;
     return(HttpClient.InternalTryDownload(url, options, response, callback));
 }
Esempio n. 4
0
        private static HttpWebRequest Create(string url, HttpClientOptions options)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.CookieContainer = options.CookieContainer;
            request.Referer         = string.IsNullOrEmpty(options.Referer) ? url : options.Referer;
            request.Accept          = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
            request.KeepAlive       = true;
            request.UserAgent       = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36";
            request.Headers.Add("Accept-Encoding: gzip, deflate");
            request.Headers.Add("DNT: 1");
            request.Headers.Add("Upgrade-Insecure-Requests: 1");
            request.Headers.Add("Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
            request.CachePolicy                  = options.CachePolicy;
            request.AllowAutoRedirect            = options.AllowAutoRedirect;
            request.Proxy                        = options.Proxy;
            request.Referer                      = options.Referer;
            request.Timeout                      = options.Timeout;
            request.MaximumAutomaticRedirections = options.MaximumAutomaticRedirections;
            request.AutomaticDecompression       = options.AutomaticDecompression;
            int[] range = options.Range;
            if (range != null && range.Length > 0)
            {
                if (range.Length > 1)
                {
                    request.AddRange(range[0], range[1]);
                }
                else
                {
                    request.AddRange(range[0]);
                }
            }
            NameValueCollection collection = options.Headers;

            if (collection != null)
            {
                WebHeaderCollection headers = request.Headers;
                foreach (string key in collection.AllKeys)
                {
                    headers.Add(collection.Get(key));
                }
            }
            return(request);
        }
Esempio n. 5
0
        private static bool InternalTryUpload(string url, IEnumerable <HttpPostValue> blobs, HttpClientOptions options, HttpClientResponse response, HttpClientAsyncCallback callback)
        {
            if (options == null || response == null)
            {
                return(false);
            }
            HttpWebRequest request = null;
            bool           success = false;

            try
            {
                request = HttpClient.Create(url, options);
                ParameterizedThreadStart copyto = delegate(object state)
                {
                    HttpWebResponse wr = null;
                    Stream          rs = null;
                    try
                    {
                        if (!HttpClient.PreparationUpload(request, blobs))
                        {
                            callback?.Invoke(HttpClientError.UnableToUploadBLOB, null, -1);
                        }
                        else
                        {
                            using (wr = (HttpWebResponse)request.GetResponse())
                            {
                                HttpClient.Fill(wr, response);
                                using (rs = wr.GetResponseStream())
                                {
                                    Func <byte[], int, bool> callbackt = null;
                                    if (callback != null)
                                    {
                                        callbackt = (buffer, len) =>
                                        {
                                            HttpClientError error = HttpClientError.Success;
                                            if (len < 0)
                                            {
                                                error = HttpClientError.ReadStreamInterruption;
                                            }
                                            return(callback(error, buffer, len));
                                        };
                                    }
                                    success = HttpClient.CopyTo(rs, response, callbackt);
                                }
                            }
                        }
                        request.Abort();
                    }
                    catch (Exception)
                    {
                        if (wr != null && rs == null)
                        {
                            callback?.Invoke(HttpClientError.UnableToSendRequest, null, -1);
                        }
                        else if (rs != null)
                        {
                            callback?.Invoke(HttpClientError.UnableToGetResponseStream, null, -1);
                        }
                        else if (wr == null)
                        {
                            callback?.Invoke(HttpClientError.UnableOpenURL, null, -1);
                        }
                        if (rs != null)
                        {
                            rs.Close();
                        }
                        if (wr != null)
                        {
                            wr.Close();
                        }
                        if (request != null)
                        {
                            request.Abort();
                        }
                    }
                };
                if (!response.AsynchronousMode)
                {
                    copyto(response);
                }
                else
                {
                    (new Thread(copyto)
                    {
                        IsBackground = true, Priority = ThreadPriority.Lowest
                    }).Start(response);
                }
                return(success);
            }
            catch (Exception)
            {
                if (request != null)
                {
                    request.Abort();
                }
                return(false);
            }
        }