Original article: http://www.cnblogs.com/netboy/archive/2006/02/18/333260.html
1.
Create a WebService. 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 calls the implementation part of 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 + '<safety helloto 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>
You can find the heap Dongdong that needs to be sent using the POST method on the WebService test page, and add the corresponding parameters together.
I found that the POST method has a slow response because of the large amount of data sent in the POST method?