• 客户端调用Web服务


    用JS,代码如下:
     function btn_click (a, b)
            {
                    var xmlObj = new ActiveXObject("Msxml2.DOMDocument") ;
                    var sXml  = "<?xml version=\"1.0\" ?>" ;
                          sXml += "<soap:Envelope "
                          sXml += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " ;
                          sXml += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " ;
                          sXml += "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" ;
                          sXml += "<soap:Body>" ;
                          sXml += "<Add xmlns=\"http://tempuri.org/\">" ;
                          sXml = sXml + "<a>" + a.value  + "</a>" ;
                          sXml = sXml + "<b>" + b.value  + "</b>" ;                     
                          sXml += "</Add></soap:Body></soap:Envelope>"
                   
                    xmlObj.loadXML(sXml) ;
                   
                    XmlRequest.innerText =  xmlObj.xml  ;
                   
                    var xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP") ;
                    xmlHTTP.Open ( "Post", "http://localhost/WebService/MyWebService.asmx", false) ;
                    xmlHTTP.setRequestHeader("SOAPAction", "http://tempuri.org/Add") ;
                    xmlHTTP.setRequestHeader("Content-Type", "text/xml; charset=utf-8" ) ;
                    xmlHTTP.Send(xmlObj.xml) ;
                    MyResult.innerText =  xmlHTTP.responseText ;
                   
                    var xmlResponse = xmlHTTP.responseXML ;
                    answer.innerText = xmlResponse.selectSingleNode("soap:Envelope/soap:Body/AddResponse/AddResult").text ;
            }

    </script>


    <form>
            <p>Please input a:<input id="a" name="a"></input></p>
            <p>Please input b:<input id="b" name="b"></input></p>
            <p>
            <input type="button" id="btn"  value="Enter"
                    onclick="jscript:btn_click(a, b)"></input>
            </p>
            <p>Answer is <span id="answer"></span></p>
            <hr></hr>
            <p>Request:</p>
            <span id="XmlRequest"></span>
            <p>Response:</p>       
            <span id="MyResult"></span>
           
    </form>

    </body>
    </html>

    用c#程序调用:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Xml;
    using Microsoft.Web.Services3.Messaging;
    using Microsoft.Web.Services3;
    namespace SOAP
    {
        class MathClient : SoapClient
        {
            public MathClient(Uri destination) : base(destination)
                //: base(destination)
            {
            }

            private SoapEnvelope CreateMessage(string op, int a, int b)
            {
                SoapEnvelope envelope = new SoapEnvelope();
                envelope.CreateBody();
                envelope.Body.InnerXml = string.Format(
                    @"<{0} xmlns = 'http://tempuri.org/'>
                        <a>{1}</a>
                        <b>{2}</b>
                    </{0}>", op, a, b);
                return envelope;
            }

            [SoapMethod("http://tempuri.org/Add")]
            public int Add(int a, int b)
            {
                try
                {
                    SoapEnvelope sendEnvelope = CreateMessage("Add", a, b);

                    SoapEnvelope receiveEnvelope = base.SendRequestResponse("Add", sendEnvelope);
                    int x = XmlConvert.ToInt32(receiveEnvelope.InnerText);
                    return x;
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return -1;
                }
            }
        }
    }
     class Program
        {
            static void Main(string[] args)
            {
                MathClient mathiClient = new MathClient(new Uri("http://localhost/WebService/MyWebService.asmx"));
                int a = mathiClient.Add(2, 10);
                Console.WriteLine(a);
            }
        }

  • 相关阅读:
    如何用UE(UltraEdit)删除重复行?--转
    spring源码分析之<context:component-scan/>vs<annotation-config/>
    annotation-config vs component-scan – Spring Core--转
    spring源码分析之spring注解@Aspect是如何工作的?
    Spring之LoadTimeWeaver——一个需求引发的思考---转
    Linux上的free命令详解
    MVC/MVP/MVVM区别——MVVM就是angular,视图和数据双向绑定
    elasticsearch如何安全重启节点
    ES等待任务——是master节点上的task任务
    1. 批量梯度下降法BGD 2. 随机梯度下降法SGD 3. 小批量梯度下降法MBGD
  • 原文地址:https://www.cnblogs.com/leon916/p/1037298.html
Copyright © 2020-2023  润新知