带有自定义标头的SOAP webservice调用 [英] SOAP webservice call with custom header

查看:114
本文介绍了带有自定义标头的SOAP webservice调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要将BeaconLive API集成到我的应用程序中。在此SOAP API中,soap标头不是SOAP Web服务的一部分。我们需要根据请求传递SOAP标头。



下面是示例XML,它是reqeust XML:



Hi,

I need to integrate BeaconLive API to my application. In this SOAP API the soap headers are not part of SOAP webservice. We need to pass SOAP header on request.

Below is the sample XML which is the reqeust XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.ecprovws.bcs.com/">
   <soapenv:Header>
   <ecProvAuthHeader0><clientId>SomeClientId</clientId><clientKey>SomeClientKey</clientKey></ecProvAuthHeader0></soapenv:Header>
   <soapenv:Body>
      <ws:getConferences>
         <!--Optional:-->
         <!--type: dateTime-->
         <startDate>2014-01-01</startDate>
         <!--Optional:-->
         <!--type: dateTime-->
         <endDate>2016-02-28</endDate>
         <!--Optional:-->
         <!--type: string-->
         <organizationAlias>SomeCompanyAlias</organizationAlias>
      </ws:getConferences>
   </soapenv:Body>
</soapenv:Envelope>





我在 SOAPUI 软件中使用实时凭据在XML上方进行了测试,我得到了相同的结果。 />


当我尝试将此XML发送到端点URL时,我没有得到任何结果。

下面是我的C#代码:



I tested above XML with live credentials in SOAPUI software and I am getting the result for the same.

When I am trying to send this XML to endpoint URL I am not getting any result.
Below is my C# code:

protected void Page_Load(object sender, EventArgs e)
    {
        CallWebService();
    }

    public static void CallWebService()
    {
        var _url = "someEndPointURL";
        var _action = "";

        XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
        HttpWebRequest webRequest = CreateWebRequest(_url, _action);
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

        // begin async call to web request.
        IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

        // suspend this thread until call is complete. You might want to
        // do something usefull here like update your UI.
        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();
            }
            HttpContext.Current.Response.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 XmlDocument CreateSoapEnvelope()
    {
        XmlDocument soapEnvelop = new XmlDocument();
        //soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><HelloWorld xmlns=""http://tempuri.org/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><int1 xsi:type=""xsd:integer"">12</int1><int2 xsi:type=""xsd:integer"">32</int2></HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>");
        soapEnvelop.LoadXml(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ws=""http://ws.ecprovws.bcs.com/""><soapenv:Header><ecProvAuthHeader0><clientId>SomeClientId</clientId><clientKey>SomeClientKey</clientKey></ecProvAuthHeader0></soapenv:Header><soapenv:Body><ws:getConferences><startDate>2014-01-01</startDate><endDate>2016-02-28</endDate><organizationAlias>SomeCompanyAlial</organizationAlias></ws:getConferences></soapenv:Body></soapenv:Envelope>");
        
        return soapEnvelop;
    }

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





请帮助我。



Please help me.

推荐答案

我得到了解决方案



Hi, I got the solution

string xml = @"xml string";
        string url = "http://68.64.91.15:8080/EcProvWs/EcProvWs?wsdl";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xml);
        req.Method = "POST";
        req.ContentType = "text/xml;charset=utf-8";
        req.ContentLength = requestBytes.Length;
        Stream requestStream = req.GetRequestStream();
        requestStream.Write(requestBytes, 0, requestBytes.Length);
        requestStream.Close();

        HttpWebResponse res = (HttpWebResponse)req.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
        string backstr = sr.ReadToEnd();

        HttpContext.Current.Response.Write(backstr);
        sr.Close();
        res.Close();


这篇关于带有自定义标头的SOAP webservice调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