Exemplo n.º 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);
        }
Exemplo n.º 2
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)
        {
            lock (ObjLock)
            {
                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);
            }
        }
Exemplo n.º 3
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)
        {
            lock (ObjLock)
            {
                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);
            }
        }
Exemplo n.º 4
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);
Exemplo n.º 5
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);