标签:webservice web服务 soap 验证 添加webservice引用
工作中使用到较多的webservice,学习了一段时间,在这里总结一下在vs中使用webservice的过程和理解。
一 创建(服务器端)
在vs中穿件webservice是比较简单的。下面记录一下步骤。
步骤1:新建一个web项目,注意不能是类库。
步骤2:在新建的web项目上,添加新项--web服务;
自动生成的代码如下:
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
步骤3: 编写webservice服务代码;
步骤4:发布一下编写好的代码,运行后的地址就是当前webservice的地址咯;
Over!
二 调用(客户端)
直接上过程:
步骤1:在要添加webservice的项目上,右键引用--添加服务引用;
步骤2:注意这里有两种方式来添加webservice引用,一种是web reference(web引用),另一种叫:service reference(服务引用); 在添加的时候注意一下添加的是哪个,这个不大好描述;直接上图咯。下图表示服务引用↓(命名空间请自定义,就是代理类的命名空间):
如果点击上图的"高级"按钮,那么将会添加web引用↓(ps:根据下图的说明,web reference是net2.0版本的的引用方式,为了兼容性才保留下来;):
步骤3:添加完引用,并设置自定义的命名空间后,vs2012会使用工具自动根据URL去读取wsdl文件,然后生成代理类,你就可以用代理类实例化对象,然后接续工作咯····
Over!
三 验证调用者身份(webservice的登录)
使用webservice的验证规则大体有这么几种:验证调用者Ip地址、利用soap头验证调用者的帐号密码、利用ssl加密验证;这里要说的是利用soap header进行验证的方法。
步骤一:在webservice服务中添加一个类(这个类作为传递soap头帐号密码的载体;继承自抽象类:System.Web.Services.Protocols.SoapHeader)。代码如下
namespace PointStoreWebService
{
/// <summary>
/// 这个代码网上一大片,可以根据需要自己修改
/// </summary>
public class MySoapHeader:System.Web.Services.Protocols.SoapHeader
{
/// <summary>
/// 属性咯;
/// Tips:不想用自动属性,有可以快捷生成属性的方法:限定下环线开头的私有成员,然后Ctrl+R+E,vs2012会自动构造出属性咯
/// </summary>
private string _UserId = string.Empty;
public string UserId
{
get { return _UserId; }
set { _UserId = value; }
}
private string _PassWord = string.Empty;
public string PassWord
{
get { return _PassWord; }
set { _PassWord = value; }
}
/// <summary>
/// 无参构造咯
/// </summary>
public MySoapHeader()
{
}
/// <summary>
/// 有参构造构造咯
/// </summary>
/// <param name="id"></param>
/// <param name="pwd"></param>
public MySoapHeader(string id, string pwd)
{
Initial(id, pwd);
}
/// <summary>
/// 初始化咯
/// </summary>
/// <param name="id"></param>
/// <param name="pwd"></param>
public void Initial(string id, string pwd)
{
UserId = id;
PassWord = pwd;
}
/// <summary>
/// 验证咯
/// </summary>
/// <param name="id"></param>
/// <param name="pwd"></param>
/// <param name="msg"></param>
/// <returns></returns>
public bool IsValid(string id, string pwd,out string msg)
{
msg = "";
if (id == "admin" && pwd == "admin")
{
return true;
}
else
{
msg = "log error!";
return false;
}
}
/// <summary>
/// 重载咯
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public bool IsValid(out string msg)
{
return IsValid(_UserId, _PassWord, out msg);
}
}
}
步骤二:在webservice服务类中添加上面类型的成员;
public MySoapHeader header = new MySoapHeader();
步骤三:在需要进行验证的方法上添加属性:(这一步不要忘记,否则在调用的时候是 点 不出来MySoapHeader类的) [SoapHeader("header")]
步骤四:在需要验证的方法中调用MySoapHeader类中的方法进行流程控制咯。。。。
步骤五:在调用端(译并更新引用
(/ □ \)记得编),先实例化MySoapHeader类的一个对象,然后给对象的验证属性(这里就是UserId、PassWord进行赋值;)
步骤六:在使用代理类对象的方法的时候将 MySoapHeader类对象传递过去;
ps:这里需要注意的是:根据vs2012添加webservice方式的不同,传递soap头的方法也不一样;具体如下:
①当你是web reference方式时:在实例化代理类后(假设名为:ServiceOBJ),可以使用ServiceOBJ.MySoapHeaderValue=soap头对象名;
②当你是Service reference方式时:代理类对象是无法找到MySoapHeaderValue这个属性的。在需要验证的方法上,作为参数传递soap头对象:ServiceOBJ.MethodName(soap头对象)
当这个方法本来就有参数的时候,那么这个方法就自动多了个参数,且在第0个参数的位置;
Ove!!!
四 在软件发布后,动态的修改引用地址的配置方法
注意这里我使用的是新版本的service 引用方式;
当你添加一个service reference后,会在对应的项目下生成一个app.config文件:(如下)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="PointStoreServiceSoap" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:25548/PointStoreService.asmx"
binding="basicHttpBinding" bindingConfiguration="PointStoreServiceSoap"
contract="PointStoreService.PointStoreServiceSoap" name="PointStoreServiceSoap" />
</client>
</system.serviceModel>
</configuration>其中需要注意的是:
①basicHttpBinding节点下的<binding >,每天加一个service reference后,会生成一个binding节点。
②client节点下的endpoint,每添加service reference,也会增加一个endpoint节点。
如果添加webservice引用的项目是一个类库,那么就需要在网站的web.config下添加响应的模块来供类库读取。即在web.config配置文件中添加如下部分:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="1111111111111" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="6553600" maxBufferPoolSize="52428800" maxReceivedMessageSize="6553600"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="320" maxStringContentLength="8192000" maxArrayLength="163840"
maxBytesPerRead="40960" maxNameTableCharCount="163840" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://1111111111111111111111111.asmx"
binding="basicHttpBinding" bindingConfiguration="11111111111"
contract="1111111111111111" name="11111111111" />
</client>
</system.serviceModel>以上xml节的结构就是app.config配置文件中的相同,只是添加了一些属性进行限制(对于每个属性的含义,大家有需要可以internet 查查。)显然,binding 节点的name属性和endpoint节点是可以直接从app.config
copy过来的;
Over!
vs2012.net WebService简单的创建、使用、验证、发布后更改引用地址、web reference、service reference
标签:webservice web服务 soap 验证 添加webservice引用
原文地址:http://blog.csdn.net/chen943354086/article/details/45689331