Jump to content

WinINet.au3 (FTP/HTTP/HTTPS/Gopher+)


-Ultima-
 Share

Recommended Posts

Sorry, I won't be able to answer your question until I find some free time to play around with the functions again (I've been a tad bit busy as of late).

its ok ive put this project to the side till its fixed :) no hurry thnx for the initial UDF tho

Link to comment
Share on other sites

  • 3 weeks later...

@ultima

Could you show us an example how to download a file from a web server, with download progress?

I hope your schedule offers you some time to continue.

I haven't gotten very far.. :P

#include "WinINet.au3"

Global $hInternetOpen, $hInternetConnect, $hHttpOpenRequest, $hHttpSendRequest
Global $iStartup, $iShutdown, $iInternetAttemptConnect 

; start WinINet libary
$iStartup = _WinINet_Startup()
ConsoleWriteLog("_WinINet_Startup", @error, @extended, $iStartup)

; attempt a connection
$iInternetAttemptConnect = _WinINet_InternetAttemptConnect()
ConsoleWriteLog("_WinINet_InternetAttemptConnect", @error, @extended, $iInternetAttemptConnect)

; successful attempt
If $iInternetAttemptConnect Then

    ; open a connection
    $hInternetOpen = _WinINet_InternetOpen("Mozilla/5.0 Firefox/3.0.1", $INTERNET_OPEN_TYPE_DIRECT, 0, Default, Default)
    ConsoleWriteLog("_WinINet_InternetOpen", @error, @extended, $hInternetOpen)

    ; connect to a web server
    $hInternetConnect = _WinINet_InternetConnect($hInternetOpen, $INTERNET_SERVICE_HTTP, "mozilla.org", 0, 0, Default, Default, 0)
    ConsoleWriteLog("_WinINet_InternetConnect", @error, @extended, $hInternetConnect)
    
    ; set up a request a file
    $hHttpOpenRequest  = _WinINet_HttpOpenRequest($hInternetConnect, "GET", "images/feature-logos2.png", $INTERNET_FLAG_RELOAD, "HTTP/1.1", Default, Default, 0)
    ConsoleWriteLog("_WinINet_HttpOpenRequest", @error, @extended, $hHttpOpenRequest)

    ; send the request to the http server
    $hHttpSendRequest = _WinINet_HttpSendRequest($hHttpOpenRequest, Default, Default)
    ConsoleWriteLog("_WinINet_HttpSendRequest", @error, @extended, $hHttpSendRequest)
    
    ;_WinINet_HttpQueryInfo($hHttpOpenRequest, $iInfoLevel, $iIndex, $iBufferSize = 2048)
EndIf

; end WinINet library
$iShutdown = _WinINet_Shutdown()
ConsoleWriteLog("_WinINet_Shutdown", @error, @extended, $iShutdown)


Func ConsoleWriteLog($vMessage, $iError, $vExtended, $vReturn)
    ConsoleWrite(StringFormat("+ %s: error: %s extended: %s return: %s \r", $vMessage, $iError, $vExtended, $vReturn))
EndFunc
Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

Seems awesome but what's the difference between that and other protocols?

My full Query: http://www.autoitscript.com/forum/index.php?showtopic=82517

[u]My Au3 Scripts:[/u]____________(E)Lephant, A Share download manager (RS/MU etc)Http1.1 Console, The Ez Way!Internet Reconnection Automation Suite & A Macro Recording Tool.SK's Alarm Clock, Playing '.MP3 & .Wav' Files._________________Is GOD a mistake of the Humanity Or the Humanity is a mistake of GOD ?!

Link to comment
Share on other sites

Hello Ultima - I have a question about using your fine functions here, I want to check the expiration date of a SSL-certificate on a remote server, first I tried the CAPICOM dll, but so far I have only been able to check local certificates using that.

In MSDN documentation, I see that WinINet has a struct called INTERNET_CERTIFICATE_INFO - I'm not sure how to use/access this struct, or if it's even possible using your functions?

Also I'm not even sure if this can be used for the purpose I had in mind, but I'm hoping you can shed some light on that?

