共用方式為


HOW TO:建置可處理 SOAP 標頭的用戶端

本主題專門說明舊有技術。 應該使用下列建立 XML Web Service 及 XML Web Service 用戶端: Windows Communication Foundation.

程式碼範例

Web 服務用戶端與 Web 服務通訊時,會傳送及接收 SOAP 標頭。針對預期或傳回 SOAP 標頭的 Web 服務,使用 Wsdl.exe 公用程式產生 Proxy 類別時,Proxy 類別會包含 SOAP 標頭的相關資訊。特別是 Proxy 類別有成員變數表示與 Web 服務中相關聯的 SOAP 標頭。Proxy 類別也有表示 SOAP 標頭之對應類別的定義。例如,針對先前的 Web 服務產生的 Proxy 類別會有 MyHeader 型別的成員變數和 MyHeader 類別的定義。如需建立 Proxy 類別的詳細資訊,請參閱建立 XML Web Service Proxy

注意:如果 Web 服務定義的成員變數表示 SoapHeaderSoapUnknownHeader 型別 (而非衍生自 SoapHeader 的類別) 的 SOAP 標頭,則 Proxy 類別不會有該 SOAP 標頭的任何資訊。

若要在 Web 服務用戶端中處理 SOAP 標頭

  1. 建立表示 SOAP 標頭之類別的新執行個體。

    Dim mySoapHeader As MyHeader = New MyHeader()
    
    MyHeader mySoapHeader = new MyHeader();
    
  2. 填入 SOAP 標頭的值。

    mySoapHeader.Username = UserName
    mySoapHeader.Password = SecurelyStoredPassword
    
    mySoapHeader.Username = Username;
    mySoapHeader.Password = SecurelyStoredPassword
    
  3. 建立 Proxy 類別的新執行個體。

    Dim proxy As MyWebService = New MyWebService()
    
    MyWebService proxy = new MyWebService();
    
  4. 將 SOAP 標頭物件指派至 Proxy 類別中表示 SOAP 標頭的成員變數。

    proxy.MyHeaderValue = mySoapHeader
    
    proxy.MyHeaderValue = mySoapHeader
    
  5. 在 Proxy 類別上呼叫與 Web 服務方法通訊的方法。

    傳送至 Web 服務的 SOAP 要求的 SOAP 標頭部分,會包含 SOAP 標頭物件中儲存的資料內容。

    Dim results as String = proxy.MyWebMethod()
    
    string results = proxy.MyWebMethod();
    

範例

下列程式碼範例示範如何將 SOAP 標頭從用戶端傳遞至 Web 服務。

<%@ Page Language="VB" %>

<asp:Label id="ReturnValue" runat="server" />
<script runat=server language=VB>

 Sub Page_Load(o As Object, e As EventArgs)

  Dim mySoapHeader As MyHeader = New MyHeader()  ' Populate the values of the SOAP header.  mySoapHeader.Username = UserName  mySoapHeader.Password = SecurelyStoredPassword

  ' Create a new instance of the proxy class.
  Dim proxy As MyWebService = New MyWebService()
  
  ' Add the MyHeader SOAP header to the SOAP request.  proxy.MyHeaderValue = mySoapHeader

  ' Call the method on proxy class that communicates with
  ' your Web service method.
  Dim results as String = proxy.MyWebMethod()

  ' Display the results of the method in a label.
  ReturnValue.Text = results

 End Sub
</script>
<%@ 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 = SecurelyStoredPassword;

  // 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 Web service method.
  string results = proxy.MyWebMethod();

  // Display the results of the method in a label.
  ReturnValue.Text = results;
 }
</script>

另請參閱

參考

SoapHeader
SoapUnknownHeader
SoapHeaderException

概念

建置 XML Web Service 用戶端

其他資源

使用 SOAP 標頭
使用 ASP.NET 的 XML Web Service