Multiple HTTP post requests to a web service from XML files

1.5k views Asked by At

I want to send multiple HTTP post requests to a Web Service in C# .For example , if n=3 then http post requests from 3 xml files should be made. So how can i implement this ? I just need ideas. I think n threads can be created and each thread will execute one http post request. If possible a bit assisstance in code as well. Thank you.

2

There are 2 answers

2
hagrawal7777 On

Use java.util.concurrent.ExecutorService As Java specs says:

An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.

So, using an implementation of ExecutorService you can run all your tasks asynchronously or synchronously in given number of threads. For that you need to create a list of Callable objects and pass it to invokeAll method of object of ExecutorService. invokeAll method will return list of list of Future objects (Each Future object will represent each task, and order is same as you put in Callable list passed to invokeAll method) which you can loop total all the result of the task and print it.

You should read all available methods of Executors class which return different instances of ExecutorService, so choose the one which suits you.

In this way, you will be able to run your N tasks (which are your HTTP requests) in M given threads, and once all the threads are finished you will get list of Future objects which will give you completion information/status of each task.

Please be sure about exception handling in threads, because they don't get propagated, you need to explicitly print the stacktrace.

try {
    List<Callable<Object>> callableList = new ArrayList<Callable<Object>>();
    callableList.add(null); /*Add instance of Callable, which would have your HTTP request code in its overridden call() method*/
    callableList.add(null); /*Add instance of Callable*/
    callableList.add(null); /*Add instance of Callable*/

    //Specify how many threads you want or need to operate. Read other methods of Executors which return different instances of ExecutorService
    final ExecutorService service = Executors.newFixedThreadPool(3);

    //This will invoke all your N tasks in specified M threads ...
    List<Future<String[]>> futureObjects = service.invokeAll(callableList);  //futureObjects will contain result of each thread execution
} catch (InterruptedException e) {
    e.printStackTrace();
}

Check below psuedo example:

1
Tanay Narkhede On

This code works . Explaination :

  • Firstly the user gives the source and destination paths for the .xml files.
  • Directory.getFiles() helps us to get the .xml files in the string array . (we have to pass .xml as a parameter) .

  • SO now what basically happens is for each file we get at the source pat , a thread is created .

  • But say if the user wants to send "n" requests at a time , then n threads are created at a time.
  • And the next set of threads are not created unless the previous threads are finished executing.
  • This is ensured by thread.Join().
  • And after a request is made to the web service , we get the response by getResponse() and the response is written in .xml files which are stored at the destination paths.

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.IO;
     using System.Threading;
     using System.Xml;
     using System.Net;
     namespace ConsoleApplication4
     {
         class Program
         {
          int flag = 1;
          string destination;
          string source;
          static void Main(string[] args)
        {
        Console.ForegroundColor = ConsoleColor.Red;
    
        Console.WriteLine("**************************** Send HTTP Post Requests **************************");
        int n = 0;
        Program p = new Program();
        Console.WriteLine("Enter the number of requests you want to send at a time");
        string s = Console.ReadLine();
        int.TryParse(s, out n);
        Console.WriteLine("Enter Source");
        p.source = Console.ReadLine();
        Console.WriteLine("Enter Destination");
        p.destination = Console.ReadLine();
    
        string[] files = null;
        files = Directory.GetFiles(p.source, "*.xml", SearchOption.TopDirectoryOnly);
    
        Thread[] thread = new Thread[files.Length];
    
        int len = files.Length;
        for (int i = 0; i<len; i+=n)
        {
            int x = i;
            //Thread.Sleep(5000);
            for (int j = 0; j < n && x < len; j++)
            {
    
                var localx = x;
                thread[x] = new Thread(() => function(files[localx], p));
                thread[x].Start();
                Thread.Sleep(50);
                //thread[x].Join();
                x++;
            }
            int y = x - n;
            for (; y < x; y++)
            {
                int t = y;
                thread[t].Join();
    
            }
    
        }
    
        // thread[0] = new Thread(() => function(files[0]));
        //thread[0].Start();
        Console.ReadKey();
    
    }
    public static void function(string temp,Program p)
    {
    
        XmlDocument doc = new XmlDocument();
        doc.Load(temp);
    
        string final_d=p.destination + "response " + p.flag + ".xml";
        p.flag++;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.76.22.135/wpaADws/ADService.asmx");
        request.ContentType = "text/xml;charset=\"utf-8\"";
        request.Accept = "text/xml";
        request.Method = "POST";
        Stream stream = request.GetRequestStream();
        doc.Save(stream);
        stream.Close();
    
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader rd = new StreamReader(response.GetResponseStream()))
        {
            string soapResult = rd.ReadToEnd();
            doc.LoadXml(soapResult);
            File.WriteAllText(final_d, doc.DocumentElement.InnerText);
    
            //XmlTextWriter xml=new XmlTextWriter(
            Console.WriteLine(soapResult);
            //Console.ReadKey();
        }
    }
    

    } }