Jump to content

Recommended Posts

My script was using _INetGetSource and BinaryToString function to get source of some webpage. It's not working anymore. I was searching forum and found this topic but there's still no result. Any idea?

My code (used to work but it's no longer working):

#include <Inet.au3>

_GetWebpageSourceCode("https://allegro.pl/oferta/fairy-platinum-kapsulki-do-zmywarek-100-szt-8100632319")

Func _GetWebpageSourceCode($sLink)
    $sHtmlSource = _INetGetSource($sLink, False)
    if @error then Return ConsoleWrite("_INetGetSource() error=" & @error & @CRLF)
    $sHtmlSource = BinaryToString($sHtmlSource, 4)
    if @error then Return ConsoleWrite("BinaryToString() error=" & @error & @CRLF)
    ConsoleWrite($sHtmlSource & @CRLF)
 EndFunc

New code (does not return source code too)

_GetWebpageSourceCode()

Func _GetWebpageSourceCode()
    TCPStartup()
    If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1)
    Local $sHost = "www.allegro.pl"
    Local $sIPAddress = TCPNameToIP($sHost)
    If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1)
    ConsoleWrite("$sIPAddress=" & $sIPAddress & @CRLF)
    Local $iSocket = TCPConnect($sIPAddress, 80)
    If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1)
    $sLink = "https://allegro.pl/oferta/fairy-platinum-kapsulki-do-zmywarek-100-szt-8100632319"
    $sRegEx = StringRegExp($sLink, "allegro\.pl/(.*)", 1)
    If @error Then _ReportError("Bad link", "", @ScriptLineNumber - 1)
    $sLink = $sRegEx[0]
    ConsoleWrite("$sLink=" & $sLink & @CRLF)
    Local $sRequest = "GET /" & $sLink & " HTTP/1.1" & @CRLF & _
        "Host: " & $sHost & @CRLF & _
        "Connection: close" & @CRLF & @CRLF
    ConsoleWrite("$sRequest:" & @CRLF &  $sRequest & @CRLF)
    TCPSend($iSocket, $sRequest)
    If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1)
    Local $sData = "", $hTimer = TimerInit()
    While 1
        $sRecv = _TCPRecv($iSocket, 2048)
        If $sRecv Then $sData &= $sRecv
        If @error Or @extended Then
            If @error Then _ReportError(@error, @extended, @ScriptLineNumber - 1)
            ExitLoop
        EndIf
        If $sRecv Then $hTimer = TimerInit()
        If TimerDiff($hTimer) > 3000 Then ExitLoop
        Sleep(10)
    WEnd
    ConsoleWrite("Response received: " & @CRLF & $sData & @CRLF)
    TCPCloseSocket($iSocket)
    TCPShutdown()
EndFunc

Func _ReportError($iError, $iExtended, $iLine)
    ConsoleWrite("Error: " & $iError & @CRLF & _
        "Extended: " & $iExtended & @CRLF & _
        "Line: " & $iLine & @CRLF)
    TCPShutdown()
    Exit
EndFunc

Func _TCPRecv($iMainsocket, $iMaxLen, $iFlag = 0)
    If IsArray($iMainsocket) And (UBound($iMainsocket, 0) = 1) And (UBound($iMainsocket) > 0) Then $iMainsocket = $iMainsocket[0]
    If $iFlag = Default Then $iFlag = 0
    $iMainsocket = Number($iMainsocket)
    $iMaxLen = Number($iMaxLen)
    $iFlag = Number($iFlag)
    If $iMainsocket < 0 Or _
            $iMaxLen < 1 Or _
            Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(-4, 0, -1) ; invalid parameter

    Local $hWs2 = DllOpen("Ws2_32.dll")
    If @error Then Return SetError(-2, 0, -1) ;missing DLL
    Local $bError = 0, $nCode = 0, $nExtended = 0

    If Not $bError Then
        $aRet = DllCall($hWs2, "int", "ioctlsocket", "uint", $iMainsocket, "long", 0x8004667e, "ulong*", 1) ;FIONBIO
        If @error Then
            $bError = -1
        ElseIf $aRet[0] <> 0 Then ;SOCKET_ERROR
            $bError = 1
        EndIf
    EndIf

    Local $tBuf = DllStructCreate("char[" & $iMaxLen & "]")
    $aRet = DllCall($hWs2, "int", "recv", "uint", $iMainsocket, "ptr", DllStructGetPtr($tBuf), "int", $iMaxLen, "int", 0)
    If @error Then
        $bError = -1
    ElseIf ($aRet[0] = -1) Or ($aRet[0] = 4294967295) Then ;SOCKET_ERROR
        $bError = 1
        $aRet = DllCall($hWs2, "int", "WSAGetLastError")
        If @error Then
            $bError = -1
        ElseIf $aRet[0] = 0 Or $aRet[0] = 10035 Then ;WSAEWOULDBLOCK
            $nCode = -10 ;internal function value, it means no error
        EndIf
    ElseIf $aRet[0] = 0 Then
        $bError = 1
        $nCode = -10
        $nExtended = 1 ;connection closed
    Else
        Local $sResult = DllStructGetData($tBuf, 1) ;data
        If BitAND($iFlag, 2) = 2 Then ;EOT
            If StringRight($sResult, 1) = Chr(3) Then
                $sResult = StringTrimRight($sResult, 1)
                $nExtended = 2 ;End of Text reached
            EndIf
        EndIf
        If BitAND($iFlag, 1) = 1 Then $sResult = Binary($sResult)
    EndIf

    If $bError < 0 Then
        $nCode = -1 ;internal error
        $nReturn = "" ;failure
    ElseIf $bError > 0 Then
        If Not $nCode Then
            $aRet = DllCall($hWs2, "int", "WSAGetLastError")
            If @error Then
                $nCode = -1
            Else
                $nCode = $aRet[0]
            EndIf
            If $nCode = 0 Then $nCode = -3 ;undefined error
        EndIf
        If $nCode = -10 Then $nCode = 0
        $nReturn = ""
    Else
        $nReturn = $sResult
    EndIf
    DllClose($hWs2)
    Return SetError($nCode, $nExtended, $nReturn)
EndFunc   ;==>_TCPRecv

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...