【发布时间】:2015-04-11 14:21:45
【问题描述】:
我正在尝试通过 POST 方法调用网络服务。输入 xml 是 rpc/literal soap 消息。
Dim myRequest As Net.HttpWebRequest
Dim objResponse As Net.HttpWebResponse
Dim result As String
Dim data As Byte()
Dim newStream As System.IO.Stream
Dim strURL As String = "https://abxxxx"
strXml.Load("D:\MyXml.xml")
data = System.Text.Encoding.ASCII.GetBytes(strXml.InnerXml)
myRequest = WebRequest.Create(strURL)
myRequest.Method = "POST"
myRequest.ContentType = "application/x-www-form-urlencoded"
myRequest.ContentLength = data.Length
myRequest.Timeout = 125000
newStream = myRequest.GetRequestStream()
newStream.Write(data, 0, data.Length)
newStream.Close()
objResponse = myRequest.GetResponse()
Dim sr As IO.StreamReader = New IO.StreamReader(objResponse.GetResponseStream())
strOutput = sr.ReadToEnd()
'Close connections
sr.Close()
objResponse.Close()
代码在objResponse = myRequest.GetResponse()行上抛出“远程服务器返回错误:500”异常
这是来自网络服务的实际soap响应:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: unexpected element (uri:"", local:"CONTACT"). Expected elements are (none) </faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
当我使用 Visual Studio(代理类)添加对此 Web 服务的引用时,这工作得很好,但我不需要使用此方法。
如果我遗漏了什么,请提出建议!
【问题讨论】:
-
不要使用 WebRequest/WebResponse。硒“How to Consume a Web Service”
-
我正在使用 .NET 2.0,需要从服务器端代码调用此 Web 服务。
-
所以使用“添加 Web 引用”。另外,.NET 2.0 是什么?十岁?您不得有任何竞争对手。
-
我已经尝试过网络参考,这很有效,但我不必使用网络参考,因为我的项目有一些用于此任务的通用组件。代码已经写在那里了。
-
您收到的异常意味着服务器不喜欢您发送的内容。根据它发回的错误,它不喜欢你的
<CONTACT>元素。
标签: web-services soap soap-client