|

楼主 |
发表于 2012-10-14 16:48:06
|
显示全部楼层
其实我很想知道,怎样提高它的效率,
在需要发送几十万次数据的时候,或者需要采集几十万次数据的情况下,怎样做效率才高?
附上一个函数,有待高手指点- '************************************************
- '函数名称: GetURLText
- '功能说明: 获取一个URL的源代码,
- '返回值 :
- '参数 :
- ' 1、ByVal ablnPost 是Post还是GET,
- ' 2、ByVal astrHost Host,一般是不带http://的,例如“www.baidu.com”
- ' 3、ByVal astrURL 要获取的URL,例如:http://www.baidu.com
- ' 4、ByVal astrPostData 要发送的数据
- ' 5、ByVal astrReferer Referer
- ' 6、ByRef astrText
- ' 7、ByRef [astrErrorInfo]
- ' 8、Byval [astrFakeIP]
- ' 9、ByVal [astrAuthorization_Basic]
- ' 10、ByRef [astrCookies] 如果有传Cookie进来,需要设置cookie[],
- ' 11、ByRef [astrCookiesRet] 本次返回的cookies值
- '
- '作者 : bloom
- '日期 : 2011-12-17 15:48:18
- '************************************************
- Public Function GetURLText(ByVal ablnPost As Boolean, _
- ByVal astrHost As String, ByVal astrURL As String, _
- ByVal astrPostData As String, ByVal astrReferer As String, _
- ByRef astrText As String, _
- Optional ByRef astrErrorInfo As String = "", _
- Optional ByVal astrFakeIP As String, _
- Optional ByVal astrAuthorization_Basic As String = "", _
- Optional ByRef astrCookies As String = "", _
- Optional ByRef astrCookiesRet As String = "", _
- Optional ByVal ablnGetReturn As Boolean = True _
- ) As Boolean
-
- On Error GoTo ErrLab
-
- Dim sngTimeOut As Single
- Dim xmlhttp As Object
-
- astrText = ""
- astrErrorInfo = ""
- astrCookiesRet = ""
-
- Set xmlhttp = CreateObject("microsoft.xmlhttp")
- If Not xmlhttp Is Nothing Then
- If LenB(astrURL) Then
- With xmlhttp
- .Open IIf(ablnPost, "POST", "GET"), astrURL, True '异步
- Call .setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded")
- Call .setRequestHeader("Accept", "*/*")
- Call .setRequestHeader("User-Agent", "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; TheWorld)")
- ' '不取缓存数据
- Call .setRequestHeader("If-Modified-Since", "0")
- If LenB(astrAuthorization_Basic) Then
- Call .setRequestHeader("Authorization Basic", astrAuthorization_Basic)
- End If
- If LenB(astrFakeIP) Then
- Call .setRequestHeader("X-Forwarded-For", astrFakeIP)
- End If
- If LenB(astrHost) Then
- Call .setRequestHeader("Host", astrHost)
- End If
- If LenB(astrReferer) Then
- Call .setRequestHeader("Referer", astrReferer)
- End If
- '设置Cookies,分解看里面有没有SetCookie的选项
- If LenB(astrCookies) Then
- Call .setRequestHeader("Cookie", astrCookies)
- End If
-
- If LenB(astrPostData) Then
- .send (astrPostData)
- Else
- .send
- End If
- End With
-
- If ablnGetReturn Then
- ' 不用等待返回
- sngTimeOut = GetTickCount()
- ' Debug.Print xmlhttp.Status
- Do While xmlhttp.ReadyState <> 4 '等待返回
- ' Debug.Print "xmlhttp.readyState:" & xmlhttp.readyState
- Sleep 300
- DoEvents
- ' Call MyDoEvents(0.02)
- '如果时间超超时
- If GetTickCount - sngTimeOut >= 60000 Then
- ' LogInfoB astrURL
- GetURLText = False
- GoTo ExitHere
- End If
- Loop
- ' Debug.Print "xmlhttp.readyState:" & xmlhttp.readyState
- If xmlhttp.Status = 200 Then '成功
- astrText = xmlhttp.responseText
- '获取到Cookies
- astrCookiesRet = xmlhttp.getAllResponseHeaders()
- GetURLText = True
- Else
- GetURLText = False
- astrErrorInfo = "HTTP错误,状态码:" & xmlhttp.Status
- End If
- Else
- sngTimeOut = GetTickCount()
- Do While xmlhttp.ReadyState < 2 '只要为2就行,不需要解析数据
- 'Sleep 300
- DoEvents
- If GetTickCount - sngTimeOut >= 60000 Then
- ' LogInfoB astrURL
- GetURLText = False
- GoTo ExitHere
- End If
- Loop
- GetURLText = True
- End If
- End If
- End If
-
- ExitHere:
- On Error Resume Next
-
- Set xmlhttp = Nothing
-
- Exit Function
- ErrLab:
- astrErrorInfo = Err.Description
- Err.Clear
- GoTo ExitHere
- End Function
复制代码 |
|