AutoHotkey Community

It is currently May 26th, 2012, 9:00 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: July 10th, 2008, 12:00 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
I wrapped the WinInet.dll functions to be StdLib compatible.

The WININET lib includes the functionality above as well as the FTP functions that were spread across the forum. This allows a single include that can be utilized for HTTP and FTP communication.

edit:
WININET_UrlGetContents() now Supports SSL.

edit:
Changed name to just UrlGetContents as it is not really a WININET function.

edit:
added api

Here it is :
Code:
; ==========
; Origional FTP Functions by Olfen & Andreone
; See the following post:
; http://www.autohotkey.com/forum/viewtopic.php?t=10393
; ==========
; WININET_UrlGetContents() is based on heresy & DerRaphael's script here:
; http://www.autohotkey.com/forum/viewtopic.php?t=33506
; ==========
; by ahklerner
; ==========


WININET_Init(){
   global
   WININET_hModule := DllCall("LoadLibrary", "Str", "WinInet.Dll")
}

WININET_UnInit(){
   global
   DllCall("FreeLibrary", "UInt", WININET_hModule)
}

WININET_InternetOpenA(lpszAgent,dwAccessType=1,lpszProxyName=0,lpszProxyBypass=0,dwFlags=0){
   ;http://msdn.microsoft.com/en-us/library/aa385096(VS.85).aspx
   return DllCall("WinINet\InternetOpenA"
            , "Str"      ,lpszAgent
            , "UInt"   ,dwAccessType
            , "Str"      ,lpszProxyName
            , "Str"      ,lpszProxyBypass
            , "Uint"   ,dwFlags )
}

WININET_InternetConnectA(hInternet,lpszServerName,nServerPort=80,lpszUsername=""
               ,lpszPassword="",dwService=3,dwFlags=0,dwContext=0){
   ;http://msdn.microsoft.com/en-us/library/aa384363(VS.85).aspx
   ; INTERNET_SERVICE_FTP = 1
   ; INTERNET_SERVICE_HTTP = 3
   return DllCall("WinINet\InternetConnectA"
            , "uInt"   ,hInternet
            , "Str"      ,lpszServerName
            , "Int"      ,nServerPort
            , "Str"      ,lpszUsername
            , "Str"      ,lpszPassword
            , "uInt"   ,dwService
            , "uInt"   ,dwFlags
            , "uInt*"   ,dwContext )
}

WININET_HttpOpenRequestA(hConnect,lpszVerb,lpszObjectName,lpszVersion="HTTP/1.1"
            ,lpszReferer="",lplpszAcceptTypes="",dwFlags=0,dwContext=0){
   ;http://msdn.microsoft.com/en-us/library/aa384233(VS.85).aspx
   return DllCall("WinINet\HttpOpenRequestA"
            , "uInt"   ,hConnect
            , "Str"      ,lpszVerb
            , "Str"      ,lpszObjectName
            , "Str"      ,lpszVersion
            , "Str"      ,lpszReferer
            , "Str"      ,lplpszAcceptTypes
            , "uInt"   ,dwFlags
            , "uInt"   ,dwContext )
}

WININET_HttpSendRequestA(hRequest,lpszHeaders="",lpOptional=""){
   ;http://msdn.microsoft.com/en-us/library/aa384247(VS.85).aspx
   return DllCall("WinINet\HttpSendRequestA"
            , "uInt"   ,hRequest
            , "Str"      ,lpszHeaders
            , "uInt"   ,Strlen(lpszHeaders)
            , "Str"      ,lpOptional
            , "uInt"   ,Strlen(lpOptional) )
}

WININET_InternetReadFile(hFile){
   ;http://msdn.microsoft.com/en-us/library/aa385103(VS.85).aspx
   dwNumberOfBytesToRead := 1024**2
   VarSetCapacity(lpBuffer,dwNumberOfBytesToRead,0)
   VarSetCapacity(lpdwNumberOfBytesRead,4,0)
   Loop {
      if DllCall("wininet\InternetReadFile","uInt",hFile,"uInt",&lpBuffer
            ,"uInt",dwNumberOfBytesToRead,"uInt",&lpdwNumberOfBytesRead ) {
         VarSetCapacity(lpBuffer,-1)
         TotalBytesRead := 0
         Loop, 4
            TotalBytesRead += *(&lpdwNumberOfBytesRead + A_Index-1) << 8*(A_Index-1)
         If !TotalBytesRead
            break
         Else
            Result .= SubStr(lpBuffer,1,TotalBytesRead)
      }
   }
   return Result
}


