Пример #1
0
        private HttpWebRequest BuildRequest(string url, IEnumerable<HTTPHeader> headers, HTTPOptions options)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);

            request.Proxy = this.currentProxy ?? request.Proxy;

            headers = headers ?? Headers;
            options = options ?? Options;

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    switch (header.PresetKey)
                    {
                        default:                            request.Headers.Add(header.CustomKey, header.Value);
                            break;
                        case HTTPHeaderKey.UserAgent:       request.UserAgent = header.Value;
                            break;
                        case HTTPHeaderKey.Accept:          request.Accept = header.Value;
                            break;
                        case HTTPHeaderKey.Host:            request.Host = header.Value;
                            break;
                        case HTTPHeaderKey.Referer:         request.Referer = header.Value;
                            break;
                        case HTTPHeaderKey.ContentType:     request.ContentType = header.Value;
                            break;
                        case HTTPHeaderKey.ContentLength:   request.ContentLength = long.Parse(header.Value);
                            break;
                        case HTTPHeaderKey.Connection:

                            if (header.Value != "keep-alive" && header.Value != "close")
                                request.Connection = header.Value;
                            else
                                request.KeepAlive = header.Value == "keep-alive";

                            break;
                    }
                }
            }

            if (options != null)
            {
                request.AllowAutoRedirect = options.AllowAutoRedirect;
                request.ServicePoint.Expect100Continue = options.Expect100Continue;
                request.Timeout = options.Timeout;
                request.Proxy = options.Proxy;
            }

            request.Accept = request.Accept ?? this.defaultRequest.Accept;
            request.UserAgent = request.UserAgent ?? this.defaultRequest.UserAgent;

            if (string.IsNullOrEmpty(request.Headers[HttpRequestHeader.AcceptLanguage]))
                request.Headers.Add(
                    header: HttpRequestHeader.AcceptLanguage,
                    value:  this.defaultRequest.Headers[HttpRequestHeader.AcceptLanguage]);

            request.Referer = request.Referer ?? Referer;

            return request;
        }
Пример #2
0
 /// <summary>
 /// Async POST request.
 /// </summary>
 /// <param name="url">URL.</param>
 /// <param name="parameters">Query.</param>
 /// <param name="rawData">Raw data before building query string.</param>
 /// <param name="cookies">Cookies.</param>
 /// <param name="headers">HTTP request headers.</param>
 /// <param name="options">HTTP request options (auto redirect, timeout, etc.).</param>
 /// <param name="encoding">Encoding.</param>
 /// <param name="tryTimes">Number of attempts when getting response has failed.</param>
 /// <returns>Response task.</returns>
 public Task<string> PostAsync(
     string                      url,
     Dictionary<string, string>  parameters  = null,
     string                      rawData     = null,
     CookieContainer             cookies     = null,
     List<HTTPHeader>            headers     = null,
     HTTPOptions                 options     = null,
     Encoding                    encoding    = null,
     int                         tryTimes    = 0)
 {
     return Task.Factory.StartNew(() => Post(url, parameters, rawData, cookies, headers, options, encoding, tryTimes));
 }
Пример #3
0
        /// <summary>
        /// Multipart POST request.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="parameters">Query.</param>
        /// <param name="rawData">Raw data before building query string.</param>
        /// <param name="files">Dictionary of files where key is name and value is path.</param>
        /// <param name="cookies">Cookies.</param>
        /// <param name="headers">HTTP request headers.</param>
        /// <param name="options">HTTP request options (auto redirect, timeout, etc.).</param>
        /// <param name="encoding">Encoding.</param>
        /// <param name="tryTimes">Number of attempts when getting response has failed.</param>
        /// <returns>Response string.</returns>
        public string Multipart(
            string                      url,
            Dictionary<string, string>  parameters  = null,
            string                      rawData     = null,
            Dictionary<string, string>  files       = null,
            CookieContainer             cookies     = null,
            List<HTTPHeader>            headers     = null,
            HTTPOptions                 options     = null,
            Encoding                    encoding    = null,
            int                         tryTimes    = 0)
        {
            try
            {
                BeginWatching();

                var boundary = new string('-', MULTIPART_BOUNDARY_LINE_LENGTH) + DateTime.Now.Ticks.ToString("x");

                var postData = parameters != null ? BuildMultipartQuery(parameters, boundary, files) : rawData;

                var request = BuildRequest(url, headers, options);

                request.Method = "POST";
                request.CookieContainer = cookies ?? Cookies;
                request.ContentType = "multipart/form-data; boundary=" + boundary;

                Cookies = cookies ?? Cookies;

                var bytes = (encoding ?? Encoding.Default).GetBytes(postData ?? string.Empty);

                request.ContentLength = bytes.Length;

                using (var requestStream = request.GetRequestStream())
                    requestStream.Write(bytes, 0, bytes.Length);

                return GetResponseString(request, encoding);
            }
            catch (Exception exception)
            {
                this.exceptions.Add(exception);

                if (ProxyAutoswitching)
                    SwitchProxy();

                if (tryTimes > 0)
                    return Multipart(url, parameters, rawData, files, cookies, headers, options, encoding, --tryTimes);

                if (!SilentMode)
                    throw;

                return null;
            }
        }
