如何使用HttpWebRequest

时间:2019-05-31 13:35:13

标签: vb.net soap httpwebrequest

当尝试发送以下WSDL时,我收到“(415)不支持的媒体类型”:

Dim xml1 As String = String.Empty
xml1 = "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" "
xml1 += "xmlns:ser=""http://client_server.com/"">"
xml1 += "<soapenv:Header/>"
xml1 += "<soapenv:Body>"
xml1 += "XML_CONTENT"
xml1 += "</soapenv:Body>"
xml1 += "</soapenv:Envelope>"
'----------------------------
Dim a As New System.Text.ASCIIEncoding()
Dim buffer As Byte() = a.GetBytes(xml1)
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://server.com?wsdl"), HttpWebRequest)
request.ServicePoint.Expect100Continue = False
request.KeepAlive = False
request.Headers.Add("SOAPAction", "")
request.Method = "POST"
request.ContentType = "application/soap+xml; charset=""UTF-8"""
request.Accept = "text/xml"
Dim stm As Stream = request.GetRequestStream()
stm.Write(buffer, 0, buffer.Length)
stm.Close()
Dim resp As WebResponse = request.GetResponse()
stm = resp.GetResponseStream()

据我了解,发生异常的原因是因为服务器使用的SOAP版本(我猜是“ 1.2”)与我请求的版本不同。 如何设置SOAP版本? 还是有其他方法可以解决此“不受支持的媒体类型”错误?

1 个答案:

答案 0 :(得分:0)

通过删除填充“ ContentType”属性的行(“ request.ContentType =” application / soap + xml; charset = UTF-8“”)来工作