VB.NET 和 Java Web 服务 - 添加标头元素并不适用给出预期结果

发布于 2024-12-28 23:58:39 字数 3371 浏览 0 评论 0原文

我对这个主题标题中描述的问题感到非常头痛。 我不太擅长 Web 服务,而且我正在处理一个用 Java 内部开发的服务。 此 Web 服务需要一个名为 Token 的 Header 元素以及一个值(加密字符串)。 我已经能够添加此“令牌”,但该服务不接受它。

这是我的代理调用生成的请求 xml(使用 WSDL.EXE 构建的代理类并修改为接受我编写的用于向请求添加标头的 SoapHeader 外部类):

<soap:Envelope>
    <soap:Header>
        <TokenHeader soap:actor="http://schemas.xmlsoap.org/soap/actor/next">
            <Token>[TOKEN VALUE]</Token>
        </TokenHeader>
    </soap:Header>
    <soap:Body>
    </soap:Body>
</soap:Envelope>

这就是 Web 服务想要的(这是由一个java tyest客户端):

<soapenv:Envelope>
    <soapenv:Header>
        <ns1:Token soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0">
            [TOKEN VALUE]
        </ns1:Token></soapenv:Header>
    <soapenv:Body>
    </soapenv:Body>
</soapenv:Envelope>

这是我写的代码:

Imports System.Web.Services
Imports System.Web.Services.Protocols

<System.Xml.Serialization.XmlRoot(Namespace:=[WSDL NAMESPACE])> _
Public Class TokenHeader
    Inherits SoapHeader

    <System.Xml.Serialization.XmlElement("Token", Namespace:=[WSDL NAMESPACE])> _
    Public Token As String

    Public Sub New()
        MyBase.New()
        Me.Actor = "http://schemas.xmlsoap.org/soap/actor/next"
    End Sub
End Class    

这是我声明令牌的代理类的代码段

<System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Bare)> _
<WebMethod(), SoapHeaderAttribute("TokenHeader", Direction:=SoapHeaderDirection.In)> _
Public Function ChiamaEdicola(<System.Xml.Serialization.XmlElementAttribute([Namespace]:=[WSDL NAMESPACE])> ByVal ChiamaEdicolaRequest As ChiamaEdicolaRequest) As <System.Xml.Serialization.XmlElementAttribute("ChiamaEdicolaResponse", [Namespace]:=[WSDL NAMESPACE])> ChiamaEdicolaResponse
    Try
        Dim results() As Object = Me.Invoke("ChiamaEdicola", New Object() {ChiamaEdicolaRequest})
        Return CType(results(0), ChiamaEdicolaResponse)
    Catch ex As Exception
        MsgBox("Errore: " & vbCrLf & ex.Message)
        Return Nothing
    End Try
End Function

最后是我调用这个函数的代码段:

Private Sub SendRivData(ByVal IRWebSrvData As ChiamaEdicola)
Dim IRWebSrvSend As New ChiamaEdicolaRequest
Dim IRWebSrvRecv As ChiamaEdicolaResponse
Dim IRWebSrvClient As New EdicolaService
Dim TokenHeader As New IR2IG.TokenHeader

    IRWebSrvSend.ChiamaEdicola = IRWebSrvData

    TokenHeader.Token = "[TOKEN VALUE]"
    IRWebSrvClient.TokenHeader = TokenHeader

    IRWebSrvRecv = IRWebSrvClient.ChiamaEdicola(IRWebSrvSend)

    If IRWebSrvRecv Is Nothing Then 
        MsgBox("Errore durante l'elaborazione!", MsgBoxStyle.Exclamation)
        Exit Sub
    End If

    With IRWebSrvRecv
        If CInt(.Stato) <> 0 Then
            MsgBox("Elaborazione OK:" & vbCrLf & .Messaggio, MsgBoxStyle.Information)
        Else
            MsgBox("Errore durante l'elaborazione:" & vbCrLf & .Messaggio, MsgBoxStyle.Exclamation)
        End If
    End With
End Sub

这就是全部(我不得不省略零件很少,因为我的老板不希望我公开它们)。

如果有人知道如何实现这一点,请帮助我。 我花了大约4天的时间来完成它。

I'm having a lot of headache with the problem described in this topic title.
I'm not very good with web services and I'm dealing with one developed internally in Java.
This web service requires a Header element named Token with a value (an encrypted string).
I've been able to add this "Token" but the service doesn't accept it.

This is the request xml my proxy calls generates (proxy class built with WSDL.EXE and modified to accept a SoapHeader external class I wrote to add Header to request):

