Web Services 学习笔记

一 相关词语

WSDLWeb Services Description Language网络服务描述语言
SOAP 指简易对象访问协议
UDDIUniversal Description, Discovery and Integration通用描述、发现与集成服务
二 举例说明
<message name="getTermRequest">
   <part name="term" type="xs:string" />
</message>

<message name="getTermResponse">
   <part name="value" type="xs:string" />
</message>

<portType name="glossaryTerms">
  <operation name="getTerm">
      <input message="getTermRequest" />
      <output message="getTermResponse" />
  </operation>
</portType>

<binding type="glossaryTerms" name="b1">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
  <operation>
    <soap:operation
     soapAction="http://example.com/getTerm" />
    <input>
      <soap:body use="literal" />
    </input>
    <output>
      <soap:body use="literal" />
    </output>
  </operation>
</binding>

三 net webService举例

服务端文件

1.service

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService
{
    public Service () {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    
}

2.Service.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>

3.web.config

<add key="localhost.Service" value="http://localhost:1454/WebSite9/Service.asmx"/>

客户端文件 

4.web.config

<add key="localhost.Service" value="http://localhost:1454/WebSite9/Service.asmx"/>

 5.Service.wsdl

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
      <s:element name="HelloWorld">
        <s:complexType />
      </s:element>
      <s:element name="HelloWorldResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="HelloWorldSoapIn">
    <wsdl:part name="parameters" element="tns:HelloWorld" />
  </wsdl:message>
  <wsdl:message name="HelloWorldSoapOut">
    <wsdl:part name="parameters" element="tns:HelloWorldResponse" />
  </wsdl:message>
  <wsdl:portType name="ServiceSoap">
    <wsdl:operation name="HelloWorld">
      <wsdl:input message="tns:HelloWorldSoapIn" />
      <wsdl:output message="tns:HelloWorldSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="HelloWorld">
      <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>

  <wsdl:service name="Service">
    <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
      <soap:address location="http://localhost:1454/WebSite9/Service.asmx" />
    </wsdl:port>

  </wsdl:service>
</wsdl:definitions>

6.Default.aspx.cs

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        localhost.Service ls = new localhost.Service();
        Lable1.Text = ls.HelloWorld();
    }
}

7.Default.aspx

<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Lable1" runat="server"></asp:Label>
    </div>
    </form>
</body>

web service 解说

更简单一点,就是就是以函数的方式调用服务。
客户端和服务端,都是通过对http服务的封装,是客户端就向调用一个函数一样的调用远程的服务函数。
那么有http服务到向一个函数的调用都发生了什么呢?
首先http就是向一个地址发生http请求。服务端能够识别的只能是post或get的数据。
那么web Service是如何的,用这个基础的通讯协议实现一个像做函数调用的效果呢。
首先,简单对象协议是基础,对于参数的类型必须要有一个标准的定义。
所有使用web Service这种技术的语言,必须按照这个标准定义和解释你的数据。
具体过程如下。
1 客户端,将参数按照web Service封装好。
2 将封装好的对象,转化成http传输的数据。
3 服务端,收到请求后,将http传输的数据解析成对象。
4 将客户端的输入参数进行处理。
5 将处理完的返回值,按照web Service的要求封装好。
6 将封装好的返回值,转化成http传递给客户端。
7 客户端收到http的数据,按照web Service的协议,解析对象,最终客户端使用返回值,完成了一个函数的调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值