Jump to content

Download a Zip file using WinHttp


Recommended Posts

Hello,

I wrote a web API in php/mysql that returns a ZIP file when doing a GET request of this type: www.mysite.com/folder/?user=myname
And adding a special Header:

Authorization: xxxxxxxxxxxxxxxxxxxx

So based on the code i found  everywhere on the forums I did the following program:

#include "WinHttp.au3"

$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, "www.mysite.com")
$hRequest = _WinHttpOpenRequest($hConnect, 'GET', "/folder/?user=kiki", Default, "http://www.mysite.com/", Default, $WINHTTP_FLAG_SECURE)
_WinHttpAddRequestHeaders($hRequest, "Authorization: xxxxxxxxxxxxxxxxxxxx")
_WinHttpSendRequest($hRequest, Default)

; Wait for the response
_WinHttpReceiveResponse($hRequest)
If @error Then
    MsgBox(48, "Error", "Error waiting for the response from the server.")
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
    Exit 5
EndIf

; See if there is data to read
Global $sChunk, $sData
If _WinHttpQueryDataAvailable($hRequest) Then
    ; Read
    While 1
        $sChunk = _WinHttpReadData($hRequest)
        If @error Then ExitLoop
        $sData &= $sChunk
    WEnd
    ; Write ZIP file
    $hFile = FileOpen(@ScriptDir & "\files.zip", 18)
    FileWrite($hFile, $sData)
    FileClose($hFile)
Else
    MsgBox(48, "Error", "Site is experiencing problems.")
EndIf

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

On my web server I see the logs of the request, and the zip has been sent, but on the PC the received ZIP file is corrupt, it is only 3Ko instead of 100KB
Where is my mistake ?? there is obviously a concern in my way of saving the data received, in the "ZIP File Registration" part.

Thanks for your help.

Edited by cetipabo
Link to comment
Share on other sites

You should initially declare $sData as binary and then use _WinHttpReadData() in binary mode

;...
$sData = Binary("")
;...
$sChunk = _WinHttpReadData($hRequest, 2)
;...

...or use _WinHttpSimpleBinaryConcat()

 

But why not simply:

#include "WinHttp.au3"


; Initialize and get session handle
$hOpen = _WinHttpOpen()
; Get connection handle
$hConnect = _WinHttpConnect($hOpen, "www.mysite.com")

; SimpleSSL-request it...
$vReturned = _WinHttpSimpleSSLRequest($hConnect, Default, "/folder/?user=kiki", "http://www.mysite.com/", Default, "Authorization: xxxxxxxxxxxxxxxxxxxx")

; Close handles
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)


$hFile = FileOpen(@ScriptDir & "\files.zip", 18)
FileWrite($hFile, $vReturned)
FileClose($hFile)

 

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Thank you very much @trancexx

Your suggestion fixed my problem. :thumbsup:

 

But your second suggestion is making an error at line 19 : Error unable to parse line

image.png.8e98241cfd4d442113eacf474a65e1a4.png

Surprisingly, if i comment the Filecose, then the zip file is correctly downloaded...looks like the Fileclose is executed before the file is downloaded or something like that...

I tried also to add a sleep(5000) before the Fileclose, but i still have that error....i don't understand what's happening.

Edited by cetipabo
Link to comment
Share on other sites

There's probably an unseen character in the copy/paste of the code you're not seeing. Try backsapcing up to the closing parentheses and see if that fixes it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 1 month later...

Hello,

is there a reason why it doesn't work when i replace the domain name with an ip:port ?

#include "WinHttp.au3"


; Initialize and get session handle
$hOpen = _WinHttpOpen()
; Get connection handle
$hConnect = _WinHttpConnect($hOpen, "xxx.xxx.xxx.xxx",5000)

; SimpleSSL-request it...
$vReturned = _WinHttpSimpleSSLRequest($hConnect, Default, "/folder/?user=kiki", Default, Default, "Authorization: xxxxxxxxxxxxxxxxxxxx")

; Close handles
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)


$hFile = FileOpen(@ScriptDir & "\files.zip", 18)
FileWrite($hFile, $vReturned)
FileClose($hFile)

Thanks for your help

Edited by cetipabo
Link to comment
Share on other sites

@Zedna that was the first thing i tried... 😩 no luck

@trancexx do you have an idea ?

OK, i went deeper and my problem doesn't seem to be related to the IP, i think winhttp is sending more Headers than specified, it is necessary for me to send only the Authorization header.

is there a way to see what headers are going to be sent ?? and delete them ? i can't see anything when i use Fiddler or any http debuger...

Edited by cetipabo
Link to comment
Share on other sites

You can use fiddler as proxy to intercept traffic with winhttp of course, you just have to specify its ip address and port when you call WinHttpOpen.

"Ip address : Port number" can't work if your website is hosted on shared server. How could server know which domain is wanted??

♡♡♡

.

eMyvnE

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...