SoapHeader是增强 webservice的安全性的。例如(支付、认证)
1,写一个web service发布了服务器上。
2,通过添加服务引用发布的webservice。
3,调用。
MyWebService的网站
添加一个类继承?System.Web.Services.Protocols.SoapHeader ?; 作为传递使用的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyWebService
{
? ? public class MyHander:System.Web.Services.Protocols.SoapHeader
? ? {
? ? ? ? public string Name { get; set; }
? ? ? ? public string Pwd { get; set; }
? ? }
}
//具体认证的代码。(UserLoad)注意 ?加这个特性?[SoapHeader("header")]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
添加一个webservice。。
namespace MyWebService
{
? ? /// <summary>
? ? /// WebServiceTest 的摘要说明
? ? /// </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 WebServiceTest : System.Web.Services.WebService
? ? {
? ? ? ? public MyHander header;
? ? ? ? [WebMethod]
? ? ? ? [SoapHeader("header")]
? ? ? ? public bool UserLoad()
? ? ? ? {
? ? ? ? ? ? if (header.Name == "admin" && header.Pwd == "123")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? [WebMethod]
? ? ? ? public int AddTest(int num1,int num2)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return num1 + num2;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
生成发布。?
下一步调用。
新建网站 添加aspx页面。。
添加服务引用。地址要正确。http://121.23.4521:8098/WebServiceTest.asmx(设置好名空间:WQSWebService)。 注意服务器 不在本地 要在web.config下的?<system.web>下添加?? ? ? (开启远程访问。。)
<webServices>
? ? ? ? ?<protocols>
? ? ? ? ? ? <add name="HttpSoap"/>
? ? ? ? ? ? <add name="HttpPost"/>
? ? ? ? ? ? <add name="HttpGet"/>
? ? ? ? ? ? <add name="Documentation"/>
? ? ? ? ?</protocols>
</webServices>?
页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
? ? <title></title>
</head>
<body>
? ? <form id="form1" runat="server">
? ? <div>
? ? ? ? <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
? ? ? ??
? ? ? ? <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
? ? ? ? <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
? ??
? ? </div>
? ? </form>
</body>
</html>
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
? ? protected void Page_Load(object sender,EventArgs e)
? ? {
? ? }
? ? protected void Button1_Click(object sender,EventArgs e)
? ? {
? ? ? ? WQSWebService.WebServiceTestSoapClient test = new WQSWebService.WebServiceTestSoapClient();
? ? ? ? WQSWebService.MyHander hander = new WQSWebService.MyHander();
? ? ? ? hander.Name = TextBox1.Text;
? ? ? ? hander.Pwd = TextBox2.Text;
? ? ? ? bool res = test.UserLoad(hander);
? ? ? ??
? ? ? ? int res2 = test.AddTest(1,13);
? ? ? ? Button1.Text = res.ToString();
? ? }
}
输入 textbox1 =admin ?textbox2=123 。
调用成功? Button1.Text =True?
。。