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.
Don’t mess with the generated proxy class but instead extend this class and give your own implementation, in your case override
GetWriterForMessageand 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!