Webservices-2.C#创建web服务,及引用访问、代码访问

时间:2023-03-09 08:55:48
Webservices-2.C#创建web服务,及引用访问、代码访问

注:web服务简介Webservices-1.web服务定义简介

以下均以C#语言为例

一、创建web服务(简单介绍,主要讨论客户端引用)

  打开VS创建网站项目,在网站项目中添加“WEB服务(ASMX)”,

  此时VS便已经默认建立好一个web服务。

  如需使用Session,请添加“[WebMethod(EnableSession=true)] ”,更多信息请实际编写代码时候查看智能提示,及msdn文档

  此时可以将服务部署于web服务器上,以IIS为例,部署完毕即可正确访问。有时创建IIS网站时,请注意配置使用的.net版本库。

  访问地址:http://172.168.0.40:8086/WebSer.asmx(个人PC示例,请勿随意copy)

二、服务解析概述

  在访问地址后添加?WSDL既可以查看服务说明文档http://172.168.0.40:8086/WebSer.asmx?WSDL

  

 <?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="ChineShine" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="ChineShine" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="ChineShine">
<s:element name="HelloWorld">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="ss" type="s:string" />
</s:sequence>
</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:element name="NoReturnValue">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="ss" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="NoReturnValueResponse">
<s:complexType />
</s:element>
<s:element name="NoParaValue">
<s:complexType />
</s:element>
<s:element name="NoParaValueResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="NoParaValueResult" 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:message name="NoReturnValueSoapIn">
<wsdl:part name="parameters" element="tns:NoReturnValue" />
</wsdl:message>
<wsdl:message name="NoReturnValueSoapOut">
<wsdl:part name="parameters" element="tns:NoReturnValueResponse" />
</wsdl:message>
<wsdl:message name="NoParaValueSoapIn">
<wsdl:part name="parameters" element="tns:NoParaValue" />
</wsdl:message>
<wsdl:message name="NoParaValueSoapOut">
<wsdl:part name="parameters" element="tns:NoParaValueResponse" />
</wsdl:message>
<wsdl:portType name="WebSerSoap">
<wsdl:operation name="HelloWorld">
<wsdl:input message="tns:HelloWorldSoapIn" />
<wsdl:output message="tns:HelloWorldSoapOut" />
</wsdl:operation>
<wsdl:operation name="NoReturnValue">
<wsdl:input message="tns:NoReturnValueSoapIn" />
<wsdl:output message="tns:NoReturnValueSoapOut" />
</wsdl:operation>
<wsdl:operation name="NoParaValue">
<wsdl:input message="tns:NoParaValueSoapIn" />
<wsdl:output message="tns:NoParaValueSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WebSerSoap" type="tns:WebSerSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap:operation soapAction="ChineShine/HelloWorld" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="NoReturnValue">
<soap:operation soapAction="ChineShine/NoReturnValue" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="NoParaValue">
<soap:operation soapAction="ChineShine/NoParaValue" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="WebSerSoap12" type="tns:WebSerSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap12:operation soapAction="ChineShine/HelloWorld" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="NoReturnValue">
<soap12:operation soapAction="ChineShine/NoReturnValue" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="NoParaValue">
<soap12:operation soapAction="ChineShine/NoParaValue" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WebSer">
<wsdl:port name="WebSerSoap" binding="tns:WebSerSoap">
<soap:address location="http://172.168.0.40:8086/WebSer.asmx" />
</wsdl:port>
<wsdl:port name="WebSerSoap12" binding="tns:WebSerSoap12">
<soap12:address location="http://172.168.0.40:8086/WebSer.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

  

折叠后查看

