C# HTTP请求 异步(async await)

本文介绍了一个使用C#进行异步HTTP POST请求的例子。通过Task和async/await关键字实现异步操作,避免阻塞主线程。展示了如何设置请求头、发送POST数据,并接收响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        static void Main(string[] args)
        {
            new Task(() =>
            {
                Invoke();
            }).Start();
            Console.WriteLine("我是主线程");
            Console.ReadKey();
        }

        public static async void Invoke()
        {
            var result = Keep();
            Console.WriteLine("执行其他的");
            string str = await result;  //等待返回
            Console.WriteLine(str);  //输出返回
        }

        public static async Task<string> Keep()
        {
            HttpWebRequest request = WebRequest.Create("http:/*****************") as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";     //这里一定不要忘记了 我排查了好久 发现这里没有写这一句  弄的怀疑人生了 后来通过抓包对比 才发现这个差距  粗心了
            string data = "userId=7c509e59-9179-4fc3-b00a-a33007b1068e&agentId=2acbf00f-aa58-44f6-88c8-6d7027b78a7f&companyId=64436ad0-8ef4-430a-b6a4-08cac3b19c0a&versionTime=1553654309167";
            byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
            request.ContentLength = buf.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(buf, 0, buf.Length);
            newStream.Close();
            HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
            string result = reader.ReadToEnd();
            return result;
        }

 如果你不写  request.ContentType   那么用下面的这种也可以   

        static void Main(string[] args)
        {
            new Task(() =>
            {
                Invoke();
            }).Start();
            Console.WriteLine("我是主线程");
            Console.ReadKey();
        }

        public static async void Invoke()
        {
            var result = Keep();
            Console.WriteLine("执行其他的");
            string str = await result;  //等待返回
            Console.WriteLine(str);  //输出返回
        }

        public static async Task<string> Keep()
        {
            string url = "http://message.sungoin.com/platform-message/getPlatformClientMsg";
            string data = "userId=7c509e59-9179-4fc3-b00a-a33007b1068e&agentId=2acbf00f-aa58-44f6-88c8-6d7027b78a7f&companyId=64436ad0-8ef4-430a-b6a4-08cac3b19c0a&versionTime=1553654309167";
            url = url + "?" + data;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
            string result = reader.ReadToEnd();
            return result.ToString();
        }

  

转载于:https://www.cnblogs.com/sajiao/p/10607783.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值