<soap:Envelope>
    <soap:Header>
        <TokenHeader soap:actor="http://schemas.xmlsoap.org/soap/actor/next">
            <Token>[TOKEN VALUE]</Token>
        </TokenHeader>
    </soap:Header>
    <soap:Body>
    </soap:Body>
</soap:Envelope>

and this is what the web services wants (this is a xml request made by a java tyest client):

<soapenv:Envelope>
    <soapenv:Header>
        <ns1:Token soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0">
            [TOKEN VALUE]
        </ns1:Token></soapenv:Header>
    <soapenv:Body>
    </soapenv:Body>
</soapenv:Envelope>

This is the code I wrote:

Imports System.Web.Services
Imports System.Web.Services.Protocols

<System.Xml.Serialization.XmlRoot(Namespace:=[WSDL NAMESPACE])> _
Public Class TokenHeader
    Inherits SoapHeader

    <System.Xml.Serialization.XmlElement("Token", Namespace:=[WSDL NAMESPACE])> _
    Public Token As String

    Public Sub New()
        MyBase.New()
        Me.Actor = "http://schemas.xmlsoap.org/soap/actor/next"
    End Sub
End Class    

and this is the piece of code of the proxy class where I've declared the Token

<System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Bare)> _
<WebMethod(), SoapHeaderAttribute("TokenHeader", Direction:=SoapHeaderDirection.In)> _
Public Function ChiamaEdicola(<System.Xml.Serialization.XmlElementAttribute([Namespace]:=[WSDL NAMESPACE])> ByVal ChiamaEdicolaRequest As ChiamaEdicolaRequest) As <System.Xml.Serialization.XmlElementAttribute("ChiamaEdicolaResponse", [Namespace]:=[WSDL NAMESPACE])> ChiamaEdicolaResponse
    Try
        Dim results() As Object = Me.Invoke("ChiamaEdicola", New Object() {ChiamaEdicolaRequest})
        Return CType(results(0), ChiamaEdicolaResponse)
    Catch ex As Exception
        MsgBox("Errore: " & vbCrLf & ex.Message)
        Return Nothing
    End Try
End Function

Finally the piece of code where I call this function:

Private Sub SendRivData(ByVal IRWebSrvData As ChiamaEdicola)
Dim IRWebSrvSend As New ChiamaEdicolaRequest
Dim IRWebSrvRecv As ChiamaEdicolaResponse
Dim IRWebSrvClient As New EdicolaService
Dim TokenHeader As New IR2IG.TokenHeader

    IRWebSrvSend.ChiamaEdicola = IRWebSrvData

    TokenHeader.Token = "[TOKEN VALUE]"
    IRWebSrvClient.TokenHeader = TokenHeader

    IRWebSrvRecv = IRWebSrvClient.ChiamaEdicola(IRWebSrvSend)

    If IRWebSrvRecv Is Nothing Then 
        MsgBox("Errore durante l'elaborazione!", MsgBoxStyle.Exclamation)
        Exit Sub
    End If

    With IRWebSrvRecv
        If CInt(.Stato) <> 0 Then
            MsgBox("Elaborazione OK:" & vbCrLf & .Messaggio, MsgBoxStyle.Information)
        Else
            MsgBox("Errore durante l'elaborazione:" & vbCrLf & .Messaggio, MsgBoxStyle.Exclamation)
        End If
    End With
End Sub

This is all (I had to omit few parts because my boss doesn't want me to make them public).

If someone knows how to accomplish this, please, help me.
It's about 4 days I'm working on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2025-01-04 23:58:39

不要弄乱生成的代理类,而是扩展此类并提供您自己的实现,在您的情况下覆盖 GetWriterForMessage 并将所需的标头添加到消息中,例如以下代码只是为您提供想法如何安装部件:

Public Class ExtendedWebService
     Inherits myWebService

     Protected Overrides Function GetWriterForMessage(message As SoapClientMessage, bufferSize As Integer) As System.Xml.XmlWriter

         message.Headers.Add(YourCustomHeader)

         Return MyBase.GetWriterForMessage(message, bufferSize)
     End Function

End Class

希望有帮助!

Don't mess with the generated proxy class but instead extend this class and give your own implementation, in your case override GetWriterForMessage and add your required header to the message e.g. The following code is just to give you the idea how to fit the pieces:

Public Class ExtendedWebService
     Inherits myWebService

     Protected Overrides Function GetWriterForMessage(message As SoapClientMessage, bufferSize As Integer) As System.Xml.XmlWriter

         message.Headers.Add(YourCustomHeader)

         Return MyBase.GetWriterForMessage(message, bufferSize)
     End Function

End Class

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文