我目前正在使用 VB.net 处理 SOAP 请求。 我在这里找到了代码WebService Send SOAP request and receive response using Visual Basic,我根据需要进行了修改。
使用 Postman 我已经能够与该网站建立连接(所以我知道它有效)。 但是,当我创建此代码时,我只是收到“服务器无法识别 HTTP 标头 SOAPAction 的值”错误。
这是我的代码:
Dim Request As WebRequest
Dim Response As WebResponse
Dim DataStream As Stream
Dim Reader As StreamReader
Dim SoapByte() As Byte
Dim SoapStr As String
Dim pSuccess As Boolean = True
'TODO: Change Header
SoapStr = "<?xml version=""1.0"" encoding=""utf-8""?>"
SoapStr = SoapStr & "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
SoapStr = SoapStr & "<soap:Header/>"
SoapStr = SoapStr & "<soap:Body>"
SoapStr = SoapStr & "<ACORD>"
'I have the rest of the body in here
SoapStr = SoapStr & "</ACORD>"
SoapStr = SoapStr & "</soap:Body>"
SoapStr = SoapStr & "</soap:Envelope>"
Try
SoapByte = System.Text.Encoding.UTF8.GetBytes(SoapStr)
Request = WebRequest.Create("https://claimsearchgwa.iso.com/xmlsoap")
Request.Headers.Add("SOAPAction", "https://public-ws-stage.dpd.com/services/LoginService/V2_0/getAuth")
Request.ContentType = "application/x-www-form-urlencoded"
'Request.ContentLength = SoapByte.Length
Request.Method = "POST"
DataStream = Request.GetRequestStream()
DataStream.Write(SoapByte, 0, SoapByte.Length)
DataStream.Close()
Response = Request.GetResponse()
DataStream = Response.GetResponseStream()
Reader = New StreamReader(DataStream)
Dim SD2Request As String = Reader.ReadToEnd()
DataStream.Close()
Reader.Close()
Response.Close()
MsgBox(SD2Request)
Catch ex As WebException
MsgBox(ex.ToString())
End Try
我错过了什么? VB.net 和 SOAP 的资源并不多。 我会使用 C#,但我的公司使用 VB.net。
我使用 PostMan 创建的 C# 代码并将其转换为 Vb。 这就是我在修复了一些语法错误后所得到的。
Public Async Function SendSoapRequest() As Task(Of HttpWebRequestElement)
Dim client = New HttpClient()
Dim request = New HttpRequestMessage(HttpMethod.Post, "https://claimsearchgwa.iso.com/xmlsoap")
request.Headers.Add("Cookie", "[CookieHere])
Dim content = New StringContent("[ContentHere]")
request.Content = content
Dim response = Await client.SendAsync(request)
response.EnsureSuccessStatusCode().ToString()
'Console.WriteLine(Await .Content._)
Return response
End Function
使用这个新代码我收到此错误: 》严重性代码描述项目文件行抑制状态 错误(活动)BC30652 需要引用包含类型“ConfigurationElement”的程序集“System.Configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”。添加一个到您的项目中。 GNYLINY 索赔 FUPWINS 文件更新 jna3\sys\MIS\Projects\VisualStudio 项目\GNYLINYClaimsFUPWINSFileUpdate\GNYLINYClaimsFUPWINSFileUpdate\Module1.vb 426“
您需要添加对 System.Configuration 程序集的引用。具体方法如下:
在解决方案资源管理器中右键单击您的项目。 选择“添加”>“参考...” 在参考管理器中,搜索
System.Configuration
。
选中 System.Configuration
旁边的框,然后单击“确定”。
这应该可以解决 BC30652 错误。
为您简化代码
Imports System.Net.Http
Imports System.Text
Imports System.Threading.Tasks
Public Async Function SendSoapRequest() As Task(Of String)
Dim client = New HttpClient()
Dim request = New HttpRequestMessage(HttpMethod.Post, "https://claimsearchgwa.iso.com/xmlsoap")
request.Headers.Add("SOAPAction", "https://public-ws-stage.dpd.com/services/LoginService/V2_0/getAuth")
request.Headers.Add("Cookie", "[YourCookieHere]")
Dim soapEnvelope = "<?xml version=""1.0"" encoding=""utf-8""?>" &
"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" &
"<soap:Body>" &
"<ACORD>Your SOAP Body Here</ACORD>" &
"</soap:Body>" &
"</soap:Envelope>"
Dim content = New StringContent(soapEnvelope, Encoding.UTF8, "text/xml")
request.Content = content
Dim response = Await client.SendAsync(request)
response.EnsureSuccessStatusCode()
Dim responseContent = Await response.Content.ReadAsStringAsync()
Return responseContent
End Function