You can see my attempts so far here: http://www.autoitscript.com/forum/index.ph...mp;#entry592517

Link to comment
Share on other sites

Anyone know why this code doesn't work? I can get it to connect to the URL but it isn't returning any content. Just trying to get the OpenUrl function to connect to any HTTPS site and read its data.

#include "WinINet.au3"

DllOpen("wininet.dll")

$internet = _WinINet_InternetOpen("Mozilla")
If $internet == 0 Then
    MsgBox(0, "Error", "Setup Error")
    Exit
EndIf

$url = "https://someserver.com"

$internetconnect = _WinINet_InternetOpenURL($internet, $url)
If @error Then 
    MsgBox(0, "Error", @error)
    Exit
EndIf

$httprequest = _WinINet_HttpOpenRequest($internetconnect, "GET", "/")

$sendrequest = _WinINet_HttpSendRequest($httprequest)

$readfile = Binary("")
Do
    $readfile &= _WinINet_InternetReadFile($httprequest, 5000)
Until @error Or Not @extended

MsgBox(0, "Content", binarytostring($readfile))
Link to comment
Share on other sites

Hi everyone!

I'm trying to use this UDF to retrieve the source code of a page that requires basic http authentication

This is the code i use so far:

#include <WinINet.au3>

$s_UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3"

$b_WinINET_Startup = _WinINet_Startup()
$h_internet = _WinINet_InternetOpen($s_UserAgent)
$h_Connect = _WinINet_InternetConnect($h_internet, $INTERNET_SERVICE_HTTP, "http://networkspy", 80, "", "[b]USERNAME[/b]", "[b]PASSWORD[/b]")
$h_OpenURL = _WinINet_InternetOpenUrl($h_internet, "http://networkspy/map.asp?map=UKBranches.wup&MapViewMode=Summary")


local $readfile = binary("")
do
    $readfile &= _WinINet_InternetReadFile($h_OpenURL, 5000)
until @error Or Not @extended

msgbox(0, "Content", binarytostring($readfile))

The page it retrieves, though, is the one that I normally get after 3 failed authentication attempts, so I guess i'm not properly sending the username & password to the server.

Can anyone tell me what I'm doing wrong & how to correct it, please?

Many thanks!

PS: thanks for the UDF, Ultima -it would have taken me ages to read&understand the MSDN wininet documentation :P

Link to comment
Share on other sites

@Everyone: Sorry I haven't been responding. I'm still trying to find free time wherever I can, but my schedule is tight, what with exams and all. I'll try to get back to you as soon as possible :P

@oren: Please clarify?

@mc83: The server should not have "http://" in it when calling _WinINet_InternetConnect. As well, I don't believe you can use _WinINet_InternetOpenUrl in conjunction with _WinINet_InternetConnect. If you need HTTP authentication, then you need to use InternetConnect with HttpOpenRequest/HttpSendRequest.

@SoulA: You're not supposed to be using a handle returned by _WinINet_InternetOpenURL with _WinINet_HttpOpenRequest... Read the function header carefully :(

#include "WinINet.au3"

DllOpen("wininet.dll")

$internet = _WinINet_InternetOpen("Mozilla")
If $internet == 0 Then
    MsgBox(0, "Error", "Setup Error")
    Exit
EndIf

$url = "http://www.google.com"

$internetconnect = _WinINet_InternetOpenURL($internet, $url)
If @error Then 
    MsgBox(0, "Error", @error)
    Exit
EndIf

$readfile = Binary("")
Do
    $readfile &= _WinINet_InternetReadFile($internetconnect, 5000)
Until @error Or Not @extended

MsgBox(0, "Content", binarytostring($readfile))

@mrRevoked: Taking your example...

#include <WinINet.au3>

Global $hInternetOpen, $hInternetConnect, $hHttpOpenRequest, $hHttpSendRequest
Global $iStartup, $iShutdown, $iInternetAttemptConnect 

; start WinINet libary
$iStartup = _WinINet_Startup()
ConsoleWriteLog("_WinINet_Startup", @error, @extended, $iStartup)

