雖然 soap 標頭可以包含與該消息相關的數據(因為 soap 規范沒有嚴格地定義 soap 標頭的內容),但是它們通常包含 web 服務器中基礎結構處理的信息。
使用 asp.net 創建的 xml web services 可以定義和操作 soap 標頭。定義 soap 標頭是通過定義表示特定 soap 標頭中數據的類以及從
創建一個從 soapheader 類派生的類,其名稱與 soap 標頭的根元素匹配。
public class myheader : soapheader
添加公共字段或屬性,與 soap 標頭中每個元素的名稱和它們各自的數據類型匹配。
例如,在給定以下 soap 標頭的情況下,其后的類定義一個表示 soap 標頭的類。
<soap:header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<myheader xmlns="http://www.contoso.com">
<username>myusername</username>
<password>mypassword</password>
</myheader>
</soap:header>
public class myheader : soapheader
{
public string username;
public string password;
}
在 xml web services 中處理 soap 標頭
將公共成員添加到實現表示 soap 標頭類型的 xml web services 的類。
[webservice(namespace="http://www.contoso.com")]
public class mywebservice
{
// add a member variable of the type deriving from soapheader.
public myheader myheadermembervariable;
將 soapheader 屬性應用于要處理 soap 標頭的每個 xml web services 方法。將 soapheader 特性的 membername 屬性設置為第一步中創建的成員變量的名稱。
[webmethod]
[soapheader("myheadermembervariable")]
public void mywebmethod()
在應用 soapheader 特性的每個 xml web services 方法中,訪問在第一步中創建的成員變量以處理在 soap 標頭中發送的數據。
[webmethod]
[soapheader("myheadermembervariable")]
public void mywebmethod()
{
// process the soapheader.
if (myheadermembervariable.username == "admin")
{
// do something interesting.
}
}
實例:
mywebservice xml web services 具有一個名為 myheadermembervariable 的成員變量,該成員變量屬于從 soapheader (myheader) 派生的類型并設置為 soapheader 特性的 membername 屬性。另外,將 soapheader 特性應用于指定 myheadermembervariable 成員變量的 mywebmethod xml web services 方法。在 mywebmethod xml web services 方法中,訪問 myheadermembervariable 成員變量來獲取 soap 標頭的 username xml 元素的值。
<%@ webservice language="c#" class="mywebservice" %>
using system.web.services;
using system.web.services.protocols;
// define a soap header by deriving from the soapheader base class.
public class myheader : soapheader
{
public string username;
public string password;
}
[webservice(namespace="http://www.contoso.com")]
public class mywebservice
{
// add a member variable of the type deriving from soapheader.
public myheader myheadermembervariable;
// apply a soapheader attribute.
[webmethod]
[soapheader("myheadermembervariable")]
public void mywebmethod()
{
// process the soapheader.
if (myheadermembervariable.username == "admin")
{
// do something interesting.
}
}
}
生成處理 soap 標頭的客戶端
當與 xml web services 進行通訊時,xml web services 客戶端可以發送和接收 soap 標頭。當使用 wsdl.exe 實用工具為預期或返回 soap 標頭的 xml web services 生成代理類時,該代理類包括有關 soap 標頭的信息。明確地說,代理類具有表示 soap 標頭的成員變量,這些 soap 標頭與 xml web services 中的 soap 標頭互相關聯。代理類也具有表示 soap 標頭的相應的類的定義。例如,為上面的 xml web services 生成的代理類將具有一個 myheader 類型的成員變量以及 myheader 類的定義。
創建表示 soap 標頭的類的新實例。
myheader mysoapheader = new myheader();
為該 soap 標頭填充值。
mysoapheader.username = "username";
mysoapheader.password = "password";
創建該代理類的新實例。
mywebservice proxy = new mywebservice();
將該 soap 標頭對象分配到表示 soap 標頭的代理類的成員變量。
proxy.myheadervalue = mysoapheader
對與 xml web services 方法通訊的代理類調用方法。
發送到 xml web services 的 soap 請求的 soap 標頭部分將包括存儲在 soap 標頭對象中數據的內容。
string results = proxy.mywebmethod();
下面演示如何將 soap 標頭從客戶端傳遞到 xml web services。
<%@ page language="c#" %>
<asp:label id="returnvalue" runat="server" />
<script runat=server language=c#>
void page_load(object o, eventargs e)
{
myheader mysoapheader = new myheader();
// populate the values of the soap header.
mysoapheader.username = "username";
mysoapheader.password = "password";
// create a new instance of the proxy class.
mywebservice proxy = new mywebservice();
// add the myheader soap header to the soap request.
proxy.myheadervalue = mysoapheader;
// call the method on the proxy class that communicates with
// your xml web service method.
string results = proxy.mywebmethod();
// display the results of the method in a label.
returnvalue.text = results;
}
</script>
新聞熱點
疑難解答