如何在ASP环境下利用SOAP来调用C#开发的Web Services

类别:.NET开发 点击:0 评论:0 推荐:

如何在ASP环境下利用SOAP来调用C#开发的Web Services
作为.NET Web Services的斑竹,很多朋友都问我如何在其他环境下调用Web Services,
原理其实很简单,大家都知道Web Services是基于SOAP协议通信的,在其他环境下自然
可以利用SOAP来访问Web Services了。
下面我就一个具体的例子来说明调用过程。
开发环境:windows 2000
          SOAP Tookit3.0
          IIS
我用C#写了如下一个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
  
  //Web 服务设计器所必需的
  private IContainer components = null;
    
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </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>
  /// 清理所有正在使用的资源。
  /// </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文件: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>

 

本文地址:http://com.8s8s.com/it/it45701.htm