gpt4 book ai didi

c# - 从 XML 文件到 Web 服务的多个 HTTP post 请求

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:29 26 4
gpt4 key购买 nike

我想向 C# 中的 Web 服务发送多个 HTTP post 请求。例如,如果 n=3,则应从 3 个 xml 文件发出 HTTP post 请求。那么我该如何实现呢?我只需要想法。我认为可以创建 n 个线程,每个线程将执行一个 http post 请求。如果可能的话,也可以在代码中提供一些帮助。谢谢。

最佳答案

此代码有效。解释:

  • 首先,用户提供 .xml 文件的源路径和目标路径。
  • Directory.getFiles() 帮助我们获取字符串数组中的.xml 文件。(我们必须将 .xml 作为参数传递)。

  • 现在基本上发生的事情是,对于我们从源 pat 获取的每个文件,都会创建一个线程。

  • 但是如果用户想一次发送“n”个请求,则一次创建 n 个线程。
  • 除非前一个线程完成执行,否则不会创建下一组线程。
  • 这是由 thread.Join() 确保的。
  • 在向 Web 服务发出请求后,我们通过 getResponse() 获得响应,并将响应写入存储在目标路径中的 .xml 文件中。

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

    }

关于c# - 从 XML 文件到 Web 服务的多个 HTTP post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30730009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com