Beispiel #1
0
        /// <summary>
        ///     带重试的发送。
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">URL。</param>
        /// <param name="json">JSON。</param>
        /// <param name="retryTimes">重试次数。</param>
        /// <returns></returns>
        internal static HttpResponse PostWithRetry(this HttpClient client, ApiUrl url, JObject json, int retryTimes)
        {
            HttpResponse response;

            do
            {
                response = client.Post(url, json);
                retryTimes++;
            } while (retryTimes >= 0 && response.StatusCode != HttpStatusCode.OK);
            return(response);
        }
Beispiel #2
0
        /// <summary>
        ///     发送GET请求。
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">URL。</param>
        /// <param name="args">附加的参数。</param>
        /// <returns></returns>
        public static HttpResponse Get(this HttpClient client, ApiUrl url, params object[] args)
        {
            var referer = client.DefaultRequestHeaders.Referrer;

            if (url.Referer != null)
            {
                client.DefaultRequestHeaders.Referrer = new Uri(url.Referer);
            }

            var response = client.GetAsync(url.BuildUrl(args));

            response.Wait();
            // 复原client
            client.DefaultRequestHeaders.Referrer = referer;

            return(response.Result);
        }
Beispiel #3
0
        /// <summary>
        ///     发送GET请求。
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">URL。</param>
        /// <param name="allowAutoRedirect">允许自动重定向。</param>
        /// <param name="args">附加的参数。</param>
        /// <returns></returns>
        public static HttpResponse Get(this HttpClient client, ApiUrl url, bool?allowAutoRedirect, params object[] args)
        {
            var referer      = client.Request.Referer;
            var autoRedirect = client.Request.AllowAutoRedirect;

            client.Request.Referer = url.Referer;
            if (allowAutoRedirect.HasValue)
            {
                client.Request.AllowAutoRedirect = allowAutoRedirect.Value;
            }
            var response = client.Get(url.BuildUrl(args));

            // 复原client
            client.Request.Referer           = referer;
            client.Request.AllowAutoRedirect = autoRedirect;

            return(response);
        }
Beispiel #4
0
        /// <summary>
        ///     发送POST请求。
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">URL。</param>
        /// <param name="json">JSON。</param>
        /// <returns></returns>
        internal static HttpResponse Post(this HttpClient client, ApiUrl url, JObject json)
        {
            var hasOrigin = client.DefaultRequestHeaders.TryGetValues("Origin", out IEnumerable <string> origin);

            if (url.Referer != null)
            {
                client.DefaultRequestHeaders.Referrer = new Uri(url.Referer);
            }
            if (client.DefaultRequestHeaders.Contains("Origin"))
            {
                client.DefaultRequestHeaders.Remove("Origin");
                client.DefaultRequestHeaders.Add("Origin", url.Origin);
            }
            else
            {
                client.DefaultRequestHeaders.Add("Origin", url.Origin);
            }

            //List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>
            //{
            //    new KeyValuePair<string, string>("r", json.ToString(Formatting.None))
            //};
            //HttpContent hc = new FormUrlEncodedContent(paramList);

            HttpContent hc = new StringContent($"r={WebUtility.UrlEncode(json.ToString(Formatting.None))}", Encoding.UTF8);

            hc.Headers.ContentType = MediaTypeHeaderValue.Parse("application/ x-www-form-urlencoded; charset=UTF-8");
            var response = client.PostAsync(url.Url, hc);

            response.Wait();

            // 复原client
            if (hasOrigin)
            {
                client.DefaultRequestHeaders.Remove("Origin");
                client.DefaultRequestHeaders.Add("Origin", origin);
            }
            else
            {
                client.DefaultRequestHeaders.Remove("Origin");
            }

            return(response.Result);
        }
Beispiel #5
0
        /// <summary>
        ///     发送POST请求。
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">URL。</param>
        /// <param name="json">JSON。</param>
        /// <param name="timeout">超时。</param>
        /// <returns></returns>
        internal static HttpResponse Post(this HttpClient client, ApiUrl url, JObject json, int timeout)
        {
            object origin;
            var    hasOrigin = client.Request.RawHeaders.TryGetValue("Origin", out origin);
            var    time      = client.Request.Timeout;

            client.Request.Referer = url.Referer;
            if (client.Request.RawHeaders.ContainsKey("Origin"))
            {
                client.Request.RawHeaders["Origin"] = url.Origin;
            }
            else
            {
                client.Request.AddExtraHeader("Origin", url.Origin);
            }
            if (timeout > 0)
            {
                client.Request.Timeout = timeout;
            }

            var response = client.Post(url.Url, "r=" + HttpUtility.UrlEncode(json.ToString(Formatting.None)),
                                       "application/x-www-form-urlencoded; charset=UTF-8");

            // 复原client
            if (hasOrigin)
            {
                client.Request.RawHeaders["Origin"] = origin;
            }
            else
            {
                client.Request.RawHeaders.Remove("Origin");
            }
            client.Request.Timeout = time;

            return(response);
        }
Beispiel #6
0
 /// <summary>
 ///     发送POST请求。
 /// </summary>
 /// <param name="client"></param>
 /// <param name="url">URL。</param>
 /// <param name="json">JSON。</param>
 /// <returns></returns>
 public static HttpResponse Post(this HttpClient client, ApiUrl url, JObject json) => client.Post(url, json, -1);
Beispiel #7
0
 /// <summary>
 ///     发送GET请求。
 /// </summary>
 /// <param name="client"></param>
 /// <param name="url">URL。</param>
 /// <param name="args">附加的参数。</param>
 /// <returns></returns>
 public static HttpResponse Get(this HttpClient client, ApiUrl url, params object[] args)
 => client.Get(url, null, args);