最近碰到的一個問題,需要在asp和客戶端調用.net的webservice,也就是說需要用vbscript或javascript來調用webservice。在網上看了看,大多數方案都是利用soap toolkit,但是因為soap toolkit在今年就會被停止后續的支持了,并且要使用soapclient需要專門安裝soap toolkit,這對客戶端來說不具有通用性,因此想到了使用xmlhttp,利用xmlhttp來和webservice交互。
客戶端代碼如下:
<script language="vbscript">
set objhttp = createobject("msxml2.xmlhttp")
set xmldoc =createobject("msxml.domdocument")
strwebserviceurl = "http://localhost/possible/service1.asmx/add"
'設置參數及其值
strrequest = "x=2&y=3"
objhttp.open "post", strwebserviceurl, false
'設置這個content-type很重要
objhttp.setrequestheader "content-type", "application/x-www-form-urlencoded"
objhttp.send(strrequest)
bok = xmldoc.load(objhttp.responsexml)
'看看狀態值
msgbox objhttp.status
msgbox objhttp.statustext
'objhttp.status=200,這里就可以處理返回的xml片段了
'如果需要,可以替換返回的xml字符串當中的<和>
xmlstr = xmldoc.xml
xmlstr = replace(xmlstr,"<","<",1,-1,1)
xmlstr = replace(xmlstr,">",">",1,-1,1)
msgbox xmlstr
</script>
改為服務器端的asp代碼為:
<%
set objhttp = server.createobject("msxml2.xmlhttp")
set xmldoc =server.createobject("msxml.domdocument")
strwebserviceurl = "http://localhost/possible/service1.asmx/add"
'設置參數及其值
strrequest = "x=2&y=3"
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/possible/service1.asmx/add"改為部署在其他機器上的webservice時,卻出了問題,結果一直是返回500錯誤,即objhttp.status一直都為500。
原因在于.net framework1.1默認不支持httpget和httppost。如果修改webservice里的web.config增加
<webservices>
<protocols>
<add name="httppost"/>
<add name="httpget"/>
</protocols>
</webservices>
后,上代碼就可以調用遠程機器上的webservice了。
而利用soap發送在默認情況下即可得到.net framework1.1的支持,因此用構造soap請求的xml字符串給xmlhttp對象來send的方法就對遠程服務器的web.config沒有要求了,于是根據local顯示的例子構造了一個soaprequest的string,發送給了即將部署的遠程主機,結果返回了200的status code,并且可以順利取得responsexml.類似代碼如下:
客戶端代碼如下:
<script language="vbscript">
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://localhost"&chr(34)&">"& _
"<x>3</x>"& _
"<y>4</y>"& _
"</add>"& _
"</soap:body>"& _
"</soap:envelope>"
url = "http://www.xxxx.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://localhost/add"
xmlhttp.setrequestheader "content-length",len(soaprequest)
xmlhttp.send(xmldoc)
msgbox xmlhttp.status
msgbox xmlhttp.statustext
msgbox xmlhttp.responsetext
if xmlhttp.status = 200 then
xmldoc.load(xmlhttp.responsexml)
msgbox "執行結果為:"&xmldoc.getelementsbytagname("addresult")(0).text
else
msgbox "failed"
end if
</script>
改為服務器端的asp代碼為:
<%
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://localhost"&chr(34)&">"& _
"<x>3</x>"& _
"<y>4</y>"& _
"</add>"& _
"</soap:body>"& _
"</soap:envelope>"
url = "http://www.xxxx.com/service1.asmx?methodname=add"
set xmldoc =server.createobject("msxml.domdocument")
xmldoc.loadxml(soaprequest)
set xmlhttp = server.createobject("msxml2.xmlhttp")
xmlhttp.open "post",url,false
xmlhttp.setrequestheader "content-type", "text/xml;charset=utf-8"
xmlhttp.setrequestheader "soapaction", "http://localhost/add"
xmlhttp.setrequestheader "content-length",len(soaprequest)
xmlhttp.send(xmldoc)
if xmlhttp.status = 200 then
xmldoc.load(xmlhttp.responsexml)
response.write xmlhttp.status&"<br>"
response.write xmlhttp.statustext&"<br>執行結果為:"
response.write xmldoc.getelementsbytagname("addresult")(0).text
else
response.write xmlhttp.status&"<br>"
response.write xmlhttp.statustext
end if
%>
以上用的都是vbscript的,對于javascript基本上都是一樣的,只需要做一些小的改動,具體代碼這里就省略了。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
附:
測試時用的webservice文件service1.asmx的代碼:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.diagnostics;
using system.web;
using system.web.services;
namespace possible
{
/// <summary>
/// service1 的摘要說明。
/// </summary>
[webservice(description="my web service",name="myservice",namespace="http://localhost")]
public class myservice : system.web.services.webservice
{
public myservice()
{
//codegen: 該調用是 asp.net web 服務設計器所必需的
initializecomponent();
}
#region 組件設計器生成的代碼
//web 服務設計器所必需的
private icontainer components = null;
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if(disposing && components != null)
{
components.dispose();
}
base.dispose(disposing);
}
#endregion
[webmethod(description="返回兩整數之和")]
public int add(int x,int y)
{
return x+y;
}
}
}