一、WebService的调试

net 2.0新建webservice为了安全考虑,默认关闭了Post和Get方法 。

让其打开,可在Web.config文件的<system.web>下增加(如果已经存在就修改之)

<webServices>
<protocols>
<add name="HttpGet"/> <!--或<add name="HttpPost"/>-->
</protocols>
</webServices>

Web服务定义如下:

[WebMethod]
public int Add(int i)
{
return 33+i;
}

通过URL访问Web服务,HttpGet方式返回XML:

http://../WebServices1.asmx/Add?i=1

返回:

<?xml version="1.0"?>
<int xmlsns="..">34</int>

二、WebMethod属性:

WebMethod有6个属性:

  1. .Description:是对webservice方法描述的信息。就像webservice方法的功能注释,可以让调用者看见的注释。
  2. .EnableSession:指示webservice否启动session标志,主要通过cookie完成的,默认false。
  3. .MessageName:主要实现方法重载后的重命名。
  4. .TransactionOption:指示 XML Web services 方法的事务支持。
  5. .CacheDuration:Web支持输出高速缓存,这样webservice就不需要执行多遍,可以提高访问效率,
  6. .BufferResponse:配置WebService方法是否等到响应被完全缓冲完,才发送信息给请求端。普通应用要等完全被缓冲完才被发送的!

三、EnableSession属性

指示webservice否启动session标志,主要通过cookie完成的。默认为false。

[WebMethod(true)] //默认参数为EnableSession
public string Login(string name)
{
Context.Session["name"] = name;
return name;
}
[WebMethod(EnableSession = true)]
public string GetName()
{
if (Context.Session["name"] != null)
return Context.Session["name"].ToString();
else
return "";
}

在客户端,要在实例化了webservice代理类之后,要为它的CookieContainer 实例化一个 new CookieContainer(),这样才能使用session存储状态多个页面使用。

新类继承引用的webservice,并给CookieContainer赋值。

public class WebService1:localhost.WebService
{
private static System.Net.CookieContainer cookieContainer; static WebService1()
{
cookieContainer = new System.Net.CookieContainer();//静态初始化
} public WebService1()
{
this.CookieContainer = cookieContainer;
}
}

在各个页面使用派生的WebService类,可以获得同一个CookieContainer :

protected void btnLogin_Click(object sender, EventArgs e)
{
WebService1 ws = new WebService1();
ws.Login(txtName.Text);
}
protected void btnGetName_Click(object sender, EventArgs e)
{
WebService1 ws = new WebService1();
lblName.Text = ws.GetName();
}

四、MessageName属性

主要实现方法重载后的重命名,在Soap消息中引用时,SOAP使用MessageName而非方法名。

在下面的示例中,MessageName 用于消除两个 Add 方法的歧义。
通过Add访问的是第一个方法,而通过Add2访问的是第二个方法!

public class Calculator : WebService {
// The MessageName property defaults to Add for this XML Web service method.
[WebMethod]
public int Add(int i, int j) {
return i + j;
}
    [WebMethod(MessageName="Add2")]
public int Add(int i, int j, int k) {
return i + j + k;
}
}

访问:http://../WebServices1.asmx/Add2?i=1

五、WebService基于SOAPHeadear实现安全认证

1、首先自定义SoapHeader,需继承自System.Web.Services.Protocols.SoapHeader

/// <summary>
///自定义的SoapHeader
/// </summary>
public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
{
public MySoapHeader()
{
} public MySoapHeader(string userName, string passWord)
{
this.UserName = userName;
this.PassWord = passWord;
} public string UserName {set;get;} public string PassWord { set; get; }
}

2、添加WebService:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
//声明Soap头实例
public MySoapHeader myHeader = new MySoapHeader(); [System.Web.Services.Protocols.SoapHeader("myHeader")]
[WebMethod]
[SoapHeader(myHeader)]//用户身份验证的SOAP头
public string HelloWord()
{
//可以通过存储在数据库中的用户与密码来验证
if (myHeader.UserName.Equals("SoapHeader") & myHeader.PassWord.Equals("456789"))
{
return "调用服务成功!";
}
else
{
return "对不起,您没有权限调用此服务!";
}
}
}

3、客户端调用,设置SoapHeader;

localhost.WebService service = new localhost.WebService();

//将用户名与密码存入SoapHeader;
localhost.MySoapHeader header = new localhost.MySoapHeader();
header.UserName = "SoapHeader";
header.PassWord = "456789";
service.MySoapHeaderValue = header;
Console.WriteLine("设置SoapHeader:" + service.HelloWord()); //或者
Console.WriteLine("设置SoapHeader:" + service.HelloWord(header));

