如何在C#windows应用程序中发送队列中的Web请求 [英] How to send web request in queue in C# windows application

查看:71
本文介绍了如何在C#windows应用程序中发送队列中的Web请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个应用程序来触发来自窗口应用程序的Web请求而忘记了,意味着没有等待响应。



I made an application to trigger web request from window application and forget,means without waiting response.

string url1 = APIUrl + "/SendNotification";
                string url2 = Url + "/SendSOA";

                ASCIIEncoding encoding = new ASCIIEncoding();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
                request.Method = "Post";
                request.ContentLength = 0;
                request.ContentType = "application/json";
                request.GetResponseAsync(); //Mohan: Method will not wait for response just trigger and forget

                ASCIIEncoding encoding1 = new ASCIIEncoding();
                HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url2);
                request1.Method = "Post";
                request1.ContentLength = 0;
                request1.ContentType = "application/json";
                request1.GetResponseAsync(); //Mohan: Method will not wait for response just trigger and forget





但当我按本地主机URL尝试时那个时候我看到这两个方法正在运行所以我想在队列中发送请求然后它将完成第一个请求然后将完成第二个请求。



表示直到一个请求不是完成的第二个应该在队列中。

请帮我做。



在此先感谢。



我尝试过的事情:



我试过这个但它不起作用





but when I am trying it by local host URL that time I see that these two methods are running so I want to send request in Queue then it will complete first Request and then will complete second request.

means till one request is not completed second should be in queue.
please help me to do it.

Thanks in Advance.

What I have tried:

I Just tried this but its not working

string APIUrl = ConfigurationManager.AppSettings["APIUrl"].ToString();
                string Url = ConfigurationManager.AppSettings["Url"].ToString();

                Queue httprequest = new Queue();
                string url1 = APIUrl + "/SendNotification";
                string url2 = Url + "/SendSOA";

                ASCIIEncoding encoding = new ASCIIEncoding();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
                request.Method = "Post";
                request.ContentLength = 0;
                request.ContentType = "application/json";
                httprequest.Enqueue(request);
                request.GetResponseAsync(); //Mohan: Method will not wait for response just trigger and forget

                ASCIIEncoding encoding1 = new ASCIIEncoding();
                HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url2);
                request1.Method = "Post";
                request1.ContentLength = 0;
                request1.ContentType = "application/json";
                httprequest.Enqueue(request1);
                request1.GetResponseAsync(); //Mohan: Method will not wait for response just trigger and forget

推荐答案

此代码对我来说很好。



This Code works fine for me.

string url1 = APIUrl + "/SendNotification";
        string url2 = Url + "/SendSOA";

        ASCIIEncoding encoding = new ASCIIEncoding();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
        request.Method = "Post";
        request.ContentLength = 0;
        request.ContentType = "application/json";
        var firstTask = request.GetResponseAsync();

        firstTask.ContinueWith(_ => {
            ASCIIEncoding encoding1 = new ASCIIEncoding();
            HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url2);
            request1.Method = "Post";
            request1.ContentLength = 0;
            request1.ContentType = "application/json";
            request1.GetResponseAsync(); //Mohan: Method will not wait for response just trigger and forget
        });


这篇关于如何在C#windows应用程序中发送队列中的Web请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