How to Use soap in ASP to call web services developed by C #
Many of my friends have asked me how to call web services in other environments,
The principle is actually very simple. We all know that Web Services communicate with each other based on the SOAP protocol.
You can use soap to access web services.
The following is a specific example to illustrate the call process.
Development Environment: Windows 2000
Soap tookit3.0
IIS
I used C # To write the following Web Services:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. diagnostics;
Using system. Web;
Using system. Web. Services;
Namespace webservice_1
{
Public class service1: system. Web. Services. WebService
{
String err_message;
Sqldataadapter my_adapter;
Dataset DS = new dataset ();
Dataset ds1 = new dataset ();
Dataset ds_err;
Dataset ds_value = new dataset ();
Datatable mydatatable;
Public service1 ()
{
Initializecomponent ();
}
Private system. Data. sqlclient. sqlconnection sqlconnection1;
# Region component designer generated code
// Required by the Web Service designer
Private icontainer components = NULL;
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. sqlconnection1 = new system. Data. sqlclient. sqlconnection ();
//
// Sqlconnection1
//
This. sqlconnection1.connectionstring = "Data Source = qieyj; initial catalog = photos; persist Security info = false; user id = sa; W" +
"Orkstation id = qieyj; packet size = 4096 ";
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing & components! = NULL)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}
# Endregion
[Webmethod]
Public dataset account_check (string username, string password, string template, string org_nummer)
{
String SQL;
String cond;
Cond = org_nummer.trim (). Replace ("",",");
SQL = "select * from account where username = '" + username + "' and Password = '" + password + "'";
My_adapter = new sqldataadapter (SQL, sqlconnection1 );
Sqlconnection1.open ();
My_adapter.fill (DS, "login ");
Sqlconnection1.close ();
If (Ds. Tables [0]. Rows. Count = 0)
{
Ds_err = new dataset ();
Mydatatable = new datatable ("error ");
Datacolumn mydatacolumn;
Datarow mydatarow;
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. String ");
Mydatacolumn. columnname = "error ";
Mydatatable. Columns. Add (mydatacolumn );
Mydatarow = mydatatable. newrow ();
Mydatarow ["error"] = "status = 6 ";
Mydatatable. Rows. Add (mydatarow );
Ds_err.tables.add (mydatatable );
Return ds_err;
}
// Check if the customer have enough points
Err_message = points_check (Template );
If (err_message! = "")
{
Ds_err = new dataset ();
Mydatatable = new datatable ("error ");
Datacolumn mydatacolumn;
Datarow mydatarow;
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. String ");
Mydatacolumn. columnname = "error ";
Mydatatable. Columns. Add (mydatacolumn );
Mydatarow = mydatatable. newrow ();
Mydatarow ["error"] = err_message;
Mydatatable. Rows. Add (mydatarow );
Ds_err.tables.add (mydatatable );
Return ds_err;
}
// Check if belong to some rules, if no, return result
Ds_err = rules_check (template, Cond );
Return ds_err;
}
}
}
ASP file: soapclient. asp
<HTML>
<Head>
<Title> calling a WebService from classic ASP, using the soap toolkit </title>
<Script language = "VBScript">
Dim xmldoc
Set xmldoc = Createobject ("msxml2.domdocument ")
Xmldoc. async = false
Xmldoc. Load "response. xml"
Function showsource ()
Source. innertext = xmldoc. xml
End Function
</SCRIPT>
</Head>
<Body onload = "showsource ()" Language = "JavaScript">
<%
Dim username, password, template, org_number
Username = trim (request. Form ("username "))
Password = trim (request. Form ("password "))
Template = trim (request. Form ("template "))
Org_number = trim (request. Form ("org_number "))
Set xmldoc = server. Createobject ("msxml2.domdocument ")
Xmldoc. async = false
If username <> "" then
On Error resume next
Set soapclient = Createobject ("mssoap. soapclient30 ")
Soapclient. clientproperty ("serverhttprequest") = true
Soapclient. mssoapinit "http: // localhost/webservice_1/service1.asmx? WSDL "," service1 "," service1soap ",""
Set nodelist = soapclient. account_check (username, password, template, org_number)
Set nodedetails = nodelist. Item (1)
Set nodenamncount = nodedetails. selectnodes ("// org_nummer ")
Xmldoc. loadxml (nodelist. Item (1). XML)
Xmldoc. Save (server. mappath ("response. xml "))
If err <> 0 then
Response. Write err. Description + "<br>"
Response. Write "faultcode =" + soapclient. faultcode + "<br>"
Response. Write "faultstring =" + soapclient. faultstring + "<br>"
Response. Write "faultactor =" + soapclient. faultactor + "<br>"
Response. Write "detail =" + soapclient. Detail
End if
End if
%>
<Form method = post name = form> & nbsp;
<P align = center> <br> Username: & nbsp; <input name = username> <br>
<Br> password: & nbsp; <input name = PASSWORD> <br>
<Br> template: & nbsp; <input name = template> <br>
<Br> org_number: <input name = org_number> <br> </P>
<Div align = center> <input type = "Submit" value = "Submit" name = submit> </div> </form>
<P> <a href = "response. xml" target = "_ blank"> response. xml </a> </P>
XML result: <Div id = source> </div>
</Body>
</Html>