【IT168 技术文档】
我在本地测试的结果运行良好,但是使用其中将要部署的某个远程主机的结果却一直是返回500错误,百思不得其解,但是在测试local的时候SOAP的request和response事例突然让我有另外一种想法,构造Soap请求的xml字符串给xmlhttp对象来send,于是根据local显示的例子构造了一个soapRequest的string,发送给了即将部署的远程主机,结果终于返回了200的status code,并且可以顺利取得responseXML.类似代码如下:
Dim url,xmlhttp,dom,node,xmlDOC
'根据webservice的测试叶不同的方法构造不同的soap request
SoapRequest = "<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _
"<soap:Envelope xmlns:xsi="&CHR(34)&"http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _
"xmlns:xsd="&CHR(34)&"http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _
"xmlns:soap="&CHR(34)&"http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
"<soap:Body>"& _
"<Add xmlns="&CHR(34)&"http://tempuri.org/"&CHR(34)&">"& _
"<a>3</a>"& _
"<b>4</b>"& _
"</Add>"& _
"</soap:Body>"& _
"</soap:Envelope>"
'msgbox SoapRequest
url = "http://www.v-instru.com/Service1.asmx?methodname=Add"
Set xmlDOC =CreateObject("MSXML.DOMDocument")
xmlDOC.loadXML(SoapRequest)
Set xmlhttp = CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
'SOAPAction这个Header头同样可以在sample中找到
xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/Add"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.Send(xmlDOC)
msgbox xmlhttp.Status
msgbox xmlhttp.StatusText
msgbox xmlhttp.responseText
If xmlhttp.Status = 200 Then 'ok
xmlDOC.load(xmlhttp.responseXML)
msgbox xmlDOC.getElementsByTagName("AddResult")(0).text
else
msgbox "failed"
end if
后来我又测试了另外一个简单的远程主机的WebService(同样基于.Net Framework1.1),使用
http://v-instru.com/blog/posts/233.aspx的代码在没有任何修改配置的情况下,status为200,但是responseXML取不到任何值,可以由于.Net Framework1.1默认不支持HttpGet和HttpPost.如果修改web.config增加HttpPost和HttpGet的支持后
<webServices> <protocols> <add name="HttpPost"/> <add name="HttpGet"/> </protocols> </webServices>
原始代码可以运行顺利,而利用SOAP发送的情况,在默认情况下即可得到支持,因此都可以顺利取得返回值.
问题就在于我最初使用的代码调用local的webservice没有任何问题,而调用实际需要部署机器的时候却一直出错,(此时我利用C#直接调用远程WebService的情况下返回也是正常的),即便此远程主机没有正确配置对于HttpPost和HttpGet的支持,但是否也应该仅仅是取不到返回值呢,就像我上面测试的那样,status返回200,但是取不到responseXML的值,而不是直接返回500错误?或者说某些配置会影响这个需求呢?至今我还是找不到此问题所在. 我想说的是或许你在HttpPost和HttpGet都失败的并且找不到问题所在的时候不妨试试此方法:)
| 第1页: 使用Vbscript调用.NET WebService |