本帖最后由 甲壳虫 于 2012-2-28 21:33 编辑
我想从网页的第500个字符开始读取,而不想要前面的字符,是否有可能实现?
sex123 发表于 2012-2-28 19:34
这需要服务器支持断点续传网页内容。大多服务器传送文件支持断点续传,但网页内容大多不支持。下面二个例子,第一个可以,第二个(就是本论坛)不支持,你可以试试(脚本没考虑 Chunked Transfer)。
;~ 例一:支持断点续传
$host = "www.w3.org"
$page = "/Protocols/rfc2616/rfc2616-sec14.html"
;~ 例二:不支持断点续传
;~ $host = "www.autoitx.com"
;~ $page = "/index.php"
;~ 字节范围
$bFrom = 500
$bTo = 600
TCPStartup()
$socket = TCPConnect(TCPNameToIP($host), 80)
If $socket = -1 Then
TCPShutdown()
EndIf
$command = "GET " & $page & " HTTP/1.1" & @CRLF
$command &= "Host: " & $host & @CRLF
$command &= "Range: bytes=" & $bFrom & "-" & $bTo & @CRLF
$command &= "Connection: close" & @CRLF
$command &= "" & @CRLF
TCPSend($socket, $command)
Local $recv, $sdata, $b = 1024
Do
$recv &= TCPRecv($socket, 1)
Until @error Or StringInStr($recv, @CRLF & @CRLF)
ConsoleWrite('服务器回应的header:' & @CRLF & $recv & @crlf) ;### Debug Console
; 以上为网页的response header,应去掉
; 回应 HTTP/1.1 200 OK - 不支持断点续传,将传送整个网页
; 回应 HTTP/1.1 206 Partial Content - 支持断点续传,允许只接收部分数据
While 1
$recv = TCPRecv($socket, $b, 1) ; 接收
If @error Then ExitLoop
$sdata = _WinHttpBinaryConcat($sdata, $recv)
WEnd
TCPCloseSocket($socket)
TCPShutdown()
$data = BinaryToString($sdata)
ConsoleWrite('接收的网页数据:' & @crlf & $data & @crlf) ;### Debug Console
; #FUNCTION# ;===============================================================================
; Name...........: _WinHttpBinaryConcat
; Description ...: Concatenates two binary data returned by _WinHttpReadData() in binary mode.
; Syntax.........: _WinHttpBinaryConcat(ByRef $bBinary1, ByRef $bBinary2)
; Parameters ....: $bBinary1 - Binary data that is to be concatenated.
; $bBinary2 - Binary data to concatenate.
; Return values .: Success - Returns concatenated binary data.
; Failure - Returns empty binary and sets @error:
; |1 - Invalid input.
; Author ........: ProgAndy
; Modified.......: trancexx
; Remarks .......:
; Related .......: _WinHttpReadData
; Link ..........:
; Example .......:
;============================================================================================
Func _WinHttpBinaryConcat(ByRef $bBinary1, ByRef $bBinary2)
Switch IsBinary($bBinary1) + 2 * IsBinary($bBinary2)
Case 0
Return SetError(1, 0, Binary(''))
Case 1
Return $bBinary1
Case 2
Return $bBinary2
EndSwitch
Local $tAuxiliary = DllStructCreate("byte[" & BinaryLen($bBinary1) & "];byte[" & BinaryLen($bBinary2) & "]")
DllStructSetData($tAuxiliary, 1, $bBinary1)
DllStructSetData($tAuxiliary, 2, $bBinary2)
Local $tOutput = DllStructCreate("byte[" & DllStructGetSize($tAuxiliary) & "]", DllStructGetPtr($tAuxiliary))
Return DllStructGetData($tOutput, 1)
EndFunc ;==>_WinHttpBinaryConcat
|