由于HttpClient类中的http调用方法都是使用的异步调用模式,在并发量大线程多的场景下可能会出现调用被长期阻塞的情况,会给我们的系统带来风险和隐患,所以请大家排查一下代码,使用HttpWebRequest的同步方式来替代HttpClient的异步方式。
1 HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest; 2 request.ContentType = "application/json"; 3 request.Method = "Get"; 4 request.Timeout = 10000;//10秒超时时间 5 6 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 7 { 8 using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) 9 { 10 string responseMsg = streamReader.ReadToEnd(); 11 } 12 }