`
happmaoo
  • 浏览: 4347465 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

XmlHttp让Asp实现“多线程”

阅读更多

转自: http://blog.csdn.net/wzgme/archive/2004/10/26/153165.aspx
看了大白菜芯写的php实现多线程,一时间觉得有用,就改了个asp版的。。呵呵,这里感谢他的思路!(http://blog.csdn.net/wapweb/archive/2004/08/16/76319.aspx

1.原理实验

原理当然都一样,利用web服务器支持多线程,在同一页面里向服务器发多个http请求来完成我们的工作。更详细的分析,看大白菜芯写的吧。php里用Socket,asp当然用封装好了的xmlhttp了。

还是先实验一下,在一个页面里同时写2txt文件,比较写入时间的差异。代码如下:

<%

startime=timer()

''----------asp实现多线程----------''

function runThread()

dim Http

set Http=Server.createobject("Msxml2.XMLHTTP")

Http.open "GET","http://127.0.0.1/thread.asp?action=b",false

Http.send()

end function

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

function a()

dim Content,FilePath,MyFile

Content=now()&chr(30)&timer()

FilePath=server.MapPath("a.txt")

Set fso = CreateObject("Scripting.FileSystemObject")

Set MyFile = fso.CreateTextFile(FilePath, True)

MyFile.Write(Content)

MyFile.Close

end function

function b()

dim Content,FilePath,MyFile

Content=now()&chr(30)&timer()

FilePath=server.MapPath("b.txt")

Set fso = CreateObject("Scripting.FileSystemObject")

Set MyFile = fso.CreateTextFile(FilePath, True)

MyFile.Write(Content)

MyFile.Close

end function

if(Request.QueryString("action")="") then

runThread()

a()

else

b()

end if

%>

Script Execution Time:<%=fix((timer()-startime)*1000)%>ms

运行后的结果显示:

a文件和b文件中的时间是基本相同的。

2.实际应用比较

比如我同时抓取2个页面的html代码,一个sohu首页,一个是sina首页,用2种方式:一个是常规的顺序的代码执行,单线程执行,一个是这里的多线程执行,比较页面完成时间,代码如下:

testspeed1.asp:

<%

startime=timer()

function getHTTPPage(url)

on error resume next

dim http

set http=Server.createobject("Msxml2.XMLHTTP")

Http.open "POST",url,false

Http.send()

if Http.readystate<>4 then exit function

getHTTPPage=bytes2BSTR(Http.responseBody)

contents = getHTTPPage

Response.Write "<xmp>"

Response.Write(contents)

Response.Write "</xmp>"

set http=nothing

if err.number<>0 then err.Clear

end function

Function bytes2BSTR(vIn)

dim strReturn

dim i,ThisCharCode,NextCharCode

strReturn = ""

For i = 1 To LenB(vIn)

ThisCharCode = AscB(MidB(vIn,i,1))

If ThisCharCode < &H80 Then

strReturn = strReturn & Chr(ThisCharCode)

Else

NextCharCode = AscB(MidB(vIn,i+1,1))

strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))

i = i + 1

End If

Next

bytes2BSTR = strReturn

End Function

getHTTPPage("http://www.sohu.com/")

getHTTPPage("http://www.sina.com.cn/")

%>

Script Execution Time:<%=fix((timer()-startime)*1000)%>ms

Testspeed2.asp:

<%

startime=timer()

function getHTTPPage(url)

on error resume next

dim http

set http=Server.createobject("Msxml2.XMLHTTP")

Http.open "POST",url,false

Http.send()

if Http.readystate<>4 then exit function

getHTTPPage=bytes2BSTR(Http.responseBody)

contents = getHTTPPage

Response.Write "<xmp>"

Response.Write(contents)

Response.Write "</xmp>"

set http=nothing

if err.number<>0 then err.Clear

end function

Function bytes2BSTR(vIn)

dim strReturn

dim i,ThisCharCode,NextCharCode

strReturn = ""

For i = 1 To LenB(vIn)

ThisCharCode = AscB(MidB(vIn,i,1))

If ThisCharCode < &H80 Then

strReturn = strReturn & Chr(ThisCharCode)

Else

NextCharCode = AscB(MidB(vIn,i+1,1))

strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))

i = i + 1

End If

Next

bytes2BSTR = strReturn

End Function

function runThread()

dim Http

set Http=Server.createobject("Msxml2.XMLHTTP")

Http.open "GET","http://127.0.0.1/thread.asp?action=b",false

Http.send()

end function

function a()

getHTTPPage("http://www.sohu.com/")

end function

function b()

getHTTPPage("http://www.sina.com.cn/")

end function

if(Request.QueryString("action")="") then

runThread()

a()

else

b()

end if

%>

Script Execution Time:<%=fix((timer()-startime)*1000)%>ms

运行的时间结果:

次数

Testspeed1运行时间ms

Testspeed2.asp运行时间ms

1

15593

13078

2

13343

14375

3

12828

12515

4

12437

12125

5

12109

11734

6

12281

12140

7

12703

12062

8

13468

12656

9

12328

12187

10

12343

12156

以上10次是一个页面完后另一个页面再执行的。谁先谁后也是任意的。有一条记录异常。

为了避免网络的原因,以下5次将测试地址改成本机http://127.0.0.1

11

109

46

12

62

46

13

62

48

14

78

64

15

62

46

以上5次是一个页面完后另一个页面再执行的。谁先谁后也是任意的。

结果:好象是要快一点哦。。。。。。。。。。。

附录:阿泰兄发布了一个《XMLHTTP批量抓取远程资料》,打算和他说说,修改成这个思路的,发布出来,估计要等一下了。

分享到:
评论

相关推荐

    清水阁原创利用XMLHttp实现asp生成HTML静态文件(带生成进度条) v.rar

    今天这个是用XMLHttp的方法来实现的,页面很简单,直接执行html.asp文件就可以了, 再次强调这只是个方法,不是完整的ASp生成html的网站,是供网友学习时使用…… 文件说明: conn.asp 数据库链接文件 ...

    清水阁XMLHttp实现Asp生成HTML静态文件(带进度条)

    清水阁出品,使用XMLHttp对象实现Asp生成HTML静态文件,支持生成进度条显示,以前发布过asp生成静态文件的模块例子,不过那是基于模板替换的方法实现的,这个是用XMLHttp的方法来实现的,页面很简单,直接执行...

    xmlhttp示例asp

    xmlhttp示例,asp xmlhttp示例,asp xmlhttp示例,asp

    xmlhttp_asp+javascript+json+xml

    实现通用型基于xmlhttp的ajax模型。 客户端用javascript建立xmlhttp对象, 采用规范化的dom来处理上传的xml格式数据, 通过eval方法解译服务端回传的json格式数据。 服务端用asp编写,通过response.binaryRead()解译...

    xmlhttp+asp+javascript+xml+json

    不推荐下载,该版本存在较多问题 推荐下载笔者的新发布的该版本,名称为xmlhttp_asp+javascript+json+xml

    asp+xmlhttp+javascript+xml

    此为ajax的测试模型,通讯内容为xml形式的string; 客户端采用javascript提交xmlhttp请求; 服务端为asp进行处理

    ASP.NET后台代码实现XmlHttp跨域访问

    ASP.NET后台代码实现XmlHttp跨域访问

    XmlHttp手册XmlHttp手册

    XmlHttp手册XmlHttp手册XmlHttp手册XmlHttp手册XmlHttp手册

    No16.HTML在线编辑.颜色.图片.多线程.XmlHttp

    No16.HTML在线编辑.颜色.图片.多线程.XmlHttp No16.HTML在线编辑.颜色.图片.多线程.XmlHttp

    Asp+XmlHttp实现RssReader

    如何生成动态的XML文档,介于动态文档是ASP格式的,所以必须借助FSO进行XML文件的生成...我这里上传的资源里面包含了用Asp+XmlHttp实现RssReader功能,用ASP生成XML数据文档(RSS订阅)两个功能的代码。希望能给你帮助

    使用XMLHTTP POST 方式,实现远程请求

    使用XMLHTTP POST 方式,实现远程请求

    ASP实例开发源码-asp下利用XMLHttp实现生成HTML静态文件(带生成进度条).zip

    ASP实例开发源码—asp下利用XMLHttp实现生成HTML静态文件(带生成进度条).zip ASP实例开发源码—asp下利用XMLHttp实现生成HTML静态文件(带生成进度条).zip ASP实例开发源码—asp下利用XMLHttp实现生成HTML静态文件(带...

    xmlhttp说明文档,xmlhttp中文帮助文档,chm格式

    配合JavaScript可以实现页面数据在无刷新下的定时数据更新,如果应用在聊天室、文字直播上可以取得较好的视觉效果。 使用范例: vb中下载页面源代码的方法: Dim XmlHttp As Object Set XmlHttp = CreateObject(...

    用ASP开多线程

    在网上找到一个用ASP开的假线程,发现和...startime=timer()”———-asp实现多线程———-”function runThread()dim Httpset Http=Server.createobject(“Msxml2.XMLHTTP”) Http.open “GET”,”http://127.0.0.1/thr

    xmlhttp

    介绍xmlhttp比较的详细

    XMLHTTP实现HTTPS+post登录

    前几天群里有个朋友问我怎么登录BAIDU,我们... ... ... 然后看到XMLHTTP可以模拟发送HTTPS的数据包,于是便有了以下函数 //构造数据包 strcat(buf,"tpl_ok=&next;_target=&tpl=mn&skip;... XmlHttp(MyLogin_URL,"POST",buf);

    详解XMLHTTP控件的应用

    最近有一些朋友问我XMLHTTP对象到底是什么,有什么用。考试完了我反正没什么事,就写了篇文章详细地介绍一下Microsoft.XMLHTTP对象的使用,文章浅显,主要...调用XMLHTTP的方法很多,为了方便,本文用VBS脚本来实现。

    使用xmlHttp结合ASP实现网页的异步调用

    通过xmlHttp和ASP的结合,我们可以轻松完成网页的异步调用。代码如下:1.新建Display.asp(这是前台显示页面)注意xmlhttp.readyState的4个属性1:LOADING;2:LOADED;3:INTERACTIVE;4:COMPLETED &lt;&#37;@ Language=...

    XMLHTTP 例子

    XMLHTTP例子,Indy http例子

    易语言xmlhttp简单应用

    易语言xmlhttp简单应用源码,xmlhttp简单应用

Global site tag (gtag.js) - Google Analytics