所有的知识都不是最新的知识点,关键是达到自己的设计目的。
某些时候,我们可能需要客户端做点什么以简化复杂的脚本处理,比如加密之类的等。
不管用什么样的客户端,首先我们应该确定我们遵守的协议,或者说如何设置或获取我的数据。这里,我们的服务器端为iis5的asp程序,所以我们首先遵守的是http协议,然后在http协议上传输xml。用xml是因为更容易统一我们的数据格式,有xml这种思想我们为什么不用呢。
恰好xmlhttp,就是基于http传输xml的很方便的类。
首先简要说一下xmlhttp的两个函数:
open(bstrmethod, bstrurl, varasync, bstruser, bstrpassword)
按照bstrmethod指定的http方法(如post,get等)打开bstrurl指定的连接,其中varasync为是否异步,bstruser, bstrpassword为访问bstrurl时需要用户及密码。
send(varbody)
发送的数据,或者应该说是提交的数据。
具体可以查看相关的msxml sdk。
以下利用c#实现xml的传输,假定反馈的xml格式为"<response><error id=\"0\">成功</error></response>"
private msxml2.domdocument40class zxml; // 处理服务器反馈结果
private int zlasterrorcode;
private string zlasterrordescription;
private const int oksuccess=0;
//以err打头的都是一些常量
private int executecmd(string scmd, string method, string ext)
{
//根据scmd计算出surl的代码省略。
//……
try
{
//messagebox.show(surl);
zxmlhttp.open(method, surl, false,"","");
zxmlhttp.send(ext);
}
finally
{
// 检测设置最后的错误代码和描述
// 检查zxmlhttp的状态
if(zxmlhttp.status / 100 ==2) //200, 201, 202, 203, 204, 205, 206
{
// 成功
//messagebox.show(zxmlhttp.responsetext);
zxml.loadxml(zxmlhttp.responsetext);
if(zxml.parseerror.errorcode!=0)
{
zlasterrorcode = errprotocol;
//zlasterrordescription = "解析服务器反馈结果时出现错误,协议版本可能不正确";
zlasterrordescription = zxml.parseerror.reason; //本行应该仅作调试时使用,发布时应该使用上一行
// messagebox.show(zxmlhttp.responsetext);
}
else
{
s = getxmlnodevalue(zxml,"/response/error/@id");
if(s.length==0)
{
zlasterrorcode = oksuccess;
}
else
{
try
{
zlasterrorcode = convert.toint32(s);
}
catch
{
zlasterrorcode = errunknown;
}
}
zlasterrordescription = getxmlnodevalue(zxml, "/response/error");
if(zlasterrordescription.length==0)
{
if(zlasterrorcode==oksuccess)
{
zlasterrordescription = "执行成功";
}
else
{
zlasterrordescription = "未知错误信息";
}
}
}
}
else
{
// 服务器出错
zlasterrorcode = errservice;
zlasterrordescription = zxmlhttp.statustext;
}
}
return zlasterrorcode;
}
然后是服务器端的test.asp
<% language=vbscript %>
<%
dim oxml
set oxml = server.createobject("msxml2.domdocument.4.0")
oxml.async = false
oxml.resolveexternals = false
oxml.load request
然后对提交的xml数据进行处理
set oxml = nothing
最后是反馈处理结果
response.contenttype = "text/xml" 由于反馈的是xml不可缺!!
response.charset= "gb2312" 反馈的内容的编码,不可缺!!否则会出现非法字符不能被客户端解析response.write "<?xml version=""1.0"" encoding=""gb2312"" ?>" & vbcrlf
response.write "<response><version>1.0.0.0</version><test>中国人民万岁</test></response>" & vbcrlf
%>
最后要说的是传输的xml数据格式,可以说就是你自己定义的协议了,以上只不过是抛砖引玉而已。努力,一切在你自己。
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




