打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[Web Service]在Web服务中使用SoapHeader

[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://tempuri.org/")]

[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.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/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();

    }

运行结果与之前一致。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
简单Web service 身份验证解决方案
WebService常用功能讲解
Web Service开发指南
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用)
调用Oracle EBS Webservice常见的几个错误
我与阿里云的一千零一夜-006-建立私人云存储
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服