WININET_FtpCreateDirectoryA(hConnect,lpszDirectory) {
   ;http://msdn.microsoft.com/en-us/library/aa384136(VS.85).aspx
   r := DllCall("wininet\FtpCreateDirectoryA", "uint", hConnect, "str", lpszDirectory)
   If (ErrorLevel or !r)
      Return 0
   else
      Return 1
   }


WININET_FtpRemoveDirectoryA(hConnect,lpszDirectory) {
   ;http://msdn.microsoft.com/en-us/library/aa384172(VS.85).aspx
   r := DllCall("wininet\FtpRemoveDirectoryA", "uint", hConnect, "str", lpszDirectory)
   If (ErrorLevel or !r)
      Return 0
   else
      Return 1
   }

WININET_FtpSetCurrentDirectoryA(hConnect,lpszDirectory) {
   ;http://msdn.microsoft.com/en-us/library/aa384178(VS.85).aspx
   r := DllCall("wininet\FtpSetCurrentDirectoryA", "uint", hConnect, "str", lpszDirectory)
   If (ErrorLevel or !r)
      Return 0
   else
      Return 1
   }

WININET_FtpPutFileA(hConnect,lpszLocalFile, lpszNewRemoteFile="", dwFlags=0,dwContext=0) {
   ;http://msdn.microsoft.com/en-us/library/aa384170(VS.85).aspx
   If lpszNewRemoteFile =
      lpszNewRemoteFile := lpszLocalFile
   r := DllCall("wininet\FtpPutFileA"
         , "uint"   , hConnect
         , "str"      , lpszLocalFile
         , "str"      , lpszNewRemoteFile
         , "uint"   , dwFlags
         , "uint"   , dwContext )
   If (ErrorLevel or !r)
      Return 0
   else
      Return 1
   }

WININET_FtpGetFileA(hConnect,lpszRemoteFile, lpszNewFile="", fFailIfExists=1
            ,dwFlagsAndAttributes=0,dwFlags=0,dwContext=0) {
   ;http://msdn.microsoft.com/en-us/library/aa384157(VS.85).aspx
   If lpszNewFile=
      lpszNewFile := lpszRemoteFile
   r := DllCall("wininet\FtpGetFileA"
         , "uint"   , hConnect
         , "str"      , lpszRemoteFile
         , "str"      , lpszNewFile
         , "int"      , fFailIfExists
         , "uint"   , dwFlagsAndAttributes
         , "uint"   , dwFlags
         , "uint"   , dwContext )
   If (ErrorLevel or !r)
   Return 0
   else
   Return 1
}

WININET_FtpOpenFileA(hConnect,lpszFileName,dwAccess=0x80000000,dwFlags=0,dwContext=0){
   ;http://msdn.microsoft.com/en-us/library/aa384166(VS.85).aspx
   Result := DllCall("wininet\FtpOpenFileA"
         , "uint"   , hConnect
         , "str"      , lpszFileName
         , "uint"   , dwAccess
         , "uint"   , dwFlags
         , "uint"   , dwContext   ) ;dwContext
   If (ErrorLevel or !r)
      Return -1
   Return Result
}

WININET_InternetCloseHandle(hInternet){
   DllCall("wininet\InternetCloseHandle"
         ,  "UInt"   , hInternet   )
}

WININET_FtpGetFileSize(hFile,lpdwFileSizeHigh=0) {
   ;http://msdn.microsoft.com/en-us/library/aa384159(VS.85).aspx
   return DllCall("wininet\FtpGetFileSize"
               , "uint"   , hFile
               , "uint"   , lpdwFileSizeHigh   )
}

WININET_FtpDeleteFileA(hConnect,lpszFileName) {
   ;http://msdn.microsoft.com/en-us/library/aa384142(VS.85).aspx
   r :=  DllCall("wininet\FtpDeleteFileA"
               , "uint"   , hConnect
            , "str"      , lpszFileName)
   If (ErrorLevel or !r)
      Return 0
   else
      Return 1
   }

