电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> .NET
使用vbscript脚本调用web服务-.NET教程,Web Service开发
作者:网友供稿 点击:40
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
最近碰到的一个问题,需要在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字符串当中的&lt;和&gt;
xmlstr = xmldoc.xml
xmlstr = replace(xmlstr,"&lt;","<",1,-1,1)
xmlstr = replace(xmlstr,"&gt;",">",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,"&lt;","<",1,-1,1)
xmlstr = replace(xmlstr,"&gt;",">",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;
}
}
}


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·经典收藏之 - C++内存管理详解-.NET教程,C#语言
·Master Page 初探-.NET教程,评论及其它
·GDI+编程10个基本技巧-.NET教程,评论及其它
·VB.NET中让Textbox只能输入数字(二)-.NET教程,VB.Net语言
·stl应用小问题-.NET教程,评论及其它
·WIN32中颜色值(COLORREF)与.NET中颜色值(Color)的转换-ASP教程,系统相关
·打造自己的专业图像工具-Visual C++ 2005图像编程系列【三】-.NET教程,C#语言
·.Net中常见问题及解决方法归类-.NET教程,.NET Framework
·Lex和Yacc从入门到精通(3)--一个极其简单的lex和yacc程序-.NET教程,评论及其它
·VB下几个非常有用的函数-.NET教程,VB.Net语言

最新文章
·VC#初学入门:第一个Windows程序
·ASP.NET 2.0-选用DataSet或DataReader
·用.net 处理xmlHttp发送异步请求
·asp.net创建文件夹的IO类的问题
·asp.net 2.0 中加密web.config 文件中的配置节
·关于ASP.NET调用JavaScript的实现
·如何实现ASP.NET网站个性化
·Acegi安全系统的配置-.NET教程,评论及其它
·Spring安全系统:Acegi Security Acegi简介-.NET教程,评论及其它
·Biztalk 开发之 架构和实例的验证-.NET教程,评论及其它




版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号