[WebMethod]参数介绍的更多相关文章

  1. SQLMAP参数介绍

    转自:http://zhan.renren.com/bugpower?gid=3602888498044629629&checked=true SQLMAP参数介绍 sqlmap的使用方式:p ...

  2. G++ 参数介绍(转载)

    g++参数介绍 From: http://www.cnblogs.com/lidan/archive/2011/05/25/2239517.html gcc and g++分别是gnu的c & ...

  3. pentaho cde 画图参数介绍

    初步接触pentaho,由于在国内的资料很少,唯有看英文文档,做了N次反复尝试,挖掘了pentaho CDE中画图的一些基本参数. 下面就列出来了一些常用参数介绍: crosstabMode:表明如果 ...

  4. mysql性能优化学习笔记-参数介绍及优化建议

    MySQL服务器参数介绍 mysql参数介绍(客户端中执行),尽量只修改session级别的参数. 全局参数(新连接的session才会生效,原有已经连接的session不生效) set global ...

  5. 【体系结构】Oracle参数介绍

    [体系结构]Oracle参数介绍 1  BLOG文档结构图 2  前言部分 2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩ ...

  6. Bootstrap Paginator 分页插件参数介绍及使用

    Bootstrap Paginator是一款基于Bootstrap的js分页插件,功能很丰富,个人觉得这款插件已经无可挑剔了.它提供了一系列的参数用来支持用户的定制,提供了公共的方法可随时获得插件状态 ...

  7. Apache中 RewriteRule 规则参数介绍

    Apache中 RewriteRule 规则参数介绍 摘要: Apache模块 mod_rewrite 提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.它支持每个完整规则可以拥有不限数量 ...

  8. Linux 启动参数介绍

    Linux 启动参数介绍 取自2.6.18 kernel Documentation/i386/boot.txt 文件中介绍 vga= 这里的不是一个整数(在C语言表示法中,应是十进制,八进制或者十六 ...

  9. Xcopy参数介绍

    DOS批处理命令,永远是不朽的命令,不仅功能强大,同时,速度也是最快的!但是,很多新手学习计算机,都已经遗忘了本不该忘记的批处理命令. 我们不可数典忘祖,该学习的还是要学习,不该忘记的还是不能忘记,尤 ...

随机推荐

  1. The request was aborted: Could not create SSL/TLS secure channel

    一.背景: 公司底层服务CDN从Akamai迁移到阿里云之后, 使用该服务的一个应用报错如下: System.AggregateException: One or more errors occurr ...

  2. C++中静态成员函数和普通成员函数存储方式相同

    先从一个示例查看类的创建过程中,静态成员函数和普通成员函数的存储区别. #include "stdafx.h" #include<iostream> #include& ...

  3. GC(Garbage Collection)

    GC(Garbage Collection) GC背景 ​  创建对象会消耗内存,如果不回收对象占用的内存,内存使用率会越来越高,最终出现OutOfMemoryError(OOM) ​  在C++中专 ...

  4. homestead的创建和使用

    1.下载vistualbox和vagrant并安装 2.安装了git的话就在想设置的目录或者文件夹下用git命令执行vagrant box add laravel/homestead,或者用cmd命令 ...

  5. unicode 格式 转汉字

    function decodeUnicode($str){ return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function( ...

  6. Centos7.3安装nexus12.1

    nexus.12.1-01的安装             1.下载nexus             2.上传到服务器/root/             3.解压                 t ...

  7. python学习-14 基本数据类型3

    1.字符串 获取字符串的字符,例如: test = 'abcd' a= test[0] # 通过索引,下标,获取字符串中的某一个字符 print(a) b = test[0:1] # 通过下标的 范围 ...

  8. 笔记-9:使用random库生成随机数

    random:主要目的是生成随机数 函数 说明 seed(a=None) 初始化随机数,默认值为当前系统时间 random() 生成一个[0.0,1.0)之间的随机数小数 randint(a,b) 生 ...

  9. python标准库之collections介绍

    collections----容器数据类型 collections模块包含了除list.dict.和tuple之外的容器数据类型,如counter.defaultdict.deque.namedtup ...

  10. 串口(USART)框图的讲解

    STM32 的 USART 简介 通用同步异步收发器(Universal Synchronous Asynchronous Receiver and Transmitter)是一个串行通信设备,可以灵 ...