Jump to content

Download / Upload files from server behind proxy


nicero
 Share

Recommended Posts

I'm an AutoIt absolute beginner and I'm working (hard) to manage to download and upload files from a server. The problem I'm facing, I suppose, it's due to the fact that I'm behind a proxy. I read carefully every single post on this forum about downloading / uploading files.

I know about HttpSetProxy and INetGet but although I can get proxy settings from the Windows' registry, I'm not able to download anything with INetGet. None of the following HttpSetProxy (one of the three alternatives of course) settings work:

Local $sProxyServer = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer")
Local $sProxyOverride = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride")

HttpSetProxy(0) 

HttpSetProxy(0, $sProxyServer)

HttpSetProxy(2, $sProxyServer)

This is what I achieved up to now thanks to a useful user's post:

DOWNLOAD (using WinHTTP)

#include "WinHttp.au3"
#include "WinHttpConstants.au3"
#include <Array.au3>

Opt("MustDeclareVars", 1)

; Internet Explorer proxy configuration for the current user:
Global $aIEproxy = _WinHttpGetIEProxyConfigForCurrentUser()

Global $sHost = "xxx.yyy.kkk.zzz"
Global $sTarget = "image.jpg"
Global $sDestination = @ScriptDir & "\image.jpg"

; Initialize and get session handle
Global $hHttpOpen = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $aIEProxy[2], $aIEProxy[3])
If @error Then
    MsgBox(48, "Error", "Error initializing the usage of WinHTTP functions.")
    Exit 1
EndIf
; Get connection handle
Global $hHttpConnect = _WinHttpConnect($hHttpOpen, $sHost)
If @error Then
    MsgBox(48, "Error", "Error specifying the initial target server of an HTTP request.")
    _WinHttpCloseHandle($hHttpOpen)
    Exit 2
EndIf
; Specify the reguest
Global $hHttpRequest = _WinHttpOpenRequest($hHttpConnect, Default, $sTarget)
If @error Then
    MsgBox(48, "Error", "Error creating an HTTP request handle.")
    _WinHttpCloseHandle($hHttpConnect)
    _WinHttpCloseHandle($hHttpOpen)
    Exit 3
EndIf
; Send request
_WinHttpSendRequest($hHttpRequest)
If @error Then
    MsgBox(48, "Error", "Error sending specified request.")
    _WinHttpCloseHandle($hHttpConnect)
    _WinHttpCloseHandle($hHttpOpen)
    Exit 4
EndIf

; Wait for the response
_WinHttpReceiveResponse($hHttpRequest)
; Read if available
Global $bChunk, $bData, $hFile
If _WinHttpQueryDataAvailable($hHttpRequest) Then
    While 1
        $bChunk = _WinHttpReadData($hHttpRequest, 2) ; read binary
        If @error Then ExitLoop
        $bData = _WinHttpSimpleBinaryConcat($bData, $bChunk) ; concat two binary data
    WEnd
    ; Save it to the file
    $hFile = FileOpen($sDestination, 26)
    FileWrite($hFile, $bData)
    FileClose($hFile)
Else
    MsgBox(48, "Error occurred", "No data available. " & @CRLF)
EndIf

; Close handles
_WinHttpCloseHandle($hHttpRequest)
_WinHttpCloseHandle($hHttpConnect)
_WinHttpCloseHandle($hHttpOpen)

Question #1. Any ideas why I'm not able to download with INetGet? Should I keep using WinHTTP?

UPLOAD

Thanks to an other precious post I managed to upload a file with the following code:

$HTTPREQUEST_PROXYSETTING_PROXY     = 2;

$File = @ScriptDir & "\1.gif"
$sHost = "xxx.yyy.kkk.zzz"
$sFormAction = "/postdata.php"

$hfile = FileOpen($File, 16)
$sFileTypeName = StringRegExpReplace($File, '^.*\\', '')

; Get system proxy settings
Local $sProxyServer = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer")
Local $sProxyOverride = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride")

While 1
    $data = FileRead($hfile, 500000) ;Read file in 500Kb chunks
    If @error Then ExitLoop
    Global $Data2 = StringTrimLeft($data,2)
    SendPost()
WEnd

Func SendPost()
   $oRequest = ObjCreate('WinHttp.WinHttpRequest.5.1')
   $oRequest.SetProxy( $HTTPREQUEST_PROXYSETTING_PROXY, $sProxyServer, $sProxyOverride);
   $oRequest.Open('POST', 'http://' & $sHost & $sFormAction, 0)
   $oRequest.SetRequestHeader('User-Agent', 'Mozilla/4.0 (Windows XP 5.1)')
   $oRequest.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
   $oRequest.SetRequestHeader('Host', $sHost)
   $oRequest.Send('filename=' & $sFileTypeName & '&data=' & $Data2)
   $sData = $oRequest.ResponseText
   ConsoleWrite($File & @CRLF)
EndFunc

Which calls the following PHP

<?php 
    $fileName = $_POST['filename']; 
    $binaryData = $_POST['data']; 
    $fh = fopen("./$fileName", 'a+b'); 
    fwrite($fh, pack("H*" , $binaryData)); 
    fclose($fh); 
?>

Question #2. As said I'm an absolute beginner. If I'm going to use WinHTTP for downloading would someone please translate the above upload code to work with WinHTTP to keep code consistency and coherency? I found >this post but it didn't worked altough I did set the proxy.

Thank you

Edited by nicero
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...