Use Vbscript to call Web Services

Source: Internet
Author: User
Recently encountered a problem that requires you to call. Net WebService on ASP and the client. That is to say, you need to use VBscript or JavaScript to call WebService. I checked it online. Most of the solutions use soap toolkit. But this year, the soap toolkit will be suspended for later support. To use soapclient, I need to install the soap toolkit, this is not universal for the client, so I thought of using XMLHTTP to interact With WebService.

the client Code is as follows:

changed to the ASP code on the server:
<%
set objhttp = server. createobject ("msxml2.xmlhttp")
set xmldoc = server. createobject ("MSXML. domdocument ")
strwebserviceurl =" http: // localhost/possible/service1.asmx/Add "
'set parameters and values
strrequest =" x = 2 & Y = 3 "
objhttp. open "Post", strwebserviceurl, false
'setting this Content-Type is very important
objhttp. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded"
objhttp. send (strrequest)
Bok = xmldoc. load (objhttp. responsexml)
'Check the status value
If objhttp. status = 200 then
xmlstr = xmldoc. XML
xmlstr = Replace (xmlstr, "<", "<", 1,-1,1)
xmlstr = Replace (xmlstr, ">", "> ", 1,-1, 1)
response. write xmlstr
else
response. write objhttp. statu & "
"
response. write objhttp. statustext
end if
%>

The above code is normal for local testing (tested on the local machine where the WebService is deployed). However, the strwebserviceurl = "http: // when localhost/possible/service1.asmx/Add "is changed to WebService deployed on other machines, a problem occurs, and the result Always Returns Error 500, that is, objhttp. status is always 500.
The reason is that. Net framework1.1 does not support httpget and httppost by default. If you modify the Web. config in WebService to add
<WebServices>
<Protocols>
<Add name = "httppost"/>
<Add name = "httpget"/>
</Protocols>
</WebServices>
Then, the code can call the WebService on the remote machine.
By default, soap can be used to send messages. net framework1.1 support, so the XML string used to construct the SOAP request to send the XMLHTTP object to the remote server web. config does not have requirements. Therefore, a soaprequest string is constructed based on the Local display example and sent to the remote host to be deployed. The status code 200 is returned, responsexml can be obtained smoothly. the Code is as follows:

The client code is as follows:
<Script language = "VBScript">
Dim URL, XMLHTTP, Dom, node, xmldoc
'Construct different SOAP requests based on different methods on the WebService test page.
Soaprequest = "<? XML version = "& CHR (34) &" 1.0 "& CHR (34) &" encoding = "& CHR (34) &" UTF-8 "& CHR (34) & "?> "&_
"<Soap: envelope xmlns: xsi =" & CHR (34) & "http://www.w3.org/2001/XMLSchema-instance" & CHR (34 )&""&_
"Xmlns: XSD =" & CHR (34) & "http://www.w3.org/2001/XMLSchema" & CHR (34 )&""&_
"Xmlns: Soap =" & CHR (34) & "http://schemas.xmlsoap.org/soap/envelope/" & CHR (34) & "> "&_
"<Soap: Body> "&_
"<Add xmlns =" & CHR (34) & "http: // localhost" & CHR (34) & "> "&_
"<X> 3 </x> "&_
"<Y> 4 </Y> "&_
"</Add> "&_
"</Soap: Body> "&_
"</Soap: envelope>"
Url = "http://www.xxxx.com/Service1.asmx? Methodname = Add"
Set xmldoc = Createobject ("MSXML. domdocument ")
Xmldoc. loadxml (soaprequest)
Set XMLHTTP = Createobject ("msxml2.xmlhttp ")
XMLHTTP. Open "Post", URL, false
XMLHTTP. setRequestHeader "Content-Type", "text/XML; charset = UTF-8"
The 'soapaction' header can also be found in the sample.
XMLHTTP. setRequestHeader "soapaction", "http: // localhost/Add"
XMLHTTP. setRequestHeader "Content-Length", Len (soaprequest)
XMLHTTP. Send (xmldoc)
Msgbox XMLHTTP. Status
Msgbox XMLHTTP. statustext
Msgbox XMLHTTP. responsetext
If xml http. Status = 200 then
Xmldoc. Load (XMLHTTP. responsexml)
Msgbox "execution result:" & xmldoc. getelementsbytagname ("addresult") (0). Text
Else
Msgbox "failed"
End if
</SCRIPT>

The ASP code on the server is changed:
<%
Dim URL, XMLHTTP, Dom, node, xmldoc
'Construct different SOAP requests based on different methods on the WebService test page.
Soaprequest = "<? XML version = "& CHR (34) &" 1.0 "& CHR (34) &" encoding = "& CHR (34) &" UTF-8 "& CHR (34) & "?> "&_
"<Soap: envelope xmlns: xsi =" & CHR (34) & "http://www.w3.org/2001/XMLSchema-instance" & CHR (34 )&""&_
"Xmlns: XSD =" & CHR (34) & "http://www.w3.org/2001/XMLSchema" & CHR (34 )&""&_
"Xmlns: Soap =" & CHR (34) & "http://schemas.xmlsoap.org/soap/envelope/" & CHR (34) & "> "&_
"<Soap: Body> "&_
"<Add xmlns =" & CHR (34) & "http: // localhost" & CHR (34) & "> "&_
"<X> 3 </x> "&_
"<Y> 4 </Y> "&_
"</Add> "&_
"</Soap: Body> "&_
"</Soap: envelope>"
Url = "http://www.xxxx.com/Service1.asmx? Methodname = Add"
Set xmldoc = server. Createobject ("MSXML. domdocument ")
Xmldoc. loadxml (soaprequest)
Set XMLHTTP = server. Createobject ("msxml2.xmlhttp ")
XMLHTTP. Open "Post", URL, false
XMLHTTP. setRequestHeader "Content-Type", "text/XML; charset = UTF-8"
XMLHTTP. setRequestHeader "soapaction", "http: // localhost/Add"
XMLHTTP. setRequestHeader "Content-Length", Len (soaprequest)
XMLHTTP. Send (xmldoc)
If xml http. Status = 200 then
Xmldoc. Load (XMLHTTP. responsexml)
Response. Write XMLHTTP. Status & "<br>"
Response. Write XMLHTTP. statustext & "<br> the execution result is :"
Response. Write xmldoc. getelementsbytagname ("addresult") (0). Text
Else
Response. Write XMLHTTP. Status & "<br>"
Response. Write XMLHTTP. statustext
End if
%>

The above uses VBScript, which is basically the same for Javascript. You only need to make some minor changes. The specific code is omitted here.

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////

Appendix:
Code of the WebService file service1.asmx used for testing:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. Web;
Using system. Web. Services;

namespace possible
{< br> ///


/// Summary of service1.
//
[WebService (description = "my web service", name = "myservice", namespace = "http: // localhost ")]
public class myservice: system. web. services. webService
{< br> Public myservice ()
{< br> // codegen: The call is ASP.
initializecomponent ();
}

# code generated by the region component designer
// required by the Web Service designer
private icontainer components = NULL;
//


// The designer supports the required methods. Do not use the code editor to modify the content of the method.
//.
//
private void initializecomponent ()
{< BR >}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing & components! = NULL)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Endregion

[Webmethod (description = "returns the sum of two integers")]
Public int add (int x, int y)
{
Return X + Y;
}
}
}

Article Source: csdn
author: possible_y (original)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.