You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET发送SOAP请求遇SOAPAction错误及程序集引用问题求助

VB.NET SOAP请求问题排查与解决

问题1:Server did not recognize the value of HTTP Header SOAPAction

原因

  • SOAPAction值不匹配:代码中硬编码的SOAPAction是第三方服务地址(https://public-ws-stage.dpd.com/...),和目标ISO服务的要求完全不符。
  • ContentType错误:设置为application/x-www-form-urlencoded,这是表单提交的类型,SOAP请求需要使用XML对应的Content-Type。

解决办法

  1. 替换正确的SOAPAction:打开能正常请求的Postman会话,复制请求头里的SOAPAction字段值,替换代码中对应行的内容。
  2. 修正ContentType:将Request.ContentType = "application/x-www-form-urlencoded"改为Request.ContentType = "text/xml; charset=utf-8"(适用于SOAP 1.1),如果目标服务是SOAP 1.2则使用application/soap+xml; charset=utf-8。
  3. 优化请求体构建(可选):使用VB.NET的XML字面量构建SOAP请求,避免手动拼接字符串的转义问题:
Dim soapEnvelope As XElement = <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:Header/>
                                   <soap:Body>
                                       <ACORD>
                                           ' 填充实际请求内容
                                       </ACORD>
                                   </soap:Body>
                               </soap:Envelope>
Dim soapStr As String = soapEnvelope.ToString()

修改后的完整第一段代码示例:

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

' 使用XML字面量构建SOAP请求体
Dim soapEnvelope As XElement = <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:Header/>
                                   <soap:Body>
                                       <ACORD>
                                           ' 填充实际请求内容
                                       </ACORD>
                                   </soap:Body>
                               </soap:Envelope>
SoapStr = soapEnvelope.ToString()

Try
    SoapByte = System.Text.Encoding.UTF8.GetBytes(SoapStr)

    Request = WebRequest.Create("https://claimsearchgwa.iso.com/xmlsoap")
    ' 替换为Postman中获取的正确SOAPAction
    Request.Headers.Add("SOAPAction", "目标服务的SOAPAction地址")

    Request.ContentType = "text/xml; charset=utf-8"
    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

问题2:BC30652 缺少System.Configuration程序集引用

原因

  • 返回类型错误:函数声明为Task(Of HttpWebRequestElement),但实际返回的是HttpResponseMessage。HttpWebRequestElement属于System.Configuration程序集,代码中完全不需要这个类型,导致不必要的依赖。
  • 字符串语法错误:request.Headers.Add("Cookie", "[CookieHere])缺少闭合双引号。
  • 方法调用错误:response.EnsureSuccessStatusCode().ToString()中EnsureSuccessStatusCode是无返回值方法,不需要调用ToString()。

解决办法

  1. 修正返回类型:将函数返回类型改为Task(Of HttpResponseMessage),与实际返回的对象匹配。
  2. 修复字符串语法:补全Cookie值的闭合双引号。
  3. 调整方法调用:去掉EnsureSuccessStatusCode后的ToString()。
  4. 补全响应读取逻辑(可选):完善注释中的响应内容读取代码。

修改后的第二段代码示例:

Imports System.Net.Http
Imports System.Text

Public Async Function SendSoapRequest() As Task(Of HttpResponseMessage)
    Dim client = New HttpClient()
    Dim request = New HttpRequestMessage(HttpMethod.Post, "https://claimsearchgwa.iso.com/xmlsoap")
    
    ' 修正字符串闭合引号
    request.Headers.Add("Cookie", "[CookieHere]")
    
    ' 替换为实际SOAP请求内容,建议从Postman复制完整XML
    Dim content = New StringContent("[ContentHere]", Encoding.UTF8, "text/xml")
    request.Content = content
    
    Dim response = Await client.SendAsync(request)
    ' 直接调用,非成功状态码会抛出异常
    response.EnsureSuccessStatusCode()
    
    ' 可选:读取并输出响应内容
    ' Dim responseContent As String = Await response.Content.ReadAsStringAsync()
    ' Console.WriteLine(responseContent)
    
    Return response
End Function

内容的提问来源于stack exchange,提问作者djblois

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 09:12:34