|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
ASP脚本是采用明文(plain text)方式来编写的。vbscript|web|web办事|剧本 比来碰着的一个成绩,需求在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 = WordStr(xmlStr,"<","<",1,-1,1)
xmlStr = WordStr(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 = WordStr(xmlStr,"<","<",1,-1,1)
xmlStr = WordStr(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;
}
}
}
源代码保护方面其实现在考虑得没那么多了..NET也可以反编译.ASP写得复杂的话别人能看得懂的话.他也有能力自己写了.这方面担心的倒不太多. 纵观现在网上可以下载的那些所谓BBS还有什么网站等等的源代码 |
|