Download de arquivo usando proxy
Enviado: 30 Dez 2015 13:50
Pessoal,
Existe algum exemplo de download de arquivo usando proxy?
Existe algum exemplo de download de arquivo usando proxy?
Código: Selecionar todos
FUNCTION MAIN
LOCAL cURL := "http://harbour.github.io/art/harbour-ico.zip"
IF ( oHTTP := win_oleCreateObject( "WinHttp.WinHttpRequest.5.1" ) ) != NIL
oHTTP:Open( "GET", cURL, .F. )
//oHTTP:SetProxy(2, "proxy:8080")
oHTTP:Send()
IF oHTTP:Status() == 200
? "Downloaded", hb_ntos( hb_BLen( oHTTP:responseBody ) ), "byte(s)"
ENDIF
ELSE
? "Error: WinHttp 5.1 not available. [" + win_oleErrorText() + "]"
ENDIF
inkey(0)
Código: Selecionar todos
? "Downloaded", hb_ntos( hb_BLen( oHTTP:responseBody ) ), "byte(s)"
Código: Selecionar todos
IF ValType( oHttp:ResponseBody ) == "C"
hb_MemoWrit( "arquivo.zip", oHttp:ResponseBody )
ELSE
nHandle := fCreate( "arquivo.zip" )
FOR EACH nAscii IN oHttp:Responsebody // FOR nCont = 1 TO Len( oHttp:ResponseBody )
fWrite( nHandle, Chr( nAscii ) )
NEXT
FClose( nHandle )
ENDIF
Código: Selecionar todos
adTypeBinary := 1
adSaveCreateOverWrite := 2
BinaryStream := win_oleCreateObject("ADODB.Stream")
BinaryStream:Type := adTypeBinary
IF ( oHTTP := win_oleCreateObject( "WinHttp.WinHttpRequest.5.1" ) ) != NIL
oHTTP:Open( "GET", cURL, .F. )
//oHTTP:SetProxy(2, "proxy:8080")
oHTTP:Send()
IF oHTTP:Status() == 200
? "Downloaded", hb_ntos( hb_BLen( oHTTP:responseBody ) ), "byte(s)"
cTeste:=oHTTP:responsebody
BinaryStream:Open()
BinaryStream:Write(cTeste)
BinaryStream:SaveToFile("teste.zip", adSaveCreateOverWrite)
ENDIF
ENDIF
Código: Selecionar todos
Sample 2:
;***************************************************************************
;**
;** Code to download a binary file using WinHttp.WinHttpRequest.5.1 and ADODB.Stream
;**
;***************************************************************************
#DefineFunction SaveBinaryData(FileName, ByteArray)
adTypeBinary = 1
adSaveCreateOverWrite = 2
;Create Stream object
BinaryStream = ObjectCreate("ADODB.Stream")
;Specify stream type - binary data.
BinaryStream.Type = adTypeBinary
;Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.Write(ByteArray)
;Save binary data To disk
BinaryStream.SaveToFile(FileName, adSaveCreateOverWrite)
BinaryStream = 0
Return
#EndFunction
; Instantiate a WinHttpRequest object.
WinHttpReq = ObjectCreate("WinHttp.WinHttpRequest.5.1")
; Initialize an HTTP request.
WinHttpReq.Open("GET", "http://files.winbatch.com/wwwftp/wb01/wwctl44i.zip", @FALSE)
; Send the HTTP request.
WinHttpReq.Send()
; Display the response text.
ByteArray = WinHttpReq.ResponseBody
SaveBinaryData("d:\temp\wwctl44i.zip", ByteArray)
; Clean up
WinHttpReq = 0
Exit
Código: Selecionar todos
oSoap := Win_OleCreateObject( "MSXML2.ServerXMLHTTP" )
Código: Selecionar todos
STATIC FUNCTION dl_file( cURL, cFileName )
LOCAL lSuccess
#if defined( __PLATFORM__WINDOWS )
LOCAL tmp
LOCAL cScript
LOCAL cJS
#endif
IF hb_processRun( ;
"curl " + ;
"-fsS " + ;
"-o " + FNameEscape( cFileName ) + " " + ;
"-L --proto-redir =https " + ;
Chr( 34 ) + StrTran( cURL, " ", "%20" ) + Chr( 34 ) ) >= 0
RETURN .T.
ENDIF
lSuccess := .F.
#if defined( __PLATFORM__WINDOWS )
#pragma __cstream | cJS := %s
var http = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
http.Open("GET", "%1$s", false);
http.Send();
if(http.Status() == 200) {
var f = new ActiveXObject("ADODB.Stream");
f.type = 1; f.open(); f.write(http.responseBody);
f.savetofile("%2$s", 2);
}
#pragma __endtext
hb_vfClose( hb_vfTempFile( @cScript,,, ".tmp" ) )
IF hb_MemoWrit( cScript, ;
hb_StrFormat( cJS, ;
cURL, ;
StrTran( cFileName, "\", "\\" ) ) )
IF hb_processRun( "cscript" + ;
" //nologo" + ;
" /e:jscript" + ;
" " + FNameEscape( cScript ),, @tmp, @tmp ) == 0
lSuccess := .T.
ENDIF
ENDIF
hb_vfErase( cScript )
#endif
RETURN lSuccess
Código: Selecionar todos
var f = new ActiveXObject("ADODB.Stream");
f.type = 1; f.open(); f.write(http.responseBody);
f.savetofile("%2$s", 2);
Código: Selecionar todos
f := win_OleDbCreateObject( "ADODB.Stream" )
f:Type := 1
f:Open()
f:Write( oHttp:ResponseBody )
f:SaveToFile( "nomearquivo", 2 )
Código: Selecionar todos
#include "hbcompat.ch"
FUNCTION Download()
LOCAL cURL := "http://harbour.github.io/art/harbour-ico.zip"
LOCAL cArq := "harbour-ico.zip"
IF dl_file( cURL, cArq )
RETURN .T.
ENDIF
RETURN Nil
FUNCTION dl_file( cURL, cFileName )
LOCAL lSuccess
#if defined( __PLATFORM__WINDOWS )
LOCAL tmp
LOCAL cScript
LOCAL cJS
#endif
#pragma __cstream | cJS := %s
var http = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
http.Open("GET", "%1$s", false);
http.Send();
if(http.Status() == 200) {
var f = new ActiveXObject("ADODB.Stream");
f.type = 1; f.open(); f.write(http.responseBody);
f.savetofile("%2$s", 2);
}
#pragma __endtext
Try
hb_vfClose( hb_vfTempFile( @cScript,,, ".tmp" ) )
IF hb_MemoWrit( cScript, ;
hb_StrFormat( cJS, ;
cURL, ;
StrTran( cFileName, "\", "\\" ) ) )
cProgram:="cscript" + ;
" //nologo" + ;
" /e:jscript" + ;
" " + FNameEscape( cScript )
Execute(cProgram)
ENDIF
hb_vfErase( cScript )
lSuccess := .T.
Catch
lSuccess := .F.
End
RETURN lSuccess
STATIC FUNCTION FNameEscape( cFileName )
RETURN '"' + cFileName + '"'
FUNCTION Execute(cProgram, lEspera)
LOCAL oShell, lOk := .T., nStyle
hb_Default(@lEspera,.T.)
TRY
oShell:=WIN_OleCreateObject( "WScript.Shell" )
CATCH
TRY
oShell:=WIN_OleCreateObject( "WScript.Shell" )
CATCH
lOk := .F.
END
END
IF lOk
TRY
/*
intWindowStyle
Description
0 Hides the window and activates another window.
1 Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2 Activates the window and displays it as a minimized window.
3 Activates the window and displays it as a maximized window.
4 Displays a window in its most recent size and position. The active window remains active.
5 Activates the window and displays it in its current size and position.
6 Minimizes the specified window and activates the next top-level window in the Z order.
7 Displays the window as a minimized window. The active window remains active.
8 Displays the window in its current state. The active window remains active.
9 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10 Sets the show-state based on the state of the program that started the application.
*/
nStyle:=0
//oShell:Run("sumatrapdf.exe -print-to-default -reuse-instance -lang pt "+cSource, nStyle, lEspera)
//oShell:Run("sumatrapdf.exe -print-to "+'"'+cPrinter+'"'+" -reuse-instance -lang pt "+cSource, nStyle, lEspera)
oShell:Run(cProgram, nStyle, lEspera)
CATCH
MsgStop("Erro executando "+cProgram, "Erro")
lOk := .F.
END
oShell:=Nil
ENDIF
RETURN lOk
Código: Selecionar todos
cTeste:=oHTTP:responsebody
IF ValType( cTeste ) == "C"
hb_MemoWrit( cArq, cTeste )
ELSE
nHandle := fCreate( cArq )
FOR EACH nAscii IN cTeste // FOR nCont = 1 TO Len( oHttp:ResponseBody )
fWrite( nHandle, Chr(nAscii) )
NEXT
FClose( nHandle )
ENDIF