; attempt a connection
$iInternetAttemptConnect = _WinINet_InternetAttemptConnect()
ConsoleWriteLog("_WinINet_InternetAttemptConnect", @error, @extended, $iInternetAttemptConnect)

; successful attempt
If $iInternetAttemptConnect Then

    ; open a connection
    $hInternetOpen = _WinINet_InternetOpen("Mozilla/5.0 Firefox/3.0.1", $INTERNET_OPEN_TYPE_DIRECT, 0, Default, Default)
    ConsoleWriteLog("_WinINet_InternetOpen", @error, @extended, $hInternetOpen)

    ; connect to a web server
    $hInternetConnect = _WinINet_InternetConnect($hInternetOpen, $INTERNET_SERVICE_HTTP, "mozilla.org", 0, 0, Default, Default, 0)
    ConsoleWriteLog("_WinINet_InternetConnect", @error, @extended, $hInternetConnect)
    
    ; set up a request a file
    $hHttpOpenRequest  = _WinINet_HttpOpenRequest($hInternetConnect, "GET", "images/feature-logos2.png", $INTERNET_FLAG_RELOAD, "HTTP/1.1", Default, Default, 0)
    ConsoleWriteLog("_WinINet_HttpOpenRequest", @error, @extended, $hHttpOpenRequest)

    ; send the request to the http server
    $hHttpSendRequest = _WinINet_HttpSendRequest($hHttpOpenRequest, Default, Default)
    ConsoleWriteLog("_WinINet_HttpSendRequest", @error, @extended, $hHttpSendRequest)

    ; get the file size
    $iLength = Default
    $avLength = _WinINet_HttpQueryInfo($hHttpOpenRequest, BitOR($HTTP_QUERY_CONTENT_LENGTH, $HTTP_QUERY_FLAG_NUMBER), 0)

    If Not @error Then
        ; file size specified in the content header
        ConsoleWriteLog("_WinINet_HttpQueryInfo", @error, @extended, "[" & DllStructGetPtr($avLength[0]) & ", " & $avLength[1] & "]")

        ; retrieve content length
        $iLength = DllStructGetData(DllStructCreate("dword", DllStructGetPtr($avLength[0])), 1)
        ConsoleWrite("--> Length of file: " & $iLength & " bytes" & @CRLF)
    EndIf

    ; set progressbar up only if file size known
    If $iLength <> Default Then
        ProgressOn("File Download", "Downloading the requested file...", StringFormat("%.2f%% (%d / %d bytes)", 0, 0, $iLength))
    EndIf

    ; download the file into $vReceived (binary data)
    $fCompleted = False
    $iReceivedSize = 0
    $vReceived = Binary("")
    Do
        $vReceived &= _WinINet_InternetReadFile($hHttpOpenRequest, 1024)
        $fCompleted = @error Or Not @extended

        ; update the progress bar
        If $iLength <> Default Then
            $iReceivedSize += @extended
            $iPercent = 100 * ($iReceivedSize / $iLength)
            ProgressSet($iPercent, StringFormat("%.2f%% (%d / %d bytes)", $iPercent, $iReceivedSize, $iLength))

            ; artificial delay so we can see the progressbar move
            Sleep(1000)
        EndIf
    Until $fCompleted
EndIf

; end WinINet library
$iShutdown = _WinINet_Shutdown()
ConsoleWriteLog("_WinINet_Shutdown", @error, @extended, $iShutdown)


Func ConsoleWriteLog($vMessage, $iError, $vExtended, $vReturn)
    ConsoleWrite(StringFormat("+ %s: error: %s extended: %s return: %s \r", $vMessage, $iError, $vExtended, $vReturn))
EndFunc
Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

  • 3 weeks later...

Ultima,

I am trying to send a POST request to my server with AutoIT.

I am using Markus21's code (seen here) as my initial test.

The request is correctly sent as POST, but the variables are not being correctly passed, eg they show up blank when I have my server echo them out.

Do you have any thoughts as to why this is happening or suggestions on how to fix it?

Thank you! :mellow:

Link to comment
Share on other sites

You shouldn't add data as Header, but sned it as Optional data in _WinINet_HttpSendRequest

#include "WinINet.au3"

DllOpen("wininet.dll")