WININET_FtpRenameFileA(hConnect,lpszExisting, lpszNew) {
   ;http://msdn.microsoft.com/en-us/library/aa384175(VS.85).aspx
   r := DllCall("wininet\FtpRenameFileA"
            , "uint"   , hConnect
            , "str"      , lpszExisting
            , "str"      , lpszNew   )
   If (ErrorLevel or !r)
      Return 0
   else
      Return 1
}

WININET_FindFirstFile(hConnect, lpszSearchFile, ByRef lpFindFileData,dwFlags=0,dwContext=0) {
   ;http://msdn.microsoft.com/en-us/library/aa384146(VS.85).aspx
   SIZE_OF_WIN32_FIND_DATA := ( 4 + (3*8) + (4*4) + (260*4) + (14*4) )
   VarSetCapacity(lpFindFileData, SIZE_OF_WIN32_FIND_DATA, 0)
   hFind := DllCall("wininet\FtpFindFirstFileA"
         , "uint"   , hConnect
         , "str"   , lpszSearchFile
         , "uint"   , &lpFindFileData
         , "uint"   , dwFlags
         , "uint"   , dwContext )

   If(!hFind)
      VarSetCapacity(lpFindFileData, 0)
   Return hFind
}

WININET_FindNextFile(hFind, ByRef lpvFindData) {
;http://msdn.microsoft.com/en-us/library/aa384698(VS.85).aspx
   Return DllCall("wininet\InternetFindNextFileA"
      , "uint", hFind
      , "uint", &lpvFindData)
}


UrlGetContents():
Code:
UrlGetContents(sUrl,sUserName="",sPassword="",sPostData="",sUserAgent="Autohotkey"){
   ; based on heresy & DerRaphael's script here:
   ; http://www.autohotkey.com/forum/viewtopic.php?t=33506
   
   ;default ports
   INTERNET_INVALID_PORT_NUMBER    = 0
   INTERNET_DEFAULT_HTTP_PORT       = 80
   INTERNET_DEFAULT_HTTPS_PORT    = 443
   
   ;ssl flags
   INTERNET_FLAG_SECURE                = 0x00800000
   SECURITY_FLAG_IGNORE_UNKNOWN_CA       = 0x00000100
   SECURITY_FLAG_IGNORE_CERT_CN_INVALID    = 0x00001000
   SECURITY_FLAG_IGNORE_CERT_DATE_INVALID    = 0x00002000
   
   SplitPath,sURL,sFileName,sPath,sExt,sNoExt,sServer
   sProtocol := (RegExMatch(sServer,"(^http://|^ftp://|^https://)",pr)) ? pr1 : "unknown"
   if (sProtocol = "http://"){
      nPort := INTERNET_DEFAULT_HTTP_PORT
      dwFlags = 0
   } else if (sProtocol = "https://") {
      nPort := INTERNET_DEFAULT_HTTPS_PORT
      dwFlags := (INTERNET_FLAG_SECURE|SECURITY_FLAG_IGNORE_CERT_CN_INVALID
         |SECURITY_FLAG_IGNORE_CERT_DATE_INVALID|SECURITY_FLAG_IGNORE_UNKNOWN_CA)
   } else {
      nPort := INTERNET_INVALID_PORT_NUMBER
      dwFlags = 0
   }      
   sPath := RegExReplace(sPath,"\Q" sServer "\E")
   sServerName := (sProtocol!="unknown") ? RegExReplace(sServer,"\Q" sProtocol "\E") : sServer
   sObjectName := sPath . "/" . sFileName
   hInternet := WININET_InternetOpenA(sUserAgent)
   hConnect := WININET_InternetConnectA(hInternet,sServerName,nPort,sUserName,sPassword)
   if sPostData
      sHTTPVerb:="POST", sHeaders:="Content-Type: application/x-www-form-urlencoded"
   else
      sHTTPVerb:="GET", sHeaders:=""
   sVersion:="HTTP/1.1", sReferer:="", sAcceptTypes:=""
   hRequest := WININET_HttpOpenRequestA(hConnect,sHTTPVerb,sObjectName,sVersion,sReferer,sAcceptTypes,dwFlags)
   if WININET_HttpSendRequestA(hRequest,sHeaders,sPostData)
      sData := WININET_InternetReadFile(hRequest)
   WININET_InternetCloseHandle(hRequest)
   WININET_InternetCloseHandle(hInternet)
   WININET_InternetCloseHandle(hConnect)
   return sData
}

