示例#1
0
        // 发起 HTTP请求
        static async Task <HttpResponseMessage> SendAsync(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
            }

            // 初始化 HTTP 消息处理程序
            HttpClientHandler handler = null;
            if (configuration != null)
            {
                if (configuration.Proxy != null)
                {
                    handler = new HttpClientHandler
                    {
                        Proxy    = configuration.Proxy,
                        UseProxy = true
                    }
                }
                ;
                if (configuration.CookieContainer != null)
                {
                    if (handler != null)
                    {
                        handler.CookieContainer = configuration.CookieContainer;
                    }
                    else
                    {
                        handler = new HttpClientHandler {
                            CookieContainer = configuration.CookieContainer
                        }
                    };
                }
            }
            var client = handler != null ? new HttpClient(handler) : new HttpClient();
            if (configuration != null && configuration.Timeout != null)
            {
                client.Timeout = new TimeSpan(0, 0, 0, 0, configuration.Timeout.Value);
            }

            // 初始化 HTTP 请求
            var method = System.Net.Http.HttpMethod.Get;
            if (configuration != null)
            {
                if (configuration.Method == HttpMethod.Get)
                {
                    method = System.Net.Http.HttpMethod.Get;
                }
                else if (configuration.Method == HttpMethod.Post)
                {
                    method = System.Net.Http.HttpMethod.Post;
                }
                else if (configuration.Method == HttpMethod.Put)
                {
                    method = System.Net.Http.HttpMethod.Put;
                }
                else if (configuration.Method == HttpMethod.Delete)
                {
                    method = System.Net.Http.HttpMethod.Delete;
                }
                else if (configuration.Method == HttpMethod.Head)
                {
                    method = System.Net.Http.HttpMethod.Head;
                }
                else if (configuration.Method == HttpMethod.Trace)
                {
                    method = System.Net.Http.HttpMethod.Trace;
                }
                else if (configuration.Method == HttpMethod.Options)
                {
                    method = System.Net.Http.HttpMethod.Options;
                }
            }
            var request = new HttpRequestMessage(method, uri);
            if (configuration != null)
            {
                if (configuration.Accept != null)
                {
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(configuration.Accept));
                }
                if (configuration.UserAgent != null)
                {
                    request.Headers.UserAgent.Add(new ProductInfoHeaderValue(configuration.UserAgent, null));
                }
                if (configuration.KeepAlive != null)
                {
                    request.Headers.Connection.Add("keep-alive");
                }

                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 (content != null)
                {
                    var encoding    = configuration.Encoding ?? Encoding.UTF8;
                    var contentType = configuration.ContentType ?? "application/json";
                    var httpContent = new StringContent(content, encoding ?? Encoding.UTF8, contentType);
                    request.Content = httpContent;
                }

                if (configuration.Headers != null)
                {
                    // Authorization TODO
                    string scheme = null;
                    string token  = null;
                    foreach (var kv in configuration.Headers)
                    {
                        if (string.Equals(kv.Key, "scheme", StringComparison.CurrentCultureIgnoreCase))
                        {
                            scheme = kv.Key;
                        }
                        else if (string.Equals(kv.Key, "token", StringComparison.CurrentCultureIgnoreCase))
                        {
                            token = kv.Key;
                        }
                        else
                        {
                            request.Headers.Add(kv.Key, kv.Value);
                        }
                    }

                    if (token != null)
                    {
                        if (scheme == null)
                        {
                            scheme = "Basic";
                        }
                        request.Headers.Authorization = new AuthenticationHeaderValue("Basic", token);
                    }
                }
            }

            try
            {
                var response = await client.SendAsync(request);//.ConfigureAwait(false);

                return(response);
            }
            catch (WebException we)
            {
                if (handler != null)
                {
                    handler.Dispose();
                }
                if (client != null)
                {
                    client.Dispose();
                }

                tryTimes--;
                if (tryTimes > 0)
                {
                    System.Threading.Thread.Sleep(sleep);
                    return(await WebHelper.SendAsync(uri, configuration, tryTimes, sleep));
                }
                else
                {
                    WebHelper.ThrowWebException(we);
                    throw;
                }
            }

            //MultipartFormDataContent => multipart/form-data
            //FormUrlEncodedContent => application/x-www-form-urlencoded
            //StringContent => application/json
            //StreamContent => binary
        }