用MSXML2.ServerXMLHTTP的setTimeouts属性解决并死问题
发布时间:2010-03-02 20:21:00
照例使用XMLhttp同步方式获取数据,可是由于网络不稳定,经常造成'死锁'状况,既send之后一直不返回服务器结果,也不出错.
被这个问题折磨了好久,最后才查到还有ServerXMLHTTP这个对象,看了介绍才知道它才是天生为了服务器端获取其他网站信息设计的.利用他的超时机制setTimeouts,问题轻松解决:D
参考:
http://support.microsoft.com/kb/290761/zh-cn
下面付简单封装了ServerXMLHTTP的简单类,共参考:
<%
'使用范例
'读取URL 的HTML
dim myHttp
set myHttp=new xhttp
myHttp.URL="http://www.aspxhome.com"
Response.Write(myHttp.html)
'保存远程图片到本地
myHttp.URL="https://www.aspxhome.com/images/logo.gif"
myHttp.saveimage="myfile.gif"
'为防止xhttp卡死的情况,使用超时,错误处理
dim sHtmlcode,iStep
myHttp.URL="http://www.acnow.net"
sHtmlcode=myHttp.html
do while myHttp.xhttpError=""
Response.Error("ERROR: AGAIN!<br />")
sHtmlcode=myHttp.html
iStep=iStep+1
if iStep>100 then
Response.Write("ERROR:OVER!<hr />")
exit do
end if
loop
Response.Write(sHtmlcode)
set myHttp=nothing
'--------------------------------------------------------------------
Class xhttp
private cset,sUrl,sError
Private Sub Class_Initialize()
'cset="UTF-8"
cset="GB2312"
sError=""
end sub
Private Sub Class_Terminate()
End Sub
Public Property LET URL(theurl)
sUrl=theurl
end property
public property GET BasePath()
BasePath=mid(sUrl,1,InStrRev(sUrl,"/")-1)
end property
public property GET FileName()
FileName=mid(sUrl,InStrRev(sUrl,"/")+1)
end property
public property GET Html()
Html=BytesToBstr(getBody(sUrl))
end property
public property GET xhttpError()
xhttpError=sError
end property
private Function BytesToBstr(body)
on error resume next
'Cset:GB2312 UTF-8
dim objstream
set objstream = Server.CreateObject("adodb.stream")
with objstream
.Type = 1 '
.Mode = 3 '
.Open
.Write body '
.Position = 0 '
.Type = 2 '
.Charset = Cset '
BytesToBstr = .ReadText '
.Close
end with
set objstream = nothing
End Function
private function getBody(surl)
on error resume next
dim xmlHttp
'Set xmlHttp=server.createobject("Msxml2.XMLHTTP.4.0")
'set xmlHttp=server.createobject("Microsoft.XMLHTTP")
set xmlHttp=server.createobject("MSXML2.ServerXMLHTTP")
xmlHttp.setTimeouts 10000,10000,10000,30000
xmlHttp.open "GET",surl,false
xmlHttp.send
if xmlHttp.readystate=4 then
'if xmlHttp.status=200 then
getBody=xmlhttp.responsebody
'end if
else
getBody=""
end if
if Err.Number<>0 then
sError=Err.Number
Err.clear
else
sError=""
end if
set xmlHttp=nothing
end function
Public function saveimage(tofile)
on error resume next
dim objStream,imgs
imgs=getBody(sUrl)
Set objStream = Server.CreateObject("ADODB.Stream")
with objStream
.Type =1
.Open
.write imgs
.SaveToFile server.mappath(tofile),2
.Close()
end with
set objstream=nothing
end function
end class
%>
猜你喜欢
- 背景最近在一次线上作业过程中执行了一句DML语句,本以为万无一失,结果应用反馈说没有更新,数据还是没有变,最后经过排查才发现是我语句写错了,
- 1. 获取abi文件合约的接口在remix工具中编译合约后,会有一个abi,复制然后新建一个xx.abi文件,把赋值的粘贴到里面注意:代码变
- 第一步:换源输入命令换掉Ubuntu的下载源sudo nano /etc/apt/sources.list将以下全部替换掉原文件,我这里用的
- 用途:图片经常使用onload来改变宽度,但这样会出现图片的闪烁,这个简单的类就是用来解决这个问题的。函数loadImage,用来加载图片,
- 本文讲述了Python检测网络延迟的代码。分享给大家供大家参考,具体如下:#!/usr/bin/env python # coding: u
- 一、项目介绍 在此项目中,目的是预测爱荷华州A
- 1、注释默认的SQLite3配置:blogproject/settings.py'''DATABASES = { &
- 问题描述:为了把之前的CPU版本的tensorflow卸载,换成GPU版本的tensorflow,经历了一番折腾。BUG1 Could no
- 1、使用说明首先说明,本文所使用的功能为pycharm专业版所支持,其他版本如社区版,教育版,则不一定支持。作为一名后端开发,我猜你的桌面上
- numpy库概述numpy库处理的最基础数据类型是由同种元素构成的多维数组,简称为“数组”数组的特点:数组中所有元素的类型必须相同数组中元素
- 最近在用vue做项目,学习了不少东西,但是有时候光顾着做项目却忘记要找个时间来整理一下最近的一些学习新得,因为是新手,所以可能会有错误的地方
- 这篇文章主要介绍了python实现括号匹配方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- ECMAScript 6 新增 const 和 let 命令,用来声明变量。声明方式变量提升作用域初始值重复定义const否块级需要不允许l
- Pandas count()与values_count()用法count()values_count()在指定的统计的列名上结果多了该列:对
- 首先抛出我们在讨论使用回调编程时的一些观点:激活errback是非常重要的。由于errback的功能与except块相同,因此用户需要确保它
- 网上提供了很多sqlserver发送邮件的文章,转载居多,大部分介绍win2000+outlook2003环境,在操作系统和office已经
- 大家好,我是不学前端的前端程序员,事情是这个样子的,前几天不是双十一预购秒杀嘛由于我女朋友比较笨,手速比较慢,就一直抢不到,她没抢到特价商品
- Python decimal 模块Python中的浮点数默认精度是15位。Decimal对象可以表示任意精度的浮点数。getcontext函
- 一、分析链接上一篇文章指路一般来说,我们参加某个网站的盖楼抽奖活动,并不是仅仅只参加一个,而是多个盖楼活动一起参加。这个时候,我们就需要分析
- 其实网上已经有许多python语言书写的串口,但大部分都是python2写的,没有找到一个合适的python编写的串口助手,只能自己来写一个