1. 创建webservice,为了免于落俗我稍稍修改了创建webserice的默认webmethod。^_^
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string SayHelloTo(string Name) {
return "Hello "+Name;
}
}
还是俗了点。:)
2. js调用webservice+xmlhttp的实现部分。
<html>
<title>
Call webservice with javascript and xmlhttp.
</title>
<body>
<script language="javascript">
//Test function with get method.
function RequestByGet(data){
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";
xmlhttp.Open("GET",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
var result = xmlhttp.status;
//OK
if(result==200) {
document.write(xmlhttp.responseText);
}
xmlhttp = null;
}
//Test function with post method
function RequestByPost(value)
{
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
data = data + '<soap:Body>';
data = data + '<SayHelloTo xmlns="http://tempuri.org/">';
data = data + '<Name>'+value+'</Name>';
data = data + '</SayHelloTo>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL="http://localhost:1323/WebSite6/Service.asmx";
xmlhttp.Open("POST",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
document.write( xmlhttp.responseText);
}
</Script>
<input type="button" value="CallWebserviceByGet" onClick="RequestByGet(null)">
<input type="button" value="CallWebserviceByPost" onClick="RequestByPost('Zach')">
</body>
</html>
对于使用post方法需要发送的那堆东东可以在webservice的测试页面中找到,自己拼凑加上对应的参数就可以。
我发现用post方法的时候响应很慢,是因为用Post方法时发送的数据多的原因吗?
<%@ WebService Language="C#" class=MyMath %> using System; using System.Web.Services; public class MyMath { [WebMethod] public int add(int a, int b) { return a + b; } [WebMethod] public int subtract(int a, int b) { return a - b; } }
然后发布,先得到其wsdl。
2、首先,我们要下载webbehavior.htc这个文件(可以到http://msdn.microsoft.com/downloads/samples/internet/behaviors/library/webservice/default.asp.)
去下载,然后放到你的web当前目录下
然后在要调用webserice的页面中,修改如下
<body> <div id="addservice" style="behavior:url(webservice.htc)"></div> </body>
这里我们将div id命名为有意义的名称,并且指定style为 webservice行为。接着,我们要书写javascript来调用 webserice了:
首先,我们在javascript中,调用其wsdl addservice.useService("http://localhost/services/math.asmx?WSDL","MyMath");
使用id.useService(WSDLL路径,简单的命名方式);
我们之前设定的id是addservice,而为了给客户端调用方便,我们这里起了名称,叫MyMath。 而为了保证能正确调用webserice,必须在body里的onload事件里,马上加载处理webservice调用的javascript, 如下<script language="JavaScript"> function init() { addservice.useService("http://localhost/services/math.asmx?WSDL","MyMath"); } </script> <body onload="init()"> <div id="service" style="behavior:url(webservice.htc)"> </div> </body>在上面,我们通过webservice行为,首先得到了返回webservice的wsdl,接下来我们要进行调用了, 调用的格式如下: iCallID = id.FriendlyName.callService([CallbackHandler,] "MethodName", Param1, Param2, ...);这里id是我们在div里设置的id,而FridndbyName是我们刚才为方面而起的命,这里就是MyMath了,而CallbackHandler 是使用回调函数的过程名,如果无设置的话,则默认是使用onresult所调用的方法来进行处理,下文会讲到,而param1, ,param2等则是说传入的参数了,如:<SCRIPT language="JavaScript"> // All these variables must be global, // because they are used in both init() and onresult(). var iCallID = 0; var intA = 5; var intB = 6; function init() { // Establish the friendly name "MyMath" for the WebServiceURL service.useService("/services/math.asmx?WSDL","MyMath"); // The following method doesn't specify a callback handler, so onWSresult() is used iCallID = service.MyMath.callService("add", intA, intB); } function onWSresult() { // if there is an error, and the call came from the call() in init() if((event.result.error)&&(iCallID==event.result.id)) { // Pull the error information from the event.result.errorDetail properties var xfaultcode = event.result.errorDetail.code; var xfaultstring = event.result.errorDetail.string; var xfaultsoap = event.result.errorDetail.raw; // Add code to handle specific error codes here } // if there was no error, and the call came from the call() in init() else if((!event.result.error) && (iCallID == event.result.id)) { // Show the arithmetic! alert(intA + ' + ' + intB + ' = ' + event.result.value); } else { alert("Something else fired the event!"); } } </SCRIPT> <body onload="init()"> <div id="service" style="behavior:url(webservice.htc)" onresult="onWSresult()"> </div> </body>注意,用onresult方式返回的话,要在div部分的onresult中指定处理的方法,这里是用onWsresult()方法, 其中根据返回的信息来判断是否出错,出错的话则显示。如果用回调的话,则如下处理<SCRIPT language="JavaScript"> // All these variables must be global, // because they are used in both init() and onResult(). var iCallID = 0; var intA = 5; var intB = 6; function init() { // Establish the friendly name "MyMath" for the WebServiceURL service.useService("/services/math.asmx?WSDL","MyMath"); // The following uses a callback handler named "mathResults" iCallID = service.MyMath.callService(mathResults, "add", intA, intB); } function mathResults(result) { // if there is an error, and the call came from the call() in init() if(result.error) { // Pull the error information from the event.result.errorDetail properties var xfaultcode = result.errorDetail.code; var xfaultstring = result.errorDetail.string; var xfaultsoap = result.errorDetail.raw; // Add code to handle specific error codes here } // if there was no error else { // Show the arithmetic alert(intA + ' + ' + intB + " = " + result.value); } } </SCRIPT> <body onload="init()"> <div id="service" style="behavior:url(webservice.htc)"> </div> </body>