Webservices-2.C#创建web服务,及引用访问、代码访问

  

  注意:WSDL元素[1]基于XML语法描述了与服务进行交互的基本元素:

    Type(消息类型):数据类型定义的容器,它使用某种类型系统(如XSD)。
    Message(消息):通信数据的抽象类型化定义,它由一个或者多个part组成。
    Part:消息参数
    Operation(操作):对服务所支持的操作进行抽象描述,WSDL定义了四种操作: 1.单向(one-way):端点接受信息;2.请求-响应(request-response):端点接受消息,然后发送相关消息;3.要求-响应(solicit-response):端点发送消息,然后接受相关消息;4.通知(notification[2]):端点发送消息。
    Port Type (端口类型):特定端口类型的具体协议和数据格式规范。
    Binding:特定端口类型的具体协议和数据格式规范
    Port:定义为绑定和网络地址组合的单个端点。
    Service:相关端口的集合,包括其关联的接口、操作、消息等。

  2.1 targerNamespasce

    主要声明的是:本服务使用的命名空间 ,默认是:http://tempuri.org/
(打开一看,跑到bing去了,呵呵),可以自定义使用,如本文定义了:ChineShine

  2.2 wsdl:types

    主要是:服务接口里面的:方法名称,参数,返回值介绍

  2.3 wsdl:Message、wsdl:portType (参看以上注意)

  2.4 wsdl:Binding

    可以使用的协议访问,如示例中提示使用的soap(即soap1.1),soap12(即soap1.2)

  2.5 wsdl:service

    主要是:各种方式服务的接口地址

  综述:其实在调用别人web服务的时候,只要有web服务地址,即可能够使用,对方的服务,要详细正确使用还需服务开发者,提供详细调用文档。

三、客户端服务引用(这里才是重点啊)

  客户端引用主要包含:项目中引用服务地址,普通引用(主要指VS中的“添加服务引用”)

名称   使用场景 优缺点
普通引用   项目建立初期 初期建立,方便快捷,易于使用
 使用代理类    完善的项目,使用中的项目,

需求变更的项目,当然初期的也可以

 需要自己手工编写代码,

对于成熟使用的项目扩展性好

代码引用 post

完善的项目,使用中的项目,

需求变更的项目,当然初期的也可以

需要自己手工编写代码,

对于成熟使用的项目扩展性好

