Click here to Skip to main content
13,558,199 members
Rate this:
 
Please Sign up or sign in to vote.
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 17-May-18 23:42pm
Updated 18-May-18 0:59am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02-2016 | 2.8.180515.1 | Last Updated 18 May 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100