A couple test scripts:
Code:
;Get Web Page Contents Test
WININET_Init()
MsgBox % UrlGetContents("http://www.autohotkey.net")
WININET_UnInit()
return

Code:
;FTP Upload-Download Test
WININET_Init()
OnExit, CleanUp

sHost = autohotkey.net
nPort = 21
sUsername = YourUserName
sPassword = YourPassword
sLocalFile = C:\test_word_document.doc
sRemoteFile = /test_word_document.doc
sNewLocalFile = C:\new_test_word_document.doc
fFailIfExists := True ; False to overwrite existing file on download

INTERNET_OPEN_TYPE_DIRECT    = 1
INTERNET_SERVICE_FTP       = 1

; establish connection to FTP server
if hInternet := WININET_InternetOpenA(A_ScriptName,INTERNET_OPEN_TYPE_DIRECT)
   hConnect := WININET_InternetConnectA(hInternet,sHost,nPort,sUserName,sPassword,INTERNET_SERVICE_FTP)
If (ErrorLevel or !hConnect) {
   MsgBox, Error connecting to FTP server.
   ExitApp
   }
MsgBox Connected to Server

;Upload File
if !WININET_FtpPutFileA(hConnect,sLocalFile, sRemoteFile){
   MsgBox, Error uploading file
   ExitApp
   }
MsgBox Upload Complete

; download file
if !WININET_FtpGetFileA(hConnect,sRemoteFile,sNewLocalFile, fFailIfExists){
   MsgBox, Error downloading file
   ExitApp
   }
MsgBox Download Complete

ExitApp

CleanUp:
MsgBox Exiting.
WININET_InternetCloseHandle(hConnect)
WININET_InternetCloseHandle(hInternet)
WININET_UnInit()
ExitApp

api

Code:
/* api for wininet.ahk
WININET_FindNextFile( hFind, lpvFindData )
WININET_FindFirstFile( hConnect, lpszSearchFile, lpFindFileData [ , dwFlags, dwContext ] )
WININET_FtpRenameFileA( hConnect, lpszExisting, lpszNew )
WININET_FtpDeleteFileA( hConnect, lpszFileName )
WININET_FtpGetFileSize( hFile [ , lpdwFileSizeHigh ] )
WININET_InternetCloseHandle( hInternet )
WININET_FtpOpenFileA( hConnect, lpszFileName [ , dwAccess, dwFlags, dwContext ] )
WININET_FtpGetFileA( hConnect, lpszRemoteFile [ , lpszNewFile, fFailIfExists, dwFlagsAndAttributes, dwFlags, dwContext ] )
WININET_FtpPutFileA( hConnect, lpszLocalFile [ , lpszNewRemoteFile, dwFlags, dwContext ] )
WININET_FtpSetCurrentDirectoryA( hConnect, lpszDirectory )
WININET_FtpRemoveDirectoryA( hConnect, lpszDirectory )
WININET_FtpCreateDirectoryA( hConnect, lpszDirectory )
WININET_InternetReadFile( hFile )
WININET_HttpSendRequestA( hRequest [ , lpszHeaders, lpOptional ] )
WININET_HttpOpenRequestA( hConnect, lpszVerb, lpszObjectName [ , lpszVersion, lpszReferer, lplpszAcceptTypes, dwFlags, dwContext ] )
WININET_InternetConnectA( hInternet, lpszServerName [ , nServerPort, lpszUsername, lpszPassword, dwService, dwFlags, dwContext ] )
WININET_InternetOpenA( lpszAgent [ , dwAccessType, lpszProxyName, lpszProxyBypass, dwFlags ] )
WININET_UnInit( )
WININET_Init( )
*/

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Last edited by ahklerner on March 17th, 2011, 7:58 pm, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2008, 5:53 pm 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
example for http authentication:
Code:
;Get Web Page Contents Test
;with username and password

WININET_Init()
MsgBox % UrlGetContents("http://www.someserver.com","UserName","Password")
WININET_UnInit()
return


example for ssl:
Code:
;Get Web Page Contents Test
;with ssl

WININET_Init()
MsgBox % UrlGetContents("https://mail.google.com/mail")
WININET_UnInit()
return

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Last edited by ahklerner on July 11th, 2008, 1:51 am, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 11th, 2008, 1:45 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
SSL now works in UrlGetContents()

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Good
PostPosted: July 11th, 2008, 10:15 am 
Offline

