Sign in to follow this  
Followers 0
dragonpiper

Http Requests to download image

4 posts in this topic

#1 ·  Posted (edited)

Hello I've been trying to download an image but for some reason when its done its not readable.Maybe you guys know whats wrong

#include <IE.au3>
#include <IE.au3>
#include <Array.au3>
#include <string.au3>

$path = ""
$addr = "www.gpsdiscussion.com"

TCPStartup()

$sc = TCPConnect(TCPNameToIP($addr), 80)
If NOT $sc < 0 Then
    MsgBox(16, "Connection Problem", @error & " Problem while connecting to host")
    exit(1)
EndIf

$request = "GET /iconimages/general-gps-discussion/smallest-gps-transponder-available_ltr.gif HTTP/1.1" & @CRLF & _
"User-Agent: AutoIt" & @CRLF & _
"Host: www.gpsdiscussion.com" & @CRLF & _
"Cache-Control: no-cache" & @CRLF & @CRLF
FileDelete("C:\image.gif")
$data = TCPSend($sc, $request)
Do

    $line = TCPRecv($sc,1024)
    If $line <> "" Then
        If StringInStr(BinaryToString($line),"HTTP") Then
            $line = StringToBinary(StringMid($line,StringInStr($line,@CRLF & @CRLF)+4))
        EndIf
        FileWrite("C:\image.gif",$line)
    EndIf
Until FileExists("C:\image.gif") And $line <> ""
MsgBox(0,"","DONE")
Edited by dragonpiper

Share this post


Link to post
Share on other sites



Hello I've been trying to download an image but for some reason when its done its not readable.Maybe you guys know whats wrong

FileWrite("C:\Users\Angel\Apps\image.jpg",Binary(ClipGet()))
Exit

Uuuuuhm.......

1 - Maybe its the Exit command after your first line of code. Nothing else is being executed

2 - Clipget returns text from the clipboard, not graphics.

Share this post


Link to post
Share on other sites

Uuuuuhm.......

1 - Maybe its the Exit command after your first line of code. Nothing else is being executed

2 - Clipget returns text from the clipboard, not graphics.

Sorry fixed it above. I however did the mote that mistake in scite i deleted it and ran it. I still don't get a proper image.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You can force TCPRecv to binary, but it should do that automated. Also check if the return dooesn't have anything added to the beginning or end.

After another look I think the problem is in the constant binary to string and back conversion. Data can get lost that way.

Edit:

Try this:

#include <IE.au3>
#include <Array.au3>
#include <string.au3>

Global $addr = "www.gpsdiscussion.com"
Global $IP, $Socket, $sRequest
Global $bData, $bChunk
Global $hFile

TCPStartup()

$IP = TCPNameToIP($addr)
If @error Then
    MsgBox(0,"TCPNameToIP Error!","Error code: " & @error & ".")
    Exit
EndIf

$Socket = TCPConnect($IP, 80)
If @error Then
    MsgBox(0,"TCPConnect Error!","Error code: " & @error & ".")
    Exit
EndIf

$sRequest = "GET /iconimages/general-gps-discussion/smallest-gps-transponder-available_ltr.gif HTTP/1.1" & @CRLF & _
"User-Agent: AutoIt" & @CRLF & _
"Host: www.gpsdiscussion.com" & @CRLF & _
"Cache-Control: no-cache" & @CRLF & @CRLF

TCPSend($Socket, $sRequest)

Do
    $bChunk = TCPRecv($Socket,16,1)
    If $bChunk Then
;~      $bData = Binary($bData & StringTrimLeft($bChunk,2)) ;I can't find a way to do a binary concatenate without a string conversion. :<
;~      Yoink! Stole this from the WinHttp UDF
        $bData = _BinaryConcat($bData, $bChunk)
    EndIf
Until $bData And Not $bChunk

TCPCloseSocket($Socket)

TCPShutdown()

$bData = BinaryMid($bData,StringInStr(BinaryToString($bData),@CRLF & @CRLF)+4) ;trim the header

$hFile = FileOpen("C:\image.gif",BitOR(2, 16))
FileWrite($hFile,$bData)
FileClose($hFile)

MsgBox(0,"","DONE")
Exit

; #FUNCTION# ;===============================================================================
; Name...........: _BinaryConcat (previously _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 concat.
; 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 _BinaryConcat(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 ;==>_BinaryConcat

Edit2: Grabbed the Binary concatenation function from the WinHttp UDF.

Edited by Tvern

Share this post


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
Sign in to follow this  
Followers 0