通过soap发送XML文档

时间:2020-02-25 10:39:20

标签: c# xml

以下是我的代码: Method(SendXmlDocument)将XML文档,URL和SOAP操作作为参数,并向Web服务发出请求。我必须使用HttpClient类向Web服务发出请求。

using System;
using System.Net;
using System.Threading.Tasks;
using System.Xml;
using System.IO;

namespace HttpClientStatus
{


class WebServiceClient
{
    public void SendXmlDocument(XmlDocument fi , string URL , string action)
    {
        XmlDocument soapEnvelopeXml = fi;
        HttpWebRequest webRequest = CreateWebRequest(URL, action);
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

        IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

        asyncResult.AsyncWaitHandle.WaitOne();

        // get the response from the completed web request.
        string soapResult;
        using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
        {
            using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
            {
                soapResult = rd.ReadToEnd();
            }
            Console.Write(soapResult);
        }
    }

    private static HttpWebRequest CreateWebRequest(string url, string action)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }

    }


   class Program
{

    static void Main(string[] args)
    {
        var _u = "http://xxxxxxxxx/Service1.asmx";
        var _a = "http://xxxxxxxxx/Service1.asmx/action";
        XmlDocument d = new XmlDocument();
        d.Load("C://Users//gmzmdz//Desktop//course.xml");
        WebServiceClient ob = new WebServiceClient();
        ob.SendXmlDocument(d, _u, _a);


    }
}
}

我的问题是,以下纯XML文档是否会发送。该方法是否向Web服务发出请求。我将如何得知请求已成功完成?然后,我必须在从Web服务返回的控制台上打印响应。

0 个答案:

没有答案