Пример #1
0
        /// <summary>
        /// 发起 HTTP,需要调用方自行释放响应对象
        /// <para>
        /// 注:4.0 版本的 WebRequest 默认限制连接并发数。
        /// 如果要提高并发数,需自行设置 ServicePointManager.DefaultConnectionLimit 属性
        /// </para>
        /// </summary>
        /// <param name="uri">请求路径</param>
        /// <param name="configuration">HTTP 配置</param>
        /// <returns></returns>
        public static HttpWebResponse Send(string uri, HttpConfiguration configuration)
        {
            int tryTimes = configuration != null && configuration.TryTimes != null ? configuration.TryTimes.Value : 0;
            int sleep    = configuration != null && configuration.Sleep != null ? configuration.Sleep.Value : 500;

            return(WebHelper.Send(uri, configuration, tryTimes, sleep));
        }
Пример #2
0
        /// <summary>
        /// 使用 POST 方式提交请求,需要调用方自行释放响应对象
        /// </summary>
        /// <param name="uri">请求路径</param>
        /// <param name="configuration">HTTP 配置</param>
        public static T Delete <T>(string uri, HttpConfiguration configuration)
        {
            if (configuration == null)
            {
                configuration = new HttpConfiguration <T>();
            }
            configuration.Method = HttpMethod.Delete;
            var response = WebHelper.Send(uri, configuration);

            return(WebHelper.ReadAsResult <T>(response, configuration));
        }
Пример #3
0
        // 发起 HTTP请求
        static HttpWebResponse Send(string uri, HttpConfiguration configuration, int tryTimes, int sleep)
        {
            if (uri != null)
            {
#if netcore
                if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                }
#endif
#if net45
                if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                }
#endif
#if net40
                if (uri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
                }
#endif
            }

            try
            {
                // 创建请求
                var request = WebRequest.Create(uri) as HttpWebRequest;
                request.Method = "GET";
                //// 默认连接最大数=2,如果没有全局设置,则需要设置并发连接数
                //if (ServicePointManager.DefaultConnectionLimit == 2) request.ServicePoint.ConnectionLimit = 65532;
                if (configuration != null)
                {
                    request.ContentLength = 0;
                    request.Method        = configuration.Method.ToString().ToUpper();
                    if (configuration.Timeout != null)
                    {
                        request.Timeout = configuration.Timeout.Value;
                    }
                    if (configuration.ContentType != null)
                    {
                        request.ContentType = configuration.ContentType;
                    }
                    if (configuration.Accept != null)
                    {
                        request.Accept = configuration.Accept;
                    }
                    if (configuration.UserAgent != null)
                    {
                        request.UserAgent = configuration.UserAgent;
                    }
                    if (configuration.KeepAlive != null)
                    {
                        request.KeepAlive = configuration.KeepAlive.Value;
                    }
                    if (configuration.Proxy != null)
                    {
                        request.Proxy = configuration.Proxy;
                    }
                    if (configuration.Headers != null)
                    {
                        foreach (var kv in configuration.Headers)
                        {
                            request.Headers.Add(kv.Key, kv.Value);
                        }
                    }
                    if (configuration.CookieContainer != null)
                    {
                        request.CookieContainer = configuration.CookieContainer;
                    }

                    // 写入参数
                    string content = null;
                    if (configuration.Content != null && configuration.Content is string)
                    {
                        content = (string)configuration.Content;
                    }
                    else if (configuration.Content != null)
                    {
                        content = SerializeHelper.SerializeToJson(configuration.Content);
                    }
                    if (!string.IsNullOrEmpty(content))
                    {
                        var    encoding = configuration.Encoding ?? Encoding.UTF8;
                        byte[] bytes    = encoding.GetBytes(content);
                        request.ContentLength = bytes.Length;

                        using (var stream = request.GetRequestStream())
                        {
                            stream.Write(bytes, 0, bytes.Length);
                            stream.Close();
                        }
                    }
                }

                var response = request.GetResponse() as HttpWebResponse;
                return(response);
            }
            catch (WebException we)
            {
                tryTimes--;
                if (tryTimes > 0)
                {
                    System.Threading.Thread.Sleep(sleep);
                    return(WebHelper.Send(uri, configuration, tryTimes, sleep));
                }
                else
                {
                    WebHelper.ThrowWebException(we);
                    throw;
                }
            }
        }