博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebService应用实列
阅读量:4660 次
发布时间:2019-06-09

本文共 2504 字,大约阅读时间需要 8 分钟。

1.新建项目WebService1

2.创建APISoapHeader,继承

System.Web.Services.Protocols.SoapHeader

APISoapHeader代码如下:

using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebService1 {
public class APISoapHeader:System.Web.Services.Protocols.SoapHeader {
private string _localhostkey = string.Empty; private string _secretkey = string.Empty; /// /// 构造函数 /// public APISoapHeader() {
//配置文件的SoapHeader_APIKey this._localhostkey = System.Configuration.ConfigurationManager.AppSettings.Get("SoapHeader_APIKey"); } /// /// 获取或设置密钥 /// public string SecretKey {
get { return this._secretkey; } set { this._secretkey = value; } } /// /// 是否为安全的API调用 /// public bool IsSafeCall {
get {
if (this._localhostkey == this._secretkey) return true; else return false; } } } }

WebService1项目的配置文件(web.config)

Service1.asmx 代码结构

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebService1 {
/// /// Service1 的摘要说明 /// [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 Service1 : System.Web.Services.WebService {
public APISoapHeader soapHeader = new APISoapHeader();//声明header [System.Web.Services.Protocols.SoapHeader("soapHeader")]//表明调用时需要传入header [WebMethod] public string HelloWorld() {
if (!soapHeader.IsSafeCall)//传入的自定义APISoapHeader不正确,讲返回“你好!” {
return "你好!"; } return "Hello World"; } } }

客户端调用:引用服务应用,ServiceReference1

ServiceReference1.Service1SoapClient soap = new WebApplication1.ServiceReference1.Service1SoapClient(); protected void Page_Load(object sender, EventArgs e)         {
ServiceReference1.APISoapHeader SoapHeader = new WebApplication1.ServiceReference1.APISoapHeader(); //设置SoapHeader Key SoapHeader.SecretKey = "1127d575-5633-4b97-a0fd-bb6bfb0d27de 5"; //WebService的方法HelloWorld没有使用APISoapHeader作参数,但是声明了 [System.Web.Services.Protocols.SoapHeader("soapHeader")] Response.Write(soap.HelloWorld(SoapHeader)); }

 

转载于:https://www.cnblogs.com/luofuxian/archive/2012/02/28/2371000.html

你可能感兴趣的文章
JAVA多线程
查看>>
delphi 更改DBGrid 颜色技巧
查看>>
POJ 2031 Building a Space Station
查看>>
任意阶幻方(魔方矩阵)C语言实现
查看>>
织梦教程
查看>>
杭电多校 Harvest of Apples 莫队
查看>>
C/C++心得-结构体
查看>>
函数名作为参数传递
查看>>
apt-get for ubuntu 工具简介
查看>>
数值计算算法-多项式插值算法的实现与分析
查看>>
day8-异常处理与网络编程
查看>>
Python基础-time and datetime
查看>>
shell脚本练习01
查看>>
WPF图标拾取器
查看>>
通过取父级for循环的i来理解闭包,iife,匿名函数
查看>>
HDU 3374 String Problem
查看>>
数据集
查看>>
[Leetcode] unique paths ii 独特路径
查看>>
HDU 1217 Arbitrage (Floyd + SPFA判环)
查看>>
IntelliJ idea学习资源
查看>>