Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 09/08/2018 in all areas

  1. trancexx

    WinHTTP functions

    The other day mikeytown2 posted one post in HTTP UDF's thread that got me thinking if there is better (different) method to send requests through the HTTP protocol to HTTP servers. There is Winhttp.dll that ships with windows and that is its main purpose. I couldn't find any examples of using this dll in AutoIt, so I came up with this. Microsoft about Windows HTTP Services: Microsoft Windows HTTP Services (WinHTTP) provides developers with an HTTP client application programming interface (API) to send requests through the HTTP protocol to other HTTP servers... .. blah, blah, and so on... This is an example of getting page header: #include "WinHttp.au3" Opt("MustDeclareVars", 1) ; Open needed handles Local $hOpen = _WinHttpOpen() Local $hConnect = _WinHttpConnect($hOpen, "msdn.microsoft.com") ; Specify the reguest: Local $hRequest = _WinHttpOpenRequest($hConnect, Default, "en-us/library/aa384101(VS.85).aspx") ; Send request _WinHttpSendRequest($hRequest) ; Wait for the response _WinHttpReceiveResponse($hRequest) Local $sHeader = _WinHttpQueryHeaders($hRequest) ; ...get full header ; Clean _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) ; Display retrieved header MsgBox(0, "Header", $sHeader)Everything you need to be able to use this UDF can be found at WinHttp site. Remember, basic understanding of the HTTP protocol is important to use this interface. ProgAndy, trancexx WinHttp.au3 is completely free and no one has right to charge you for it. That's very important. If you feel WinHttp.au3 was helpful to you and you wish to support my further work you can donate to my personal account via PayPal address: trancexx at yahoo dot com I will appreciate that very much. Thank you in advance! :kiss:
    1 point
  2. If you ever tried to download some remote file from your script using InetGet(), InetRead() or other download functions, you probably noticed that the download speed was never as good as when you try to download the same file using a download manager. But that's over, you won't lack the high speed download capability of download managers in your AutoIt scripts, not anymore. Enjoy: #include-once #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <WinHttp.au3> ;http://www.autoitscript.com/forum/topic/84133-winhttp-functions/ #include <FileConstants.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: UrlDownloadEx ; Description ...: Download a single file by splitting it in segments and using several simultaneous connections to download these segments from a single server. ; Syntax ........: UrlDownloadEx($sUrl [, $sFileName = Default [, $iNumberOfConnections = Default [, $CallbackFunction = Default [, $vParam = 0]]]]) ; Parameters ....: $sUrl - Url of the file to download. ; $sFileName - [optional] Local filename to download to. Default is "". ; $iNumberOfConnections - [optional] Number of simultaneous connections. Default is 8. ; $CallbackFunction - [optional] An application-defined callback function to call when progress is made. Default is Null. ; $vParam - [optional] An application-defined value to be passed to the callback function. Default is 0. ; Return values .: Success - Returns a binary string if file name is not supplied. ; - Returns 1 if file name is supplied ; - @extended receives the number of received bytes. ; Failure - Returns 0 and sets @error: ; |1 - Invalid number of connections ; |2 - Invalid callback function ; |3 - Invalid url ; |4 - _WinHttpOpen failed ; |5 - _WinHttpConnect failed ; |6 - _WinHttpOpenRequest failed ; |7 - _WinHttpSendRequest failed ; |8 - _WinHttpReceiveResponse failed ; |9 - _WinHttpQueryHeaders failed ; |10 - _WinHttpOpenRequest failed, while trying to create multiple request ; |11 - _WinHttpQueryHeaders failed, while trying to prepare multiple request ; |12 - Not enough memory to allocate buffer ; |13 - _WinHttpQueryDataAvailable failed ; |14 - Download aborted, callback function returned False ; |15 - FileOpen failed ; |16 - FileWrite failed ; Author ........: FaridAgl ; Remarks .......: The Url parameter should be in the form "http://www.somesite.com/path/file.html", just like an address you would type into your web browser. ; + Please use a high number of connections with caution. Some servers may have specific limitations for a number of connections from one client, when the number of connections is high, the servers can put your computer to a black list. ; + For some connection types or for low performance computers or routers, the download speed may decrease when you increase the number of connections. ; + The callback function is called with the following parameters: $iReceivedBytes, $iTotalReceivedBytes, $iDownloadSize, $vParam ; + To continue downloading, the callback function must return True; to stop downloading, it must return False. ; Example .......: Yes ; =============================================================================================================================== Func UrlDownloadEx($sUrl, $sFileName = Default, $iNumberOfConnections = Default, $CallbackFunction = Default, $vParam = 0) If ($sFileName = Default Or $sFileName = -1) Then $sFileName = "" If ($iNumberOfConnections = Default Or $iNumberOfConnections = -1) Then $iNumberOfConnections = 8 ElseIf (IsInt($iNumberOfConnections) = 0 Or $iNumberOfConnections < 1) Then Return SetError(1, 0, 0) EndIf If ($CallbackFunction = Default Or $CallbackFunction = -1) Then $CallbackFunction = Null Else If (IsFunc($CallbackFunction) = 0) Then Return SetError(2, 0, 0) EndIf Local $avUrlComponents = _WinHttpCrackUrl($sUrl, $ICU_DECODE) If ($avUrlComponents = 0) Then Return SetError(3, 0, 0) Local Const $SERVER_NAME = $avUrlComponents[2] Local Const $OBJECT_NAME = $avUrlComponents[6] Local Const $SERVER_PORT = $avUrlComponents[3] Local Const $FLAGS = ($avUrlComponents[1] = $INTERNET_SCHEME_HTTPS) ? ($WINHTTP_FLAG_SECURE) : (0) Local $hSession = _WinHttpOpen("Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))", $WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, $WINHTTP_NO_PROXY_NAME, $WINHTTP_NO_PROXY_BYPASS, 0) If ($hSession = 0) Then Return SetError(4, 0, 0) Local $hConnect = _WinHttpConnect($hSession, $SERVER_NAME, $SERVER_PORT) If ($hConnect = 0) Then _WinHttpCloseHandle($hSession) Return SetError(5, 0, 0) EndIf Local $hRequest = _WinHttpOpenRequest($hConnect, "GET", $OBJECT_NAME, 0, $WINHTTP_NO_REFERER, $WINHTTP_DEFAULT_ACCEPT_TYPES, $FLAGS) If ($hRequest = 0) Then _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(6, 0, 0) EndIf If (_WinHttpSendRequest($hRequest, "Range: bytes=0-0", $WINHTTP_NO_REQUEST_DATA, 0, 0) = 0) Then _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(7, 0, 0) EndIf If (_WinHttpReceiveResponse($hRequest) = 0) Then _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(8, 0, 0) EndIf Local $sStatusCode = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_STATUS_CODE, $WINHTTP_HEADER_NAME_BY_INDEX, $WINHTTP_NO_HEADER_INDEX) If (Int($sStatusCode, 1) <> $HTTP_STATUS_PARTIAL_CONTENT) Then _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(9, 0, 0) EndIf Local $sContentRange = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_RANGE, $WINHTTP_HEADER_NAME_BY_INDEX, $WINHTTP_NO_HEADER_INDEX) _WinHttpCloseHandle($hRequest) Local Enum $REQUEST_HANDLE, $RECEIVED_BYTES, $DOWNLOAD_OFFSET, $BYTES_TO_DOWNLOAD Local $avConnections[$iNumberOfConnections][4] Local $iDownloadSize = Int(StringTrimLeft($sContentRange, StringLen("bytes 0-0/")), 1) Local $iDownloadSizePerConnection = Floor($iDownloadSize / $iNumberOfConnections) Local $iLastByteToDownload = 0 For $i = 0 To $iNumberOfConnections - 1 $avConnections[$i][$REQUEST_HANDLE] = _WinHttpOpenRequest($hConnect, "GET", $OBJECT_NAME, 0, $WINHTTP_NO_REFERER, $WINHTTP_DEFAULT_ACCEPT_TYPES, $FLAGS) If ($avConnections[$i][$REQUEST_HANDLE] = 0) Then For $j = $i - 1 To 0 Step -1 _WinHttpCloseHandle($avConnections[$j][$REQUEST_HANDLE]) Next _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(10, 0, 0) EndIf $avConnections[$i][$RECEIVED_BYTES] = 0 $avConnections[$i][$DOWNLOAD_OFFSET] = $i * $iDownloadSizePerConnection If ($i <> $iNumberOfConnections - 1) Then $avConnections[$i][$BYTES_TO_DOWNLOAD] = $iDownloadSizePerConnection $iLastByteToDownload = $avConnections[$i][$DOWNLOAD_OFFSET] + $iDownloadSizePerConnection - 1 Else $avConnections[$i][$BYTES_TO_DOWNLOAD] = $iDownloadSizePerConnection + Mod($iDownloadSize, $iNumberOfConnections) $iLastByteToDownload = $iDownloadSize - 1 EndIf _WinHttpSendRequest($avConnections[$i][$REQUEST_HANDLE], "Range: bytes=" & $avConnections[$i][$DOWNLOAD_OFFSET] & "-" & $iLastByteToDownload, $WINHTTP_NO_REQUEST_DATA, 0, 0) _WinHttpReceiveResponse($avConnections[$i][$REQUEST_HANDLE]) $sStatusCode = _WinHttpQueryHeaders($avConnections[$i][$REQUEST_HANDLE], $WINHTTP_QUERY_STATUS_CODE, $WINHTTP_HEADER_NAME_BY_INDEX, $WINHTTP_NO_HEADER_INDEX) If (Int($sStatusCode, 1) <> $HTTP_STATUS_PARTIAL_CONTENT) Then For $j = $i - 1 To 0 Step -1 _WinHttpCloseHandle($avConnections[$j][$REQUEST_HANDLE]) Next _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(11, 0, 0) EndIf Next Local $tBuffer = DllStructCreate("BYTE[" & $iDownloadSize & "]") If (@error) Then Return SetError(12, 0, 0) Local $pBuffer = DllStructGetPtr($tBuffer) Local $iTotalReceivedBytes = 0 While (True) For $i = 0 To $iNumberOfConnections - 1 If ($avConnections[$i][$RECEIVED_BYTES] = $avConnections[$i][$BYTES_TO_DOWNLOAD]) Then ContinueLoop If (_WinHttpQueryDataAvailable($avConnections[$i][$REQUEST_HANDLE]) = 1) Then $iTotalReceivedBytes += @extended _WinHttpReadData($avConnections[$i][$REQUEST_HANDLE], 2, @extended, $pBuffer + $avConnections[$i][$DOWNLOAD_OFFSET] + $avConnections[$i][$RECEIVED_BYTES]) $avConnections[$i][$RECEIVED_BYTES] += @extended If ($CallbackFunction <> Null) Then If ($CallbackFunction(@extended, $iTotalReceivedBytes, $iDownloadSize, $vParam) = False) Then For $j = 0 To $iNumberOfConnections - 1 If ($avConnections[$j][$REQUEST_HANDLE] > 0) Then _WinHttpCloseHandle($avConnections[$j][$REQUEST_HANDLE]) Next _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(14, 0, 0) EndIf EndIf If ($avConnections[$i][$RECEIVED_BYTES] = $avConnections[$i][$BYTES_TO_DOWNLOAD]) Then _WinHttpCloseHandle($avConnections[$i][$REQUEST_HANDLE]) $avConnections[$i][$REQUEST_HANDLE] = 0 EndIf Else For $j = 0 To $iNumberOfConnections - 1 If ($avConnections[$j][$REQUEST_HANDLE] > 0) Then _WinHttpCloseHandle($avConnections[$j][$REQUEST_HANDLE]) Next _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) Return SetError(13, 0, 0) EndIf Next If ($iTotalReceivedBytes = $iDownloadSize) Then _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) ExitLoop EndIf WEnd If ($sFileName <> "") Then Local $hFile = FileOpen($sFileName, BitOR($FO_OVERWRITE, $FO_CREATEPATH, $FO_BINARY)) If ($hFile = -1) Then Return SetError(15, 0, 0) If (FileWrite($hFile, DllStructGetData($tBuffer, 1)) = 0) Then FileClose($hFile) Return SetError(16, 0, 0) EndIf FileClose($hFile) Return SetError(0, $iDownloadSize, 1) EndIf Return SetError(0, $iDownloadSize, DllStructGetData($tBuffer, 1)) EndFunc Example 1 - Download to a local file and use callback function to monitor and control the download progress: #include "UrlDownloadEx.au3" Global $t1 = TimerInit() Global $vResult = UrlDownloadEx("http://www.autoitscript.com/files/autoit3/autoit-v3-setup.exe", @ScriptDir & "\AutoIt 3.3.12.0.exe", Default, BytesReceived) ConsoleWrite(StringFormat("Result:\t%s\nSize:\t%u\nError:\t%u\nTimer:\t%u\n", $vResult, @extended, @error, TimerDiff($t1))) Func BytesReceived($iReceivedBytes, $iTotalReceivedBytes, $iDownloadSize, $vParam) If ($iTotalReceivedBytes >= 2 * 1024 * 1024) Then Return False ;Stop downloading ConsoleWrite(StringFormat("%u bytes received.\n%u/%u\nParam: %s\n\n", $iReceivedBytes, $iTotalReceivedBytes, $iDownloadSize, $vParam)) Return True ;Continue downloading EndFunc Example 2 - Download to memory and set the number of connections to 2: #include "UrlDownloadEx.au3" Global $t1 = TimerInit() Global $vResult = UrlDownloadEx("http://icdn4.digitaltrends.com/image/microsoft_xp_bliss_desktop_image-650x0.jpg", Default, 2) ConsoleWrite(StringFormat("Result:\t%s\nSize:\t%u\nError:\t%u\nTimer:\t%u\n", $vResult, @extended, @error, TimerDiff($t1))) You can also download all of the above scripts at once: UrlDownloadEx + Examples.zip
    1 point
  3. Afaren, Welcome to the AutoIt forums. I suggest reading the Managing Multiple GUIs tutorial in the Wiki to understand how to get your various GUIs playing nicely with each other. If you still have problems, then post your code so we can take a look - see here how to do it. M23
    1 point
  4. lucamad, Just add Opt("GUIResizeMode", 102) at the top of your script. You can use the Help file to work out why 102 is the number to use! M23
    1 point
×
×
  • Create New...