To invoke a Web service using VBScript scripts

Source: Internet
Author: User
Tags chr config soap xmlns
Vbscript|web|web Service | The script recently encountered a problem that needs to be invoked at the ASP and client. NET, which means that you need to call WebService with VBScript or JavaScript. \ WebService On the Internet, most scenarios use soap Toolkit, but because the soap Toolkit is stopped for subsequent support this year, and to use soapclient requires a dedicated soap Toolkit, which is not universal for the client, So thought of using XMLHTTP, using XMLHTTP to interact with WebService.

The client code is as follows:
<script language= "VBScript" >
Set objhttp = CreateObject ("MSXML2. XMLHTTP ")
Set xmldoc =createobject ("MSXML"). DOMDocument ")
Strwebserviceurl = "Http://localhost/possible/Service1.asmx/add"
' Set parameters and their values
Strrequest = "X=2&y=3"
Objhttp.open "POST", Strwebserviceurl, False
' It's important to set up this Content-type
Objhttp.setrequestheader "Content-type", "application/x-www-form-urlencoded"
Objhttp.send (Strrequest)
BOK = Xmldoc.load (objhttp.responsexml)
' Look at the state value
MsgBox Objhttp.status
MsgBox Objhttp.statustext
' objhttp.status=200, we can handle the returned XML fragment here.
' If necessary, you can replace the < and > in the returned XML string
Xmlstr = Xmldoc.xml
Xmlstr = Replace (Xmlstr, "<", "<", 1,-1,1)
Xmlstr = Replace (Xmlstr, ">", ">", 1,-1,1)
MsgBox Xmlstr
</script>

The server-side ASP code is:
<%
Set objhttp = Server.CreateObject ("MSXML2. XMLHTTP ")
Set xmldoc =server.createobject ("MSXML"). DOMDocument ")
Strwebserviceurl = "Http://localhost/possible/Service1.asmx/add"
' Set parameters and their values
Strrequest = "X=2&y=3"
Objhttp.open "POST", Strwebserviceurl, False
' It's important to set up this Content-type
Objhttp.setrequestheader "Content-type", "application/x-www-form-urlencoded"
Objhttp.send (Strrequest)
BOK = Xmldoc.load (objhttp.responsexml)
' Look at the state 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& "<br>"
Response.Write Objhttp.statustext
End If
%>

The above code does not have a problem with the local test (tested on the local machine where the webservice is deployed), however the Strwebserviceurl = "Http://localhost/possible/Service1.asmx/add" Instead of webservice when deployed on other machines, there was a problem, and the result was a 500 error, the Objhttp.status 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 increase
<webServices>
<protocols>
<add name= "HttpPost"/>
<add name= "HttpGet"/>
</protocols>
</webServices>
, the code on the remote machine can be invoked on the webservice.
The use of SOAP to send by default can get. Net Framework1.1 support, the method of using the XML string to construct the SOAP request to send the XMLHTTP object is not required for the web.config of the remote server, and a soaprequest string is constructed based on the local display example , sent to a remote host that is about to be deployed, returning 200 status code and getting responsexml. Similar code is as follows:

The client code is as follows:
<script language= "VBScript" >
Dim Url,xmlhttp,dom,node,xmldoc
' Construct a different SOAP request based on the different methods of the WebService test page
Soaprequest = "<?xml version=" &AMP;CHR (a) & "1.0" &AMP;CHR (a) & "encoding=" &AMP;CHR (+) & "Utf-8" & CHR & "?>" & _
"<soap:envelope xmlns:xsi=" &AMP;CHR & "Http://www.w3.org/2001/XMLSchema-instance" &AMP;CHR (a) & "" & _
"Xmlns:xsd=" &AMP;CHR & "Http://www.w3.org/2001/XMLSchema" &AMP;CHR (+) & "" & _
"Xmlns:soap=" &AMP;CHR & "http://schemas.xmlsoap.org/soap/envelope/" &AMP;CHR (+) & ">" & _
"<soap:Body>" & _
"<add xmlns=" &AMP;CHR & "http://localhost" &AMP;CHR (+) & ">" & _
"<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"
' SOAPAction This header can also be found in 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 XMLHTTP. Status = Then
Xmldoc.load (Xmlhttp.responsexml)
MsgBox "Execution result is:" &xmldoc.getelementsbytagname ("Addresult") (0). Text
Else
MsgBox "Failed"
End If
</script>

The server-side ASP code is:
<%
Dim Url,xmlhttp,dom,node,xmldoc
' Construct a different SOAP request based on the different methods of the WebService test page
Soaprequest = "<?xml version=" &AMP;CHR (a) & "1.0" &AMP;CHR (a) & "encoding=" &AMP;CHR (+) & "Utf-8" & CHR & "?>" & _
"<soap:envelope xmlns:xsi=" &AMP;CHR & "Http://www.w3.org/2001/XMLSchema-instance" &AMP;CHR (a) & "" & _
"Xmlns:xsd=" &AMP;CHR & "Http://www.w3.org/2001/XMLSchema" &AMP;CHR (+) & "" & _
"Xmlns:soap=" &AMP;CHR & "http://schemas.xmlsoap.org/soap/envelope/" &AMP;CHR (+) & ">" & _
"<soap:Body>" & _
"<add xmlns=" &AMP;CHR & "http://localhost" &AMP;CHR (+) & ">" & _
"<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 XMLHTTP. Status = Then
Xmldoc.load (Xmlhttp.responsexml)
Response.Write XMLHTTP. status& "<br>"
Response.Write XMLHTTP. statustext& "<br> Implementation results:"
Response.Write Xmldoc.getelementsbytagname ("Addresult") (0). Text
Else
Response.Write XMLHTTP. status& "<br>"
Response.Write XMLHTTP. StatusText
End If
%>

All of the above are VBScript, for JavaScript is basically the same, only need to do some small changes, the specific code here is omitted.

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

Report:
Code for the WebService file Service1.asmx when testing:
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Diagnostics;
Using System.Web;
Using System.Web.Services;

Namespace possible
{
<summary>
Summary description of the Service1.
</summary>
[WebService (description= "My Web Service", name= "MyService", namespace= "http://localhost")]
public class MyService:System.Web.Services.WebService
{
Public MyService ()
{
CodeGen: This call is required by the ASP.net Web service designer
InitializeComponent ();
}

Code generated #region Component Designer

Required by the WEB service designer
Private IContainer components = null;

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
}

<summary>
Clean up all resources that are 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;
}
}
}



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.