1:在服务器端定义和处理soap标头
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace TestWS
{
/// <summary>
/// Service1 的摘要说明。
/// </summary>
public class TestWS : System.Web.Services.WebService
{
public MyHeader myHeaderMemberVariable;
public TestWS()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
#region 组件设计器生成的代码
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB 服务示例
// HelloWorld() 示例服务返回字符串 Hello World
// 若要生成,请取消注释下列行,然后保存并生成项目
// 若要测试此 Web 服务,请按 F5 键
[WebMethod]
[SoapHeader("myHeaderMemberVariable")]
public string HelloWorld(string name)
{
if(myHeaderMemberVariable.Username=="admin")
{
}
if(name=="")
return "Hello World";
return "Hello,"+name;
}
}
public class MyHeader:SoapHeader
{
public string Username;
public string Password;
}
}
2:生成处理soap标头的客户端
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace TestWSApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblMess;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblMess = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblMess
//
this.lblMess.Location = new System.Drawing.Point(64, 80);
this.lblMess.Name = "lblMess";
this.lblMess.TabIndex = 0;
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(64, 32);
this.txtName.Name = "txtName";
this.txtName.TabIndex = 1;
this.txtName.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 32);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "调用TestWS";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 157);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtName);
this.Controls.Add(this.lblMess);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
localhost.MyHeader mySoapHeader=new localhost.MyHeader();
mySoapHeader.Username="admin";
mySoapHeader.Password="";
localhost.TestWS proxy=new localhost.TestWS();
proxy.MyHeaderValue=mySoapHeader;
lblMess.Text=proxy.HelloWorld(txtName.Text);
}
}
}