$internet = _WinINet_InternetOpen("Mozilla")
If $internet == 0 Then
    MsgBox(0, "Error", "Setup Error")
EndIf
$ip = "www.snee.com"
$port = "80"
$internetconnect = _WinINet_InternetConnect($internet, $INTERNET_SERVICE_HTTP, $ip, $port)

If @error Then MsgBox(0, "Error", "Error 2")


$data = "fname=bla&lname=blub"  ; Data encoded
$type = "Content-Type: application/x-www-form-urlencoded"&@CRLF
$agent = "User-Agent: Mozilla/4.0"&@CRLF

$newdata = $agent & $type

$httprequest = _WinINet_HttpOpenRequest($internetconnect, "POST", "/xml/crud/posttest.cgi?REQUEST_URI=works%20too" )
$addheader = _WinINet_HttpAddRequestHeaders($httprequest, $newdata, $HTTP_ADDREQ_FLAG_ADD)
ConsoleWrite($addheader & @CRLF)

$sendrequest = _WinINet_HttpSendRequest($httprequest,Default,StringToBinary($data))
local $readfile = Binary("")
Do
$readfile &= _WinINet_InternetReadFile($httprequest, 5000)
Until @error<>0 Or Not @extended
MsgBox(0, "Content", binarytostring($readfile))
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

This is birlliant stuff.

I just want to report a minor issue:

$tagSYSTEMTIME already exists in StructureConstants.au3 , which is included in Date.au3 for example.

So this causes a conflict when including Date.au3 in a script.

Global Const : WinINetConstants.au3(29,136) : ERROR: $tagSYSTEMTIME previously declared as a 'Const'

Edited by uteotw
Link to comment
Share on other sites

So delete this Structure and include StructureConstants.au3 instaed OR rename it to$ __WinInet_tagSYSTEMTIME :mellow:

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

  • 4 weeks later...

Hi, I'm trying to logging in to secure site:

Func GetTraf2()

    DllOpen("wininet.dll")
    
    $data = "user%5Blogin%5D=some_username&user%5Bpassword%5D=some_password"
    $type = "Content-Type: application/x-www-form-urlencoded" & @CRLF
    $agent = "User-Agent: Mozilla/5.0" & @CRLF
    $newdata = $agent & $type

    $internet = _WinINet_InternetOpen("Mozilla")
    If $internet == 0 Then
        MsgBox(0, "Error in Mozilla", "Setup Error")
        Exit
    EndIf
    
    $internetconnect = _WinINet_InternetConnect($internet, $INTERNET_SERVICE_HTTP, "https://some_site.ru")
    If @error Then
        MsgBox(0, "Error in internetconnect", @error)
        Exit
    EndIf
    
    $HTTPRequest = _WinINet_HttpOpenRequest($internetconnect, "POST", "/login", $INTERNET_FLAG_KEEP_CONNECTION, "HTTP/1.1", "https://some_site.ru/login")
    $addheader = _WinINet_HttpAddRequestHeaders($HTTPRequest, $newdata, $HTTP_ADDREQ_FLAG_ADD)
    ConsoleWrite($addheader & @CRLF)
    $sendrequest = _WinINet_HttpSendRequest($HTTPRequest, Default, StringToBinary($data))
    
    Local $readfile = Binary("")
    Do
        $readfile &= _WinINet_InternetReadFile($HTTPRequest, 5000)
    Until @error<>0 Or Not @extended
    
    MsgBox(2, "Content", BinaryToString($readfile))

EndFunc  ;==>GetTraf2

But after execution this function I only see empty Content msg box :). Wireshark shows nothing activity.

Link to comment
Share on other sites

I was doing a search and found one of your older posts -ultima- so i really need some help on understanding I how i can do this.

Uploading via HTTP on websites is done through POST with Multipart Content-Type. I managed to get this working via my WinINet UDFs just last night. The basic gist is that you...

1) Generate the Content-Type header field as described by RFC1341 (boundaries, content type, content length)

2) Combine the Content-Type header field with the file data, and the boundary again to form the request body

