分享

[Web Service]在Web服务中使用SoapHeader

 昵称8888806 2012-03-01

[Web Service]在Web服务中使用SoapHeader

本文将演示如何在Web服务中使用SoapHeader,同时介绍在VS2010 IDE中不同的两种引用方式。

1)创建空的ASP.NET网站SoapHeaderTest

2)创建类MyCustomSoapHeader继承自SoapHeader,代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services.Protocols;

/// <summary>

/// My Custom SoapHeader

/// </summary>

publicclassMyCustomSoapHeader:SoapHeader

{

    /// <summary>

    /// user name for validation

    /// </summary>

    publicstring UserName;

    /// <summary>

    /// user password for validation

    /// </summary>

    publicstring UserPwd;

}

3)新建Web服务MyWebService.asmx;为服务添加自定义字段CutomHeader,并且添加WebMethod Test,设置SoapHeader,最终代码如下:

using System.Web.Services;

using System.Web.Services.Protocols;

 

/// <summary>

/// Summary description forMyWebService

/// </summary>

[WebService(Namespace = "http:///")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// Toallow this Web Service to be called from script, using ASP.NET AJAX, uncommentthe following line. 

//[System.Web.Script.Services.ScriptService]

publicclassMyWebService : System.Web.Services.WebService {

    publicMyWebService () {

        //Uncommentthe following line if using designed components 

        //InitializeComponent();

    }

    /// <summary>

    /// input custom header

    /// </summary>

    public MyCustomSoapHeader header;

    /// <summary>

    /// Test method that return user name and password.

    /// </summary>

    /// <returns></returns>

    [WebMethod]

    [SoapHeader("header",Direction=SoapHeaderDirection.In)]

    publicstring Test()

    {

        if(header == null) return "Header is null";

        else return string.Format("Username is {0} and password is {1}.",header.UserName,header.UserPwd);

    }

}

4)新建一个网页Default.aspx

包含一个TextBoxButton控件,当点击Button后,引用Web服务的方法,并将返回值显示在TextBox中,Web页面标记如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml">
<head runat="server">
    <title>SoapHeader Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:TextBox ID="TextBoxReturnString" runat="server" Height="80px"
            TextMode="MultiLine" Width="600px"></asp:TextBox>
        <br />
        <asp:Button ID="ButtonTest" runat="server" Text="Invoke Web Method"
            onclick="ButtonTest_Click" />
    </div>
    </form>
</body>
</html>

5)添加对Web Service的引用:

(点击Discover按钮即可查找到本解决方案的Web服务)

修改ButtonTest_Click方法为:

protected void ButtonTest_Click(object sender, EventArgs e)

    {

       MyWebServiceSoapClient client = new MyWebServiceSoapClient();

       MyServiceReference.MyCustomSoapHeader header = new MyServiceReference.MyCustomSoapHeader();

       header.UserName = "Raymond";

       header.UserPwd = "123";

       this.TextBoxReturnString.Text = client.Test(header);

    }

运行结果:

6)亦可以添加Web服务而非Service Reference:

对于高版本的VSIDE,比如2010,添加Web服务按照如下方式:



相应的方法ButtonTest_Click修改为:

protectedvoidButtonTest_Click(object sender, EventArgs e)

    {

        MyWebServiceReference.MyWebService service = newMyWebServiceReference.MyWebService();

        service.MyCustomSoapHeaderValue = new MyWebServiceReference.MyCustomSoapHeader();

       service.MyCustomSoapHeaderValue.UserName = "Raymond";

        service.MyCustomSoapHeaderValue.UserPwd= "123";

        this.TextBoxReturnString.Text= service.Test();

    }

运行结果与之前一致。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多