Client use XMLHTTP to call Web service via SOAP

http://www.codeproject.com/KB/webservices/aspwebsvr.aspx

 

Web Service

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.Diagnostics;
using System.Configuration;

 

namespace MyWebService
{
    /// <summary>
    /// Summary description for Search
    /// </summary>

    [WebService(Namespace = "http://tempuri.org/ ")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class Search : System.Web.Services.WebService
    {
   
        [WebMethod]
        public string Add (int a, int b)
        {

                ...

       }

 

 

     }

 

 

 

 

HTML + AJAX (IE)

 


<html>
<body>
<script language="javascript">
function btn_click ()
{
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//">";

note: from envykok

Add- web method name

http://tempuri.org - web method namesapce

 

sXml = sXml + "<a>"+ document.getElementById('a').value  + "</a>";
sXml = sXml + "<b>"+ document.getElementById('b').value  + "</b>";  
sXml += "</Add></soap:Body></soap:Envelope>"
// Try to parse the XML string into DOM object
xmlObj.loadXML(sXml) ;
//To see the validated XML string is well-formed
document.getElementById('XmlRequest').innerText =  xmlObj.xml  ;
var xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP") ;
xmlHTTP.Open ("Post", "http://localhost/ASP.NET/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 ;
}
</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" οnclick="jscript:btn_click()"></input>
</p>
<p>
Answer is <span id="answer"></span>
</p>
<hr/>
<p>
Request:</p>
<span id="XmlRequest"></span>
<p>Response:</p>
<span id="MyResult"></span>
</form>

</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值