<SCRIPT language="JavaScript">
// Client invoke WebService use SOAP request and response
function GetHelloWorld_SOAP(i)
{
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var soapMessage, soapData, URL;
// Set the soap message
soapMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
soapMessage += "<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/\">";
soapMessage += "<soap:Body>";
// Set the data for soap body ---- begin ------
soapData = "<HelloWorld xmlns=\"http://tempuri.org/\">";
soapData += " <i>" + i + "</i>";
soapData += "</HelloWorld>";
// Set the data for soap body ---- end ------
soapMessage = soapMessage + soapData + "</soap:Body>";
soapMessage = soapMessage + "</soap:Envelope>";
URL = "Service1.asmx"; //可以使用相对地址或完整URL
xmlhttp.Open("POST",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/HelloWorld");
xmlhttp.send(soapMessage);
alert(soapMessage)
var x = xmlhttp.responseXML;
alert(x.childNodes[1].text);
//那么如何知道是否调用成功呢,状态为200说明调用成功,500则说明出错
alert(xmlhttp.Status);
alert(xmlhttp.StatusText);
}
</SCRIPT>
<SCRIPT language="vbscript">
' Client invoke WebService use SOAP request and response
Function vbGetHelloWorld_SOAP(i)
dim soapMessage, soapData, URL
' Set the soap message
soapMessage = "<?XML version=""1.0"" encoding=""utf-8""?>"
soapMessage = soapMessage & "<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/"">"
soapMessage = soapMessage & "<SOAP:BODY>"
' Set the data for soap body ---- begin ------
soapData = "<HELLOWORLD xmlns=""http://tempuri.org/"">"
soapData = soapData & " <I>" & i & "</I>"
soapData = soapData & "</HELLOWORLD>"
' Set the data for soap body ---- end ------
soapMessage = soapMessage & soapData & "</SOAP:BODY>"
soapMessage = soapMessage & "</SOAP:ENVELOPE>"
URL = "Service1.asmx" '可以使用相对地址或完整URL
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "POST",URL, False
xmlhttp.SetRequestHeader "Content-Type","text/xml; charset=utf-8"
xmlhttp.SetRequestHeader "SOAPAction","http://tempuri.org/HelloWorld"
xmlhttp.send(soapMessage)
Set x2 = xmlhttp.responseXML
alert(x2.childNodes(1).text)
'那么如何知道是否调用成功呢,状态为200说明调用成功,500则说明出错
alert(xmlhttp.Status)
alert(xmlhttp.StatusText)
End Function
</SCRIPT>
JavaScript
vbscript
通过HTTP POST对WebService进行调用
<SCRIPT language="JavaScript">
// Client invoke WebService use HTTP POST request and response
function GetHelloWorld_HTTPPOST(i)
{
var URL = "http://localhost/WSInvoke/Service1.asmx/HelloWorld";
var Params = "i=" + i;// Set postback parameters
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.Open("POST",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","application/x-www-form-urlencoded");
xmlhttp.SetRequestHeader ("Content-Length",Params.length);
xmlhttp.send(Params);
var x = xmlhttp.responseXML;
alert(x.childNodes[1].text);
//那么如何知道是否调用成功呢,状态为200说明调用成功,500则说明出错
alert(xmlhttp.Status);
alert(xmlhttp.StatusText);
}
</SCRIPT>
<SCRIPT language="vbscript">
' Client invoke WebService use HTTP POST request and response
Function vbGetHelloWorld_HTTPPOST(i)
URL = "http://localhost/WSInvoke/Service1.asmx/HelloWorld"
Params = "i=" & i ' Set postback parameters
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "POST",URL,False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "Content-Length",LEN(Params)
xmlhttp.Send(Params)
Set x = xmlhttp.responseXML
alert(x.childNodes(1).text)
'那么如何知道是否调用成功呢,状态为200说明调用成功,500则说明出错
alert(xmlhttp.Status)
alert(xmlhttp.StatusText)
Set xmlhttp = Nothing
End Function
</SCRIPT>
<SCRIPT language="JavaScript">
function Init()
{
// establishes a friendly name for a Web service URL
wsCaller.useService("http://localhost/WSInvoke/Service1.asmx?WSDL","HW");
}
function callWS(i)
{
wsCaller.HW.callService(Handler_HW, "HelloWorld", i);
}
function Handler_HW(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
{
document.all.Text1.value = result.value;
}
}
</SCRIPT>
<BODY id="wsCaller" οnlοad="Init()" style="BEHAVIOR:url(webservice.htc)">
<INPUT type="text" id="Text1" name="Text1">
<INPUT οnclick="callWS(1)" type="button" value="Invoke" id="Button1" name="Button1">
Web Service 的完整代码(C#, Service1.asmx.cs)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace WSInvoke
{
/// <SUMMARY>
/// Summary description for Service1.
/// </SUMMARY>
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
/// <SUMMARY>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </SUMMARY>
private void InitializeComponent()
{
}
/// <SUMMARY>
/// Clean up any resources being used.
/// </SUMMARY>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5
[WebMethod]
public string HelloWorld(int i)
{
return "Hello World" + i;
}
}
}