3) Send via InternetOpen -> InternetConnect -> HttpOpenRequest -> HttpSendRequest with the request body and the relevant request headers (different from the Content-Type header field) using POST

It's not simple, but it works.

I want to make a Desktop Application with AutoIT so i can Batch Upload Videos in .flv, .wmv format to BLIP.TV. They have an API where they describe on how do this so i will post the directions and see if the WININET is what i need to accomplish this or maybe some advice on what road to take.

CODE

Purpose

To allow programmatic uploads to blip.tv in the simplest way possible.

[edit]

API URL

* http://blip.tv/file/post/

* http://uploads.blip.tv/

While both URLs will currently work, future applications should use uploads.blip.tv.

[edit]

Request

Send a multipart POST (Content-Type: multi-part/form-data) with these fields:

POST http://www.blip.tv/ (Content-Type: multi-part/form-data)

BLIP Examples

Upload a new episode

To upload a new episode, send a multipart post with these fields:

Field Name Value

post "1"

skin "api"

userlogin "yourusername"

password "YoUrPwD"

file file to upload

title "New File"

any help appreciated becasue i really have no idea where to start at..

so any help pointing me in the right direction or small example..

Edited by subfighter
Link to comment
Share on other sites

Here, just threw this together in the last few minutes:

_WinINet_POSTUpload.au3

Local $sHost = ""
Local $sPath = ""
Local $iPort = 0
Local $sUser = ""
Local $sPass = ""

Local $asFormData[1][2] = [ _
    ["field_name", "value"] _
]

Local $asFiles[1][2] = [ _
    ["file_field_name", "file_path"] _
]

Local $vReceived = _WinINet_POSTUpload($asFormData, $asFiles, $sHost, $sPath, $iPort, $sUser, $sPass)
If @error Then
    ConsoleWrite("Error Code: " & @error & @CRLF)
Else
    ConsoleWrite(BinaryToString($vReceived) & @CRLF)
EndIf

I'm not careful about MIME types in that function, but oh well. Anyhow, I guess for your purposes, $asFormData and $asFiles would be as follows:

Local $asFormData[5][2] = [ _
    ["post", "1"], _
    ["skin", "api"], _
    ["userlogin", "yourusername"], _
    ["password", "YoUrPwD"], _
    ["title", "New File"] _
]

Local $asFiles[1][2] = [ _
    ["file", "file_path"] _
]

You can probably leave out the $sUser and $sPass parameters when calling the function, as the form data already seems to hold it (don't take my word for it though -- experiment, as it depends on the server setup and serverside script).

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

-ultima- thanks so much for the example as your genius as i got it to work on the a file an it uploaded it.. :) so here is what you posted on the other page as it worked and uploaded the files..

this is new to me as i do not understand exactly how it works :o but i want to eventually understand it enough to build a GUI where i can Batch Upload files from my computer.

and i just need to understand the working parts that i need.. my question is there is a list of others FIELDNAMES(KEYS) that i can send like description, keywords, format, channel,etc.. So i do not understand what the 5 and 2 mean here as this would be place i would add the other fields i would imagine. I can see the 5 must represent the total number or FIELDS since if count them there 5 so my guess its the total fields that are being sent. But i do not understand what the 2 means?? And i may be wrong about the 5.. :D

Edited by Jon
Link to comment
Share on other sites

-ultima- can i get some help again as i already have many videos i uploaded and I can EDIT the videos without having to upload a VIDEO FILE first. So i tried tried to create a new function _WinINet_POSTEdit based off the example you provided. And tried to remove the $asFiles variables and the parts in the function i thought dealed with it uploading the file.. But I tried a couple of the different ways but kept getting errors. :)

i would appreciate if you could make a _WinINet_POSTEdit function which basically lets me update the video files i already have uploaded on blip.tv.. I guess just need the same function like the example you provide but do not need it the upload a a file part..

any help appreciated thanks. here is the short list of basic info need to be sent to edit an exisiting video

Edit the title of an existing episode

To modify the title on an existing episode, send a multipart post with these fields:

Field Name | Value

===========================

post | "1"

skin | "api"

userlogin | "yourusername"

password | "YoUrPwD"

id | "123456"

title | "New Title"

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...