zoukankan      html  css  js  c++  java
  • 同步+TASK异步请求

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading.Tasks;
    using System.Net;
    using System.Web;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            /// <summary>
            /// 按钮事件,异步事件,获取结果,非UI堵塞
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private async void button1_Click(object sender, EventArgs e)
            {
                string getstr=await GetHttpPostStringAsync(this.textBox1.Text);
                this.textBox2.Text=getstr;
            }
    
            /// <summary>
            /// 异步请求,返回请求结果
            /// </summary>
            /// <param name="Url">请求地址</param>
            /// <returns>参数列表</returns>
            public async Task<string> GetHttpPostStringAsync(string Url)
            {
                return await Task.Run<string>(() =>
                {
                    return  HttpPost(Url);
                });
    
                //Invoke(new Action(() => { }));
                //Action<int> act = new Action<int>((i) => {  i=i + 1; });  
                //Func<int, string> func = new Func<int, string>((i) =>
                //    {
                //        return (i + 1).ToString();
                //    });
            }
        
            /// <summary>
            /// HTTP POST请求
            /// </summary>
            /// <param name="Url">URL地址</param>
            /// <param name="postDataStr">参数字符串</param>
            /// <returns>返回结果</returns>
            public string HttpPost(string Url, string postDataStr = "")
            {
                string responseData = "";
                HttpWebResponse response;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postDataStr.Length;
                request.Timeout = 1000;
    
                try
                {
                    byte[] bs = Encoding.ASCII.GetBytes(postDataStr);
                    Stream reqStream = request.GetRequestStream();
    
                    reqStream.Write(bs, 0, bs.Length);
                    reqStream.Close();
                    response = (HttpWebResponse)request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseData = reader.ReadToEnd().ToString();
                    reader.Close();
                    request.Abort();
                    response.Close();
                    
                }
                catch (Exception ee)
                {
                    responseData = ee.ToString();
                }
                return responseData;
            }
    
            /// <summary>
            /// 按钮事件。请求结果。UI堵塞
            /// </summary>
            /// <param name="sender">按钮</param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                this.textBox2.Text = HttpPost(this.textBox1.Text);
            }
    
        }
    
    }
    

      

  • 相关阅读:
    3名百度 ,京东,腾讯被辞退的高级Android工程师现在怎么了?30岁真的是“罪”吗
    Python 开发者在迁移到 Go 前需要知道的事情
    centos7 常用命令--查看当前用户的4种方法
    Centos7找不到ifconfig和netstat命令
    Centos 7 修改日期和时间的命令
    如何利用MobaX同时处理多台虚拟机输入相同命令如何利用MobaX同时处理多台虚拟机输入相同命令
    配置坑了我好久:Logback按天生成文件失效
    quartz系列文章
    SpringBoot使用多实例QUARTZ出现重复执行问题
    IDEA多个springboot项目启动修改端口
  • 原文地址:https://www.cnblogs.com/Jeely/p/11004180.html
Copyright © 2011-2022 走看看