Joined: September 15th, 2006, 10:25 am
Posts: 567
Good work :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 14th, 2008, 8:22 am 
Offline

Joined: March 11th, 2008, 11:36 pm
Posts: 291
Nice. it's time to wrap winsock too :lol:
Thanks for wrapping it ahklerner

_________________
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 15th, 2008, 4:55 pm 
Offline

Joined: November 1st, 2007, 10:03 pm
Posts: 885
Can you wrap the Wireless API for me?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 16th, 2008, 1:02 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
sorry, as I said before, I dont have an interest in it.

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 16th, 2008, 1:41 am 
Offline

Joined: November 1st, 2007, 10:03 pm
Posts: 885
Just a few functions?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 16th, 2008, 2:10 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
well i was (am) bored.
http://www.autohotkey.com/forum/viewtopic.php?t=33726

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 24th, 2008, 2:40 am 
Offline

Joined: July 12th, 2008, 5:39 pm
Posts: 3
Thanks for the wrapper. One question about setting the referer.

Can I just pass do this?

Code:
UrlGetContents(sUrl,sUserName="",sPassword="",sPostData="",sUserAgent="Autohotkey",sReferer="someurl")


tia
tae


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 25th, 2008, 10:35 am 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
hi AHKlerner, hi rest

i thought this lil function might be of interest for u, so i just paste it here:

Code:
WinINet_InternetCrackURL(lpszUrl,arrayName="URL")
{ ; v 0.1 / (w) 25.07.2008 by derRaphael / zLib-Style release
   local hModule, offset_name_length
   hModule := DllCall("LoadLibrary", "Str", "WinINet.Dll")

   ; SetUpStructures for URL_COMPONENTS / needed for InternetCrackURL
   ; http://msdn.microsoft.com/en-us/library/aa385420(VS.85).aspx
   offset_name_length:= "4-lpszScheme-255|16-lpszHostName-1024|28-lpszUserName-1024|"
                    . "36-lpszPassword-1024|44-lpszUrlPath-1024|52-lpszExtrainfo-1024"
   VarSetCapacity(URL_COMPONENTS,60,0)
   ; Struc Size               ; Scheme Size                  ; Max Port Number
   NumPut(60,URL_COMPONENTS,0), NumPut(255,URL_COMPONENTS,12), NumPut(0xffff,URL_COMPONENTS,24)
   
   Loop,Parse,offset_name_length,|
   {
      RegExMatch(A_LoopField,"(?P<Offset>\d+)-(?P<Name>[a-zA-Z]+)-(?P<Size>\d+)",iCU_)
      VarSetCapacity(%iCU_Name%,iCU_Size,0)
      NumPut(&%iCU_Name%,URL_COMPONENTS,iCU_Offset)
      NumPut(iCU_Size,URL_COMPONENTS,iCU_Offset+4)
   }

   ; Split the given URL; extract scheme, user, pass, authotity (host), port, path, and query (extrainfo)
   ; http://msdn.microsoft.com/en-us/library/aa384376(VS.85).aspx
   DllCall("WinINet\InternetCrackUrlA","Str",lpszUrl,"uInt",StrLen(lpszUrl),"uInt",0,"uInt",&URL_COMPONENTS)

   ; Update variables to retrieve results
   Loop,Parse,offset_name_length,|
   {
      RegExMatch(A_LoopField,"-(?P<Name>[a-zA-Z]+)-",iCU_)
      VarSetCapacity(%iCU_Name%,-1)

      %arrayName%_%iCU_Name% := % %iCU_Name%
   }
   %arrayName%_nPort:=NumGet(URL_COMPONENTS,24,"uInt")
   DllCall("FreeLibrary", "UInt", hModule)                    ; unload the library
}


Its being used as this:
WinINe_InternetCrackURL(URL [,AHKarrayPrefix])
URL - the url to be cracked this is mandatory
AHKarrayPrefix - a string to indicate which AHKArray-Prefix to use for Output
When no PrefixString is given the function defaults the PrefixString to URL. Following exampleOutputs illustrates how the results 'd look like:

Here are three examples to illustrate the results
assumed URL
http://www.autohotkey.com/forum/viewtop ... 876#206876

