Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


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
Posted
Updated 18-May-18 0:59am

1 solution

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
        });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900