我必须调用一些在登录时使用 ID 和密码提供令牌的服务。令牌不在标头的属性中。请求和响应的类型是自定义的。从登录响应中获取令牌后,我编写此代码以在请求标头中传递令牌:
Public Sub New(token As String)
MyBase.New
Me.ttoken = token
Me.Url = Global.WILLAGENT2_2_2.My.MySettings.Default.WILLAGENT2_2_2_ana_AnagrafeService
If (Me.IsLocalFileSystemWebService(Me.Url) = True) Then
Me.UseDefaultCredentials = True
Me.useDefaultCredentialsSetExplicitly = False
Else
Me.useDefaultCredentialsSetExplicitly = True
End If
End Sub
Protected Overrides Function GetWebRequest(ByVal uri As Uri) As System.Net.WebRequest
Dim request As HttpWebRequest = CType(WebRequest.Create(uri), HttpWebRequest)
'Dim urzi As New Uri("Https://us.creditonet.it:443/xxxxx")
'Dim request As System.Net.WebRequest = mybase.GetWebRequest(uri) 'gil
request.Headers.Add("SecurityToken", ttoken)
' request.Headers.Add("SecurityToken", ttoken)
Dim soapEnvelopeXml As New XmlDocument
Dim stream As Stream = request.GetRequestStream()
soapEnvelopeXml.Save(stream)
Dim response = request.GetRequestStream()
Return request
End Function
但是代码失败了:
Dim request As HttpWebRequest = CType(WebRequest.Create(uri), HttpWebRequest)
调用代码是:
Dim token As String
Dim dt As New LoginRequestDTO
dt.Login = Me.TxtId.Text.Trim
dt.Password = Me.TxtPwd.Text.Trim
token = service.Login(dt).SecurityToken.ToString
Dim ws2 As New ana.AnagrafeService(token)
Dim ws2request As New ana.RicercaCertificatiRequestDTO
Dim ws2result As ana.RicercaCertificatiDTO()
ws2request.CODICE_FISCALE = "DREDMN74E01A509B"
ws2request.CODICE_RITENUTA = "X45"
ws2request.ID_UTENTE = 33
ws2result = ws2.RicercaCertificati(ws2request)
其中 RicercaCertificatiDTO 是复杂类型。 谢谢
在覆盖中以不同的方式编写 GetWebRequest 响应失败。错误说:
无法将数据集转换为数据表
我希望请求能够工作并且响应返回预期类型的对象