BetterHttpClient.HttpClient.DownloadBytes C# (CSharp) Метод

DownloadBytes() публичный Метод

Excecute POST request.
public DownloadBytes ( string url, NameValueCollection data ) : byte[]
url string
data System.Collections.Specialized.NameValueCollection
Результат byte[]
        public byte[] DownloadBytes(string url, NameValueCollection data)
        {
            int counter = 0;
            WebException lastWebException = null;
            bool unkownProxy = Proxy.ProxyType == ProxyTypeEnum.Unknown;

            while (counter < NumberOfAttempts + (NumberOfAttempts < 2 && unkownProxy ? 1 : 0)) // min two try for unkonwn proxy type
            {
                try
                {
                    if (unkownProxy && counter % 2 == 0) // every odd try is as http proxy
                        Proxy.ProxyType = ProxyTypeEnum.Http;
                    else if (unkownProxy)
                        Proxy.ProxyType = ProxyTypeEnum.Socks;

                    byte[] result = data == null ? Encoding.GetBytes(DownloadString(url)) : UploadValues(url, data);
                    return result;
                }
                catch (WebException e)
                {
                    lastWebException = e;
                    counter++;
                }
            }

            if (unkownProxy)
                Proxy.ProxyType = ProxyTypeEnum.Unknown;
            // ReSharper disable once PossibleNullReferenceException
            throw lastWebException;
        }
    }

Same methods

HttpClient::DownloadBytes ( string url ) : byte[]

Usage Example

Пример #1
0
        private byte[] DownloadBytes(string url, NameValueCollection data, Proxy proxy, string requiredString = null, CookieContainer cookies = null, string referer = null)
        {
            if (requiredString == null)
            {
                requiredString = RequiredString;
            }

            HttpClient client = CreateHttpClient();

            client.Proxy = proxy;
            if (cookies != null)
            {
                client.Cookies = cookies;
            }
            if (referer != null)
            {
                client.Referer = referer;
            }

            try
            {
                return(client.DownloadBytes(url, data));
            }
            catch (WebException e)
            {
                if (e.Response != null && (e.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
                {
                    throw new WebPageNotFoundException();
                }
                proxy.IsOnline = false;
            }

            return(null);
        }