如何向VB.NET 2008 SOAP请求添加标头?

时间:2012-10-22 14:42:53

标签: vb.net wcf web-services soap siebel

我有一个VB.NET 2008程序,它访问由WSDL定义并使用SOAP协议的Siebel Web服务。

Siebel Web服务要求包含用户名,密码和会话类型的标头包含在服务请求中,但标头未在WSDL中定义。

因此,当我使用soapUI实用程序测试WSDL时,WSDL定义的请求如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lov="http://www.siebel.com/xml/LOVService" xmlns:lis="http://www.siebel.com/xml/ListQuery">
<soapenv:Header/>
   <soapenv:Body>
      <lov:EAILOVGetListOfValues_Input>
         <lis:ListsQuery>
            <lis:ListQuery>
               <lis:Active>Y</lis:Active>
               <lis:LanguageCode>ENU</lis:LanguageCode>
               <lis:Type>CUT_ACCOUNT_TYPE</lis:Type>
            </lis:ListQuery>
         </lis:ListsQuery>
      </lov:EAILOVGetListOfValues_Input>
   </soapenv:Body>
</soapenv:Envelope>

但上述方法不起作用,因为它包含缺少用户和会话凭据的空标头。只有当我用包含用户名,密码和会话类型的标头手动替换<soapenv:Header/>时,它才有效:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lov="http://www.siebel.com/xml/LOVService" xmlns:lis="http://www.siebel.com/xml/ListQuery">
<soapenv:Header>
  <UsernameToken xmlns="http://siebel.com/webservices">TESTUSER</UsernameToken>
  <PasswordText xmlns="http://siebel.com/webservices">TESTPASSWORD</PasswordText>
  <SessionType xmlns="http://siebel.com/webservices">None</SessionType>
</soapenv:Header>
   <soapenv:Body>
      <lov:EAILOVGetListOfValues_Input>
         <lis:ListsQuery>
            <lis:ListQuery>
               <lis:Active>Y</lis:Active>
               <lis:LanguageCode>ENU</lis:LanguageCode>
               <lis:Type>CUT_ACCOUNT_TYPE</lis:Type>
            </lis:ListQuery>
         </lis:ListsQuery>
      </lov:EAILOVGetListOfValues_Input>
   </soapenv:Body>
</soapenv:Envelope>

我的问题是我无法理解如何将上述内容翻译成VB.NET 2008代码。

将WSDL导入Visual Studio 2008,在VB代码中定义服务并引用Web服务方法没有问题。但是,我无法理清如何在VB中定义Web服务,以便更新的头包含在Web服务请求中而不是空头中。因此,我对VB的所有服务请求都失败了。

我可以定义一个继承自SoapHeader类的类......

Public Class MySoapHeader : Inherits System.Web.Services.Protocols.SoapHeader
    Public Username As String
    Public Password As String
    Public SessionType As String
End Class

...但是如何在由VB生成的SOAP请求中包含此标头?

我用来测试它的示例代码是一个带有按钮和列表框的简单表单。

Public Class Form1

    Private Sub btnGetLOV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetLOV.Click

        Dim MyService As New wsLOV.EAILOVPortClient

        Dim MyInput As New wsLOV.EAILOVGetListOfValues_Input
        Dim MyParams(0) As wsLOV.ListQuery
        Dim temp As New wsLOV.ListQuery

        Dim MyResult As New wsLOV.EAILOVGetListOfValues_Output

        temp.Active = "Y"
        temp.Type = "CUT_ACCOUNT_TYPE"
        temp.LanguageCode = "ENU"
        MyParams(0) = temp

        MyInput.ListsQuery = MyParams

        Dim MyRequest As New wsLOV.EAILOVGetListOfValuesRequest(MyInput)
        MyResult = MyService.EAILOVGetListOfValues(MyInput)


    End Sub

End Class

代码在子例程的最后一行失败,并显示一条消息,指示请求未经过身份验证(错误代码:10944642错误消息:错误:入站SOAP消息 - 会话令牌丢失或无效或已过期),这是当我离开包含用户名,密码和会话类型的标题时,我在soapUI中得到了同样的错误。

我相信我需要将标头添加到端点(每http://msdn.microsoft.com/en-us/library/ms731749.aspxhttp://msdn.microsoft.com/en-us/library/system.servicemodel.configuration.serviceendpointelement.aspx)但我不知道如何在VB中执行此操作。

1 个答案:

答案 0 :(得分:3)

以下是我们解决此问题的方法。密钥似乎是使用附加头实例化端点,而不是在端点已经实例化后尝试添加头。

Imports System.ServiceModel.Channels
Imports System.ServiceModel

Public Class Form1

    Private Sub btnGetOrg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetOrgType.Click
        ' This example code queries the Siebel web service using the SOAP protocol for a list of
        ' Account (organization) types stored in a List of Values (LOV). The service request
        ' requires that a SOAP header be added that contains the username, password, and session
        ' type. We have to add this header because the WSDL file definition generated by Siebel
        ' does not include the header definition. The WSDL file was added to the VS2008 project as
        ' a Service Reference named "wsGetLOV"

        ' Create address headers for special services and add them to an array
        Dim addressHeader1 As AddressHeader = AddressHeader.CreateAddressHeader("UsernameToken", "http://siebel.com/webservices", "TESTUSER")
        Dim addressHeader2 As AddressHeader = AddressHeader.CreateAddressHeader("PasswordText", "http://siebel.com/webservices", "TESTPASSWORD")
        Dim addressHeader3 As AddressHeader = AddressHeader.CreateAddressHeader("SessionType", "http://siebel.com/webservices", "None")
        Dim addressHeaders() As AddressHeader = {addressHeader1, addressHeader2, addressHeader3}

        ' Endpoint address constructor with URI and address headers
        ' Replace <servername> in the following line with the name of your Siebel server.
        ' For example: http://actual-server/eai_enu...
        Dim endpointAddressWithHeaders As New EndpointAddress(New Uri("http://<servername>/eai_enu/start.swe?SWEExtSource=WebService&SWEExtCmd=Execute&WSSOAP=1"), addressHeaders)
        Dim MyService As New wsGetLOV.EAILOVPortClient("EAILOVPort", endpointAddressWithHeaders)

        Dim MyInput As New wsGetLOV.EAILOVGetListOfValues_Input
        Dim MyOutput As wsGetLOV.EAILOVGetListOfValues_Output
        Dim MyList(0) As wsGetLOV.ListQuery

        MyList(0) = New wsGetLOV.ListQuery
        MyList(0).Active = "Y"
        MyList(0).LanguageCode = "ENU"
        MyList(0).Type = "CUT_ACCOUNT_TYPE"
        MyInput.ListsQuery = MyList

        MyOutput = MyService.EAILOVGetListOfValues(MyInput)

        Dim myStrings As New List(Of String)

        ' We use nested loops because the results returned by the service is a list of
        ' lists though in our case, the result set is a list with a single item (this item
        ' being a list of multiple items)
        For Each myResultList As wsGetLOV.ListResult In MyOutput.ListsResult
            For Each myResultValue As wsGetLOV.ListValueResult In myResultList.ListValuesResult
                myStrings.Add(myResultValue.Value)
            Next
        Next

        ListBox1.DataSource = myStrings

    End Sub
End Class