URL_lpszScheme -> http
URL_lpszHostName -> www.autohotkey.com
URL_lpszUserName ->
URL_lpszPassword ->
URL_lpszUrlPath -> /forum/viewtopic.php
URL_lpszExtraInfo -> ?p=206876#206876
URL_nPort -> 80

assumed URL
https://www.somesecuresite.com/

URL_lpszScheme -> https
URL_lpszHostName -> www.somesecuresite.com
URL_lpszUserName ->
URL_lpszPassword ->
URL_lpszUrlPath ->
URL_lpszExtraInfo ->
URL_nPort -> 443

assumed URL
https://myusername:secret@www.somesecur ... y#fragment

URL_lpszScheme -> https
URL_lpszHostName -> www.somesecuresite.com
URL_lpszUserName -> myusername
URL_lpszPassword -> secret
URL_lpszUrlPath -> /path/to/file.ext
URL_lpszExtraInfo -> ?query#fragment
URL_nPort -> 12345


below is a lil working example showing its possible usage ... in this case its getting a PathInformation to extract an ObjectName in a quoted URL style URL.

Code:
url := "http://tbn0.google.com/images?q=tbn:-RkbDeCx-wL4pM:http://www.hi-consult-"
     . "online.de/blah/HI%2520logo%25202.JPG"
WinINet_InternetCrackURL(url)
; Dump for further control
MsgBox,0,%url%,% "URL_lpszScheme:`t`t"    URL_lpszScheme    "`n"
       . "URL_lpszHostName:`t"    URL_lpszHostName  "`n"
       . "URL_lpszUserName:`t"    URL_lpszUserName  "`n"
       . "URL_lpszPassword:`t`t"  URL_lpszPassword  "`n"
       . "URL_lpszUrlPath:`t`t"   URL_lpszUrlPath   "`n"
       . "URL_lpszExtraInfo:`t`t" URL_lpszExtraInfo "`n"
       . "URL_nPort:`t`t"         URL_nPort         "`n"
RegExMatch(URL_lpszExtraInfo,"(?P<URL>http.*)",new)
WinINet_InternetCrackURL(newURL)
; Dump for further control
MsgBox,0,%newURL%,% "URL_lpszScheme:`t`t"    URL_lpszScheme    "`n"
       . "URL_lpszHostName:`t"    URL_lpszHostName  "`n"
       . "URL_lpszUserName:`t"    URL_lpszUserName  "`n"
       . "URL_lpszPassword:`t`t"  URL_lpszPassword  "`n"
       . "URL_lpszUrlPath:`t`t"   URL_lpszUrlPath   "`n"
       . "URL_lpszExtraInfo:`t`t" URL_lpszExtraInfo "`n"
       . "URL_nPort:`t`t"         URL_nPort         "`n"
; ATM we have the the Path to our picture above in the variable URL_lpszUrlPath.
; Now we just make exchange all slashes with backslashes, add some drive infotmation
; and let autohotkey handle the rest to get the pure filname
StringReplace,tmpName,URL_lpszUrlPath,/,\,All
tmpName := "C:" tmpName
SplitPath,tmpName,FileName
; the variable FileName now holds the name without any pathinformations
; all we have to do is to uriDecode it and store the decoded result
unescape:
   fileName:=uriDecode(FileName)
   if (RegExMatch(fileName,"%[0-9a-zA-Z][0-9a-zA-Z]"))
      goto unescape
MsgBox % FileName
return

