.NET Core 2.0 httpclient 请求卡顿解决方法

2019-04-16 15:20:15 浏览数 (1)

代码语言:javascript复制
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip,UseProxy = false };

            using (var http = new HttpClient( handler ))
            {
                if (headers != null)
                {
                    foreach (KeyValuePair<string,string> header in headers)
                    {
                        http.DefaultRequestHeaders.Add( header.Key,header.Value );
                    }
                }
                if (timeout > 0)
                {
                    http.Timeout = new TimeSpan( 0,0,timeout );
                }
                var response = http.GetAsync( address );
                if (!response.Result.IsSuccessStatusCode)
                {
                    return new FunctionResult<TData>() { Code = ApiStatusCode.ServerError,Message = "" };
                }
                var json = response.Result.Content.ReadAsStringAsync().Result;
                var result = json.FromApiJson<ApiData<TData>>();
                return new FunctionResult<TData> { Code = result.Result.Code,Message = result.Result.Msg,Data = result.Detail };
            }
代码语言:javascript复制
UseProxy = false必须加上这句,否则http请求会绕一圈

0 人点赞