get
soap1.1
soap1.2

  3.1 普通引用

    在需要引用的VS项目中,引用→添加服务引用→添加服务地址“http://172.168.0.40:8086/WebSer.asmx”,

    编写基本代码即可使用

    TestServiceReference.WebSerSoapClient wc = new TestServiceReference.WebSerSoapClient();//初始化web服务客户端对象
            string ss=wc.HelloWorld("你好");//调用web服务的,接口方法  

    说明:这种方法适用于VS操作,其他语言及IDE未测试。优点方便快捷,简单易于使用。

  3.2 代码引用

    这里以soap1.1 为例,post,get比较简单,soap1.2 与soap1.1类似,讲述soap1.1后会添加全部代码

    

    打开其中一个接口,参看

  Webservices-2.C#创建web服务,及引用访问、代码访问

  

  

  注:前提条件,根据接口的wsdl,以及开发商提供的文档可以查看到:接口的地址(URL)、方法名(Method)、参数列表(Paras)、命名空间等信息(tns)

  编写请求代码:

    //创建一个HttpWebRequest
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
    request.Method = "POST";//请求方式
    request.ContentType = "text/xml; charset=utf-8";//网络文件的类型和网页的编码
    request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");//构建soap1.1 请求头文件
    request.Credentials = CredentialCache.DefaultCredentials;//获取系统凭据
    request.Timeout = 10000;//请求超时时间
    byte[] data = EncodeParsToSoap(Pars, XmlNs, MethodName, soapVersion);//组织发送内容,并序列化成字节流,详细参看附注代码,
    request.ContentLength = data.Length;//请求数据长度
            using (Stream writer = request.GetRequestStream())//请求参数
            {
                writer.Write(data, 0, data.Length);
                writer.Close();
            }
    XmlDocument doc = new XmlDocument(), doc2 = new XmlDocument();
    doc = ReadXmlResponse(request.GetResponse());//接收返回消息  request.GetResponse() 附注代码已修改成使用异步编程方式
    这是一次请求,就完成了。详细处理,请参看详细代码

  附注:详细代码(复制到程序中,直接就可以使用哦!)

 using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization; namespace TestWS
{
public class WebServicesCodeTool
{
private System.Collections.Hashtable _xmlNamespaces = new System.Collections.Hashtable();//缓存xmlNamespace,避免重复调用GetNamespace /// <summary>
/// 需要WebService支持Post调用
/// </summary>
public System.Xml.XmlDocument QueryPostWebService(String URL, String MethodName, System.Collections.Hashtable Pars)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
SetWebRequest(request);
byte[] data = EncodePars(Pars);
WriteRequestData(request, data);
return ReadXmlResponse(request.GetResponse());//这里没有修改成异步方式,建议自行修改
} /// <summary>
/// 需要WebService支持Get调用
/// </summary>
public XmlDocument QueryGetWebService(String URL, String MethodName, Hashtable Pars)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + ParsToString(Pars));
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
SetWebRequest(request);
return ReadXmlResponse(request.GetResponse());
}
/// <summary>
/// 通用WebService调用(Soap),参数Pars为String类型的参数名、参数值、soap版本(如果支持1.2,使用1.2,默认填写1.1)
/// </summary>
/// <param name="URL"></param>
/// <param name="MethodName"></param>
/// <param name="Pars"></param>
/// <param name="soapVersion"></param>
/// <returns></returns>
public XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars, string soapVersion)
{
if (_xmlNamespaces.ContainsKey(URL))
{
return QuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces[URL].ToString(), soapVersion);
}
else
{
return QuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL), soapVersion);
}
}
XmlDocument doc = null;
private XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars, string XmlNs, string soapVersion)
{
_xmlNamespaces[URL] = XmlNs;//加入缓存,提高效率
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST"; if (soapVersion == "1.2")
{
request.ContentType = "application/soap+xml;charset=utf-8";
}
else
{
request.ContentType = "text/xml; charset=utf-8";
request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");
}
SetWebRequest(request);
byte[] data = EncodeParsToSoap(Pars, XmlNs, MethodName, soapVersion);
WriteRequestData(request, data); XmlDocument doc2 = new XmlDocument();
IAsyncResult ar = request.BeginGetResponse(AsyncCallbackGetResponse, request);//异步方式
//XmlDocument doc = new XmlDocument();
//doc = ReadXmlResponse(request.GetResponse());//原来同步方式
if (ar.IsCompleted && doc != null)
{
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
String RetXml = "";
if (soapVersion == "1.2")
{
mgr.AddNamespace("soap12", "http://www.w3.org/2003/05/soap-envelope");
RetXml = doc.SelectSingleNode("//soap12:Body/*/*", mgr).InnerXml;
}
else
{
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
RetXml = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;
}
doc2.LoadXml("<root>" + RetXml + "</root>");
AddDelaration(doc2); }
return doc2;
} private void AsyncCallbackGetResponse(IAsyncResult ar)
{
WebRequest request = ar.AsyncState as WebRequest;
var response = request.EndGetResponse(ar);
Stream stream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
string content = reader.ReadToEnd();
if (!string.IsNullOrEmpty(content))
{
doc = new XmlDocument();
doc.LoadXml(content);
}
}
} /// <summary>
/// 第一次执行获取要访问的命名空间
/// </summary>
/// <param name="URL"></param>
/// <returns></returns>
private string GetNamespace(String URL)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");
SetWebRequest(request);
WebResponse response = request.GetResponse();
XmlDocument doc = null;
using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
doc = new XmlDocument();
doc.LoadXml(sr.ReadToEnd());
sr.Close();
}
return doc != null ? doc.SelectSingleNode("//@targetNamespace").Value : "http://tempuri.org/";
} private byte[] EncodeParsToSoap(Hashtable Pars, String XmlNs, String MethodName, string soapVersion)
{
XmlDocument doc = new XmlDocument();
XmlElement soapBody = null;
if (soapVersion == "1.2")
{
doc.LoadXml("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"></soap12:Envelope>");
AddDelaration(doc);
soapBody = doc.CreateElement("soap12", "Body", "http://www.w3.org/2003/05/soap-envelope");
}
else
{
doc.LoadXml("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"></soap:Envelope>");
AddDelaration(doc);
soapBody = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
}
XmlElement soapMethod = doc.CreateElement(MethodName);
soapMethod.SetAttribute("xmlns", XmlNs);
foreach (string k in Pars.Keys)
{
XmlElement soapPar = doc.CreateElement(k);
soapPar.InnerXml = ObjectToSoapXml(Pars[k]);
soapMethod.AppendChild(soapPar);
}
soapBody.AppendChild(soapMethod);
doc.DocumentElement.AppendChild(soapBody);
return Encoding.UTF8.GetBytes(doc.OuterXml);
} private string ObjectToSoapXml(object o)
{
XmlSerializer mySerializer = new XmlSerializer(o.GetType());
MemoryStream ms = new MemoryStream();
mySerializer.Serialize(ms, o);
XmlDocument doc = new XmlDocument();
doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
if (doc.DocumentElement != null)
{
return doc.DocumentElement.InnerXml;
}
else
{
return o.ToString();
}
} private void SetWebRequest(HttpWebRequest request)
{
request.Credentials = CredentialCache.DefaultCredentials;
request.Timeout = ;
} private void WriteRequestData(HttpWebRequest request, byte[] data)
{
request.ContentLength = data.Length;
using (Stream writer = request.GetRequestStream())
{
writer.Write(data, , data.Length);
writer.Close();
}
} private byte[] EncodePars(Hashtable Pars)
{
return Encoding.UTF8.GetBytes(ParsToString(Pars));
} private String ParsToString(Hashtable Pars)
{
StringBuilder sb = new StringBuilder();
foreach (string k in Pars.Keys)
{
if (sb.Length > )
{
sb.Append("&");
}
sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));
}
return sb.ToString();
} private XmlDocument ReadXmlResponse(WebResponse response)
{
String retXml = "";
using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
retXml = sr.ReadToEnd();
sr.Close();
}
XmlDocument doc = new XmlDocument();
if (!string.IsNullOrEmpty(retXml)) { doc.LoadXml(retXml); }
return doc;
} private void AddDelaration(XmlDocument doc)
{
XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(decl, doc.DocumentElement);
} }
}

  项目中的调用:

    WebServicesTool ws = new WebServicesTool();
    Hashtable paras = new Hashtable();
           paras.Add("ss", "你好");
           XmlDocument docPost = ws.QueryPostWebService("http://172.168.0.40:8086/WebSer.asmx", "HelloWorld", paras);
           XmlDocument docSoap = ws.QuerySoapWebService("http://172.168.0.40:8086/WebSer.asmx", "HelloWorld", paras, "1.2");
           context.Response.Write(docPost.OuterXml);
           context.Response.Write(docSoap.OuterXml);

  3.3 使用wsdl代理类

    ①、查看webservice服务,访问Service Document,即这里的wsdl文件,

    ②、将查看的wsdl文件保存。

    ③、本机使用VS自带的“VS2013 开发人员命令提示”。点击打开

    ④、wsdl  文件路径,即可生成代理类,调用代理类即可访问不同程序编写的webservice服务

四、心得

  4.1 使用代码方式,使得成熟稳定的项目,易于扩展。

  4.2 不论何种语言,只要读懂wsdl及接口开发商提供文档即可,调用web接口。

  4.3 一些关于soap1.2 的简介http://blog.****.net/xiaojianpitt/article/details/5258254

    更多的soap介绍,可以查看:http://www.ibm.com/developerworks/cn/xml/x-sisoap/