uriDecode(str)
{ ; v 0.2 / (w) 25.07.2008 by derRaphael / zLib-Style release
   Loop,Parse,str,`%
   {
      if (A_Index>1)
         RegExMatch(A_LoopField,"(?P<Hex>..)(?P<rest>.*)",var)
      else
         txt := A_LoopField
      txt .= chr("0x" varHex) varRest
   }
   return txt
}


greets
DerRaphael

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 9th, 2008, 10:01 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
ok so question
this is still a bit deep for me (understanding this well enuff to modify it)
I can use the heck out of it tho except below
but our workplace has a proxy server and i need to use NTLM
http://msdn.microsoft.com/en-us/library/aa383144(VS.85).aspx
im a bit lost when it comes to implementing this into the scripts so far
:( and well i dont seem to have the depth of knowledge to make anything even remotely workable from it

[ Moderator!: MSDN link fixed ]
thanks for the fix
Actually the more in the msdn i read i think i might even be pointed in the wrong direction
basically our proxy uses our Domain login information for the proxy user
In IE we arent prompted but when i use any winhttp function i get blank results
seems like im getting blocked at the proxy for instance
Code:
WININET_Init()
MsgBox % UrlGetContents("http://www.autohotkey.net")
WININET_UnInit()
return
fine at home work pc returns blank
and im able to view this site without restriction on work pc in IE

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 20th, 2008, 1:42 pm 
Offline

Joined: October 11th, 2006, 5:27 am
Posts: 27
Whether probably to receive last-modification date of html page?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2009, 10:19 pm 
Offline

Joined: July 15th, 2006, 6:07 pm
Posts: 254
I've got an application that (tries to) upload content to my Amazon S3 account. I've been trying to use UrlGetContents() but am stymied. See
http://www.autohotkey.com/forum/viewtopic.php?t=44844 (second message.) I'm sure I've got multiple challenges to overcome but the first one is I'm getting a response from the Amazon S3 web page:
Quote:
Bucket POST must be of the enclosure-type multipart/form-data

I've modified your function as I described but I must be doing something wrong as it still fails in the same way. Any suggestions would be greatly appreciated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2009, 5:21 am 
Offline

Joined: November 27th, 2008, 9:44 am
Posts: 62
I modified UrlGetContents slightly so I could add custom headers to the request (it's very slight, just added the bits in red):

Code:
UrlGetContents(sUrl,sUserName="",sPassword="",sPostData="",sHeaders="",sUserAgent="Autohotkey"){
   ; based on heresy & DerRaphael's script here:
   ; http://www.autohotkey.com/forum/viewtopic.php?t=33506
   
   ;default ports
   INTERNET_INVALID_PORT_NUMBER    = 0
   INTERNET_DEFAULT_HTTP_PORT       = 80
   INTERNET_DEFAULT_HTTPS_PORT    = 443
   
   ;ssl flags
   INTERNET_FLAG_SECURE                = 0x00800000
   SECURITY_FLAG_IGNORE_UNKNOWN_CA       = 0x00000100
   SECURITY_FLAG_IGNORE_CERT_CN_INVALID    = 0x00001000
   SECURITY_FLAG_IGNORE_CERT_DATE_INVALID    = 0x00002000
   
   SplitPath,sURL,sFileName,sPath,sExt,sNoExt,sServer
   sProtocol := (RegExMatch(sServer,"(^http://|^ftp://|^https://)",pr)) ? pr1 : "unknown"
   if (sProtocol = "http://"){
      nPort := INTERNET_DEFAULT_HTTP_PORT
      dwFlags = 0
   } else if (sProtocol = "https://") {
      nPort := INTERNET_DEFAULT_HTTPS_PORT
      dwFlags := (INTERNET_FLAG_SECURE|SECURITY_FLAG_IGNORE_CERT_CN_INVALID
         |SECURITY_FLAG_IGNORE_CERT_DATE_INVALID|SECURITY_FLAG_IGNORE_UNKNOWN_CA)
   } else {
      nPort := INTERNET_INVALID_PORT_NUMBER
      dwFlags = 0
   }     
   sPath := RegExReplace(sPath,"\Q" sServer "\E")
   sServerName := (sProtocol!="unknown") ? RegExReplace(sServer,"\Q" sProtocol "\E") : sServer
   sObjectName := sPath . "/" . sFileName
   hInternet := WININET_InternetOpenA(sUserAgent)
   hConnect := WININET_InternetConnectA(hInternet,sServerName,nPort,sUserName,sPassword)
   if sPostData
      sHTTPVerb:="POST", sHeaders:="Content-Type: application/x-www-form-urlencoded" . ((sHeaders) ? ("`n" . sHeaders) : (""))
   else
      sHTTPVerb:="GET", sHeaders:=""
   sVersion:="HTTP/1.1", sReferer:="", sAcceptTypes:=""
   hRequest := WININET_HttpOpenRequestA(hConnect,sHTTPVerb,sObjectName,sVersion,sReferer,sAcceptTypes,dwFlags)
   if WININET_HttpSendRequestA(hRequest,sHeaders,sPostData)
      sData := WININET_InternetReadFile(hRequest)
   WININET_InternetCloseHandle(hRequest)
   WININET_InternetCloseHandle(hInternet)
   WININET_InternetCloseHandle(hConnect)
   return sData
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Stigg, tomoe_uehara and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group