Пример #4
0
        /// <summary>
        /// POST request.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="parameters">Query.</param>
        /// <param name="rawData">Raw data before building query string.</param>
        /// <param name="cookies">Cookies.</param>
        /// <param name="headers">HTTP request headers.</param>
        /// <param name="options">HTTP request options (auto redirect, timeout, etc.).</param>
        /// <param name="encoding">Encoding.</param>
        /// <param name="tryTimes">Number of attempts when getting response has failed.</param>
        /// <returns>Response string.</returns>
        public string Post(
            string                      url,
            Dictionary<string, string>  parameters  = null,
            string                      rawData     = null,
            CookieContainer             cookies     = null,
            List<HTTPHeader>            headers     = null,
            HTTPOptions                 options     = null,
            Encoding                    encoding    = null,
            int                         tryTimes    = 0)
        {
            try
            {
                BeginWatching();

                var postData = parameters != null ? BuildQuery(parameters) : rawData;

                var request = BuildRequest(url, headers, options);

                request.Method = "POST";
                request.ContentType = request.ContentType ?? DEFAULT_POST_CONTENT_TYPE;
                request.CookieContainer = cookies ?? Cookies;

                Cookies = cookies ?? Cookies;

                var bytes = (encoding ?? Encoding.Default).GetBytes(postData ?? string.Empty);

                request.ContentLength = bytes.Length;

                using (var requestStream = request.GetRequestStream())
                    requestStream.Write(bytes, 0, bytes.Length);

                return GetResponseString(request, encoding);
            }
            catch (Exception exception)
            {
                this.exceptions.Add(exception);

                if (ProxyAutoswitching)
                    SwitchProxy();

                if (tryTimes > 0)
                    return Post(url, parameters, rawData, cookies, headers, options, encoding, --tryTimes);

                if (!SilentMode)
                    throw;

                return null;
            }
        }
Пример #5
0
        /// <summary>
        /// GET request.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="parameters">Query.</param>
        /// <param name="cookies">Cookies.</param>
        /// <param name="headers">HTTP request headers.</param>
        /// <param name="options">HTTP request options (auto redirect, timeout, etc.).</param>
        /// <param name="encoding">Encoding.</param>
        /// <param name="tryTimes">Number of attempts when getting response has failed.</param>
        /// <returns>Response string.</returns>
        public string Get(
            string                      url,
            Dictionary<string, string>  parameters  = null,
            CookieContainer             cookies     = null,
            List<HTTPHeader>            headers     = null,
            HTTPOptions                 options     = null,
            Encoding                    encoding    = null,
            int                         tryTimes    = 0)
        {
            try
            {
                BeginWatching();

                var queryString = ParseQuery(url);
                var possibleQuery = BuildQuery(parameters);

                url = url.Split('?')[0];

                var request
                    = BuildRequest(
                        string.Format(
                            "{0}{1}{2}{3}{4}",
                            url,
                            string.IsNullOrEmpty(queryString) && string.IsNullOrEmpty(possibleQuery)
                                ? string.Empty
                                : "?",
                            queryString,
                            !string.IsNullOrEmpty(queryString) && !string.IsNullOrEmpty(possibleQuery)
                                ? "&"
                                : string.Empty,
                            possibleQuery),
                        headers,
                        options);

                request.Method = "GET";
                request.CookieContainer = cookies ?? Cookies;

                Cookies = cookies ?? Cookies;

                return GetResponseString(request, encoding);
            }
            catch (Exception exception)
            {
                this.exceptions.Add(exception);

                if (ProxyAutoswitching)
                    SwitchProxy();

                if (tryTimes > 0)
                    return Get(url, parameters, cookies, headers, options, encoding, --tryTimes);

                if (!SilentMode)
                    throw;

                return null;
            }
        }