VB.NET 和 Java Web 服务 - 添加标头元素并不适用给出预期结果
我对这个主题标题中描述的问题感到非常头痛。 我不太擅长 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要弄乱生成的代理类,而是扩展此类并提供您自己的实现,在您的情况下覆盖
GetWriterForMessage
并将所需的标头添加到消息中,例如以下代码只是为您提供想法如何安装部件:希望有帮助!
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:Hope it helps!