标签:work table bsp 调用 另一个 set pen http 设置
不知道用什么词形容,就叫它假异步吧。
写异步方法,async 和 await 要一路写到底,否则就是假异步,并不能提高请求线程池的吞吐量。
/// <summary> /// HttpGet /// </summary> /// <param name="url">url路径名称</param> /// <param name="cookie">cookie</param> public static string HttpGet(string url, CookieContainer cookie = null, WebHeaderCollection headers = null) { try { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = cookie; request.Method = "GET"; request.ContentType = "text/plain;charset=utf-8"; if (headers != null) { foreach (string key in headers.Keys) { request.Headers.Add(key, headers[key]); } } //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } catch (Exception ex) { LogUtil.Error(ex); return string.Empty; } }
HttpUtil.HttpGetAsync方法:
/// <summary> /// HttpGetAsync /// </summary> /// <param name="url">url路径名称</param> /// <param name="cookie">cookie</param> public static async Task<string> HttpGetAsync(string url, CookieContainer cookie = null, WebHeaderCollection headers = null) { try { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = cookie; request.Method = "GET"; request.ContentType = "text/plain;charset=utf-8"; if (headers != null) { foreach (string key in headers.Keys) { request.Headers.Add(key, headers[key]); } } //发送请求并获取相应回应数据 HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } catch (Exception ex) { LogUtil.Error(ex); return string.Empty; } }
测试代码:
/// <summary> /// 测试1 /// </summary> private async void button1_Click(object sender, EventArgs e) { //task是自己写的独立线程池,为了防止测试过程对CLR线程池和异步线程池造成干扰 _task.Run(() => { Thread.Sleep(200); int workerThreads1, completionPortThreads1, workerThreads2, completionPortThreads2; ThreadPool.GetMaxThreads(out workerThreads1, out completionPortThreads1); ThreadPool.GetAvailableThreads(out workerThreads2, out completionPortThreads2); Log("假异步 已使用辅助线程:" + (workerThreads1 - workerThreads2) + ", 已使用异步线程:" + (completionPortThreads1 - completionPortThreads2)); }); string str = await GetDataAsync(); } /// <summary> /// 测试2 /// </summary> private async void button2_Click(object sender, EventArgs e) { //task是自己写的独立线程池,为了防止测试过程对CLR线程池和异步线程池造成干扰 _task.Run(() => { Thread.Sleep(200); int workerThreads1, completionPortThreads1, workerThreads2, completionPortThreads2; ThreadPool.GetMaxThreads(out workerThreads1, out completionPortThreads1); ThreadPool.GetAvailableThreads(out workerThreads2, out completionPortThreads2); Log("真异步 已使用辅助线程:" + (workerThreads1 - workerThreads2) + ", 已使用异步线程:" + (completionPortThreads1 - completionPortThreads2)); }); string str = await GetDataAsync2(); } /// <summary> /// 假异步 /// </summary> private async Task<string> GetDataAsync() { return await Task.Run<string>(() => { //接口耗时大约1秒 return HttpUtil.HttpGet("http://localhost:8500/api/test/TestGet?val=1", null, null); }); } /// <summary> /// 真异步 /// </summary> /// <returns></returns> private async Task<string> GetDataAsync2() { //接口耗时大约1秒 return await HttpUtil.HttpGetAsync("http://localhost:8500/api/test/TestGet?val=1", null, null); }
测试截图:
我想知道 WebRequest 类的 GetResponseAsync 方法是怎么实现的
标签:work table bsp 调用 另一个 set pen http 设置
原文地址:https://www.cnblogs.com/s0611163/p/13415212.html