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);
} } }

  

同步+TASK异步请求的更多相关文章

  1. C# ASP.NET Core使用HttpClient的同步和异步请求

    引用 Newtonsoft.Json // Post请求 public string PostResponse(string url,string postData,out string status ...

  2. ASIHTTPRequest系列(一):同步和异步请求

    ASIHTTPRequest系列(一):同步和异步请求 发表于8个月前(2013-11-27 19:21)   阅读(431) | 评论(0) 6人收藏此文章, 我要收藏 赞0 ASIHTTPRequ ...

  3. ASP.NET Core使用HttpClient的同步和异步请求

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.I ...

  4. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第四节:同步与异步请求方式

    前两节,我们对WebRequest和WebResponse这两个类做了介绍,但两者还相对独立.本节,我们来说说如何将两者结合起来,方式有哪些,有什么不同. 1.4.1 说结合,无非就是我们如何发送一个 ...

  5. js中同步与异步请求方式

    异步请求方式: $.ajax({ url : 'your url', data:{name:value}, cache : false, async : true, type : "POST ...

  6. Jquery的同步和异步请求

    1 异步请求:    1.1 $.ajax       $.ajax({                url : 'your url',                data:{name:valu ...

  7. Swift3.0:Get/Post同步和异步请求

    一.介绍 Get和Post区别: Get是从服务器上获取数据,Post是向服务器发送数据. 对于Get方式,服务端用Request.QueryString获取变量的值,对于Post方式,服务端用Req ...

  8. $.post 和 $.get 设置同步和异步请求

    由于$.post() 和 $.get() 默认是 异步请求,如果需要同步请求,则可以进行如下使用:在$.post()前把ajax设置为同步:$.ajaxSettings.async = false;在 ...

  9. ASIHTTP 框架,同步、 异步请求、 上传 、 下载

    ASIHTTPRequest详解 ASIHTTPRequest 是一款极其强劲的 HTTP 访问开源项目.让简单的 API 完成复杂的功能,如:异步请求,队列请求,GZIP 压缩,缓存,断点续传,进度 ...

随机推荐

  1. element-UI 点击一行,背景色变化

    代码: @row-click="rowClick" 当某一行被点击时会触发该事件 :row-class-name="tableRowClassName"  可以 ...

  2. SQL Server SQLBindCol

    说明 编辑 版本引入:ODBC 1.0 遵从标准:ISO 92 功能说明: SQLBindCol将应用程序的数据缓冲绑定到结果集的各列 函数原型: SQLRETURN SQLBindCol( SQLH ...

  3. thinkphp读取配置

    无论何种配置文件,定义了配置文件之后,都统一使用系统提供的C方法(可以借助Config单词来帮助记忆)来读取已有的配置. 获取已经设置的参数值:C('参数名称') 例如, $model = C('UR ...

  4. iptables开通某些端口

    #!/bin/bash #define all variance or parameter WAH_INT="eth0" WAH_INT_IP="222.222.101. ...

  5. GDI+图像与GDI位图的相互转换

    Delphi的TBitmap封装了Windows的GDI位图,因此,TBitmap只支持bmp格式的图像,但是在Delphi应用程序中,常常会遇到图形格式的转换,如将Delphi位图TBitmap的图 ...

  6. NX二次开发-UFUN创建圆锥UF_MODL_create_cone1

    NX11+VS2013 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建圆锥 UF_FEATURE_SIGN ...

  7. C++ 字符串的分割函数split 及 用法【转载】

    文章出处https://blog.csdn.net/glmushroom/article/details/80690881 之前在C#中总用到字符串的分割,使用系统函数即可,比如: string a ...

  8. Go的异常处理 defer, panic, recover

    Go语言追求简洁优雅,所以,Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在一起会很容易使得代码变得混乱.因为开发者很容易滥用异常, ...

  9. 使用RAS+AES对接口数据加解密

    在实际开发中,会遇到两个系统之间传输数据,为了对传输的数据进行保护,需要发送方对接口数据进行加密,接收方对数据解密. 对数据加密,采用RSA+AES双重加密,是数据更加安全. 使用前提: 如果客户端本 ...

  10. IDEA web 开发环境搭建

    最近由eclipse 换 IDEA ,记录下开发环境的搭建过程. 1 配置idea vim 既可以使用IDEA方便的代码提示和调试功能,又可以方便使用vim编辑文件,安装完成后显示为vim Emula ...