2 回答

TA貢獻1773條經驗 獲得超3個贊
如果要用ASP來調用WevService,就一定要使用SOAP Toolkit或者XMLHTTP, 使用SOAP Client需要專門安裝SOAP Toolkit,這對客戶端來說不具有通用性,因此我們就學習使用XML來進行對WebService的調用。
<%
Set objHTTP = Server.CreateObject( "MSXML2.XMLHTTP ")
Set xmlDOC =Server.CreateObject( "MSXML.DOMDocument ")
strWebserviceURL = "http://localhost/WebService1/Service1.asmx/Add "
'設置參數和值
strRequest = "a=5&b=6 "
objHTTP.Open "POST ", strWebserviceURL, False
'設置Content-Type很重要
objHTTP.SetRequestHeader "Content-Type ", "application/x-www-form-urlencoded "
objHTTP.Send(strRequest)
bOK = xmlDOC.load(objHTTP.responseXML)
'查看狀態值
if objHTTP.Status=200 then
xmlStr = xmlDOC.xml
xmlStr = Replace(xmlStr, "< ", " < ",1,-1,1)
xmlStr = Replace(xmlStr, "> ", "> ",1,-1,1)
Response.Write xmlStr
else
Response.Write objHTTP.Statu& " <br> "
Response.Write objHTTP.StatusText
end if
%>
以上代碼在本地測試都沒有問題(在部署webservice的本地機器上測試的),然而把strWebserviceURL = "http://localhost/WebService1/Service1.asmx/Add "改為部署在其他機器上的WebService時,卻出了問題,結果一直是返回500錯誤,即objHTTP.Status一直都為500。
原因在于.Net Framework1.1默認不支持HttpGet和HttpPost。如果修改webservice里的web.config增加上代碼5后,上代碼就可以調用遠程機器上的WebService了。
<webServices>
<protocols>
<add name= "HttpPost "/>
<add name= "HttpGet "/>
</protocols>
</webServices>
- 2 回答
- 0 關注
- 767 瀏覽
添加回答
舉報