Jump to content
Sign in to follow this  
OverloadUT

HTTP UDF's

Recommended Posts

-Ultima-

Correct, that's a bug I noticed as well. I was in the process of updating these UDFs a few months ago, and managed to attain massive speed improvements over OverloadUT's version, as well as allowing custom headers (so basic HTTP auth would be possible, though an external UDF for Base64-encode the login would be needed). Unfortunately, I ran into some roadblocks (namely, how to keep connections alive in cases where the content length is unknown). This made thigs overly complicated. In the end, I opted to wrap the WinINet UDFs in AutoIt, which turned out to be much more flexible/powerful, not to mention quicker. It also supports HTTPS, has HTTP auth support built in, and basically anthing WinINet can do can now be done with AutoIt.

The only potential problem here is that there's a slightly steeper learning curve, but I'd imagine someone can wrap those functions into a simpler set of APIs/UDFs not too dissimilar to OverloadUT's functions.

Edited by -Ultima-

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

Share this post


Link to post
Share on other sites
RyGoTypE

i tried everythinkg i could think of to connect to my website and login with the specified username/password entered into the gui.

still no luck connecting to my site.

i also wasnt able to connect through sql either. reason i made the switch over from sql to http is safer for the website. but the only problem with http is its more unsafe for the client. cookies create an issue storing usernames and passwords in the search bar.

someone with a hex editor could break down the code and get the information for the username and login of my database on the site.

so either way its not looking super fantastic. but id much rather login via http.

i wish i could get this working properly!

Share this post


Link to post
Share on other sites
oren

Wll it was hard to find this post..

I remember i once sow it...

But i could not find it...

i started building my script with ObjCreate("winhttp.winhttprequest.5.1")

and now i've found it maybe i'll rebuild..

Anyone know whats better? ObjCreate("winhttp.winhttprequest.5.1") OR include http.au3

?

Share this post


Link to post
Share on other sites
Mattraks

Thank you for this. This really saved my life!

Share this post


Link to post
Share on other sites
Apzo

Well...

I've used a simple example:

CODE
$host = "My.server.com"

$page = "directory/res.php"

$vars = "cid=" & $cid & "&data=" & $data

$vars = _HTTPEncodeString($vars)

$socket = _HTTPConnect($host)

$post = _HTTPPost($host, $page, $socket, $vars)

$recv = _HTTPRead($socket, 1)

_ArrayDisplay($recv)

This is the error I got :

CODE
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>400 Bad Request</title>

</head><body>

<h1>Bad Request</h1>

<p>Your browser sent a request that this server could not understand.<br />

</p>

<hr>

I tried some changes to $vars (starting with a '?'), $page, but still nothing.

Can someone drop here a working post example ??

Thanks in advance

Apzo.

Share this post


Link to post
Share on other sites
ProgAndy

You mustn't encode the whole string :mellow: Just the Variables...

$vars = "cid=" & _HTTPEncodeString($cid) & "&data=" & _HTTPEncodeString($data)


*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

Share this post


Link to post
Share on other sites
Kademlia

Hey, just tested your code.

You got bad performance for _HTTPRead

As you are using TCPRecv($socket,16) on line 243. This causes very bad transfer speeds.

By using 8192 or more the code works well even if you download big files.

Missing: _HTTPReadToFile() ; as AutoIt will 'crash' if you download files > 20MiB

anyway gread work helped me alot!

Share this post


Link to post
Share on other sites
mikeytown2

Changed _HTTPPost() so it can connect to a server that requires a user/pass

Func _HTTPPost($host, $page, $socket = -1, $data = "", $user = "", $pass = "")
    Dim $command
    
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    Dim $datasize = StringLen($data)
    
    $command = "POST " & $page & " HTTP/1.1" & @CRLF
    $command &= "Host: " & $host & @CRLF
    $command &= "User-Agent: " & $_HTTPUserAgent & @CRLF
    $command &= "Connection: close" & @CRLF
    If $user <> "" And $pass <> "" Then
        $command &= "Authorization: Basic " & _Base64Encode($user & ":" & $pass)
    EndIf
    $command &= "Content-Type: application/x-www-form-urlencoded" & @CRLF
    $command &= "Content-Length: " & $datasize & @CRLF
    $command &= "" & @CRLF
    $command &= $data & @CRLF
    
    Dim $bytessent = TCPSend($socket, $command)
    
    If $bytessent == 0 Then
        SetExtended(@error)
        SetError(2)
        Return 0
    EndIf
    
    SetError(0)
    Return $bytessent
EndFunc   ;==>_HTTPPost

Using this Base64Encode function http://www.autoitscript.com/forum/index.php?showtopic=81332

Share this post


Link to post
Share on other sites
Sercankd

i need this udf someone please reupload it :S

Share this post


Link to post
Share on other sites
SmOke_N

i need this udf someone please reupload it :S

First post tells you where to get it :)http://www.autoitscript.com/forum/index.ph...st&p=319354

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Joennuh

How to work around proxy-servers?

I want my application to fetch some plain text from a PHP script, but this UDF couldn't create a socket when it has to connect through a proxy-server.

Edited by Joennuh

Share this post


Link to post
Share on other sites
noidea

I could really use this UDF as well, but don't see a link in the thread anywhere. Can someone repost?

Share this post


Link to post
Share on other sites
scriptguru

I've added ability to send files through HTTP POST (multipart/form-data)

Should I post my function here?

Also I've added function that works equal to Javascript global function encodeURIComponent()

Share this post


Link to post
Share on other sites
seesoe

so this can be used to read the post data of a page being sent out?

Share this post


Link to post
Share on other sites
scriptguru

this can be used to upload files to server

for example - to take screenshot and upload it to server

Share this post


Link to post
Share on other sites
random

I've added ability to send files through HTTP POST (multipart/form-data) Should I post my function here?

Yes, please!

this is what i was looking when i found this topic. thank you.

Share this post


Link to post
Share on other sites
scriptguru

Yes, please!

this is what i was looking when i found this topic. thank you.

Note that it requires Base64.au3

; ===================================================================
; _HTTPPost_files($host, $page, [$socket], [$data], [$filenames])
;
; Executes a POST request on an open socket.
; Parameters:
;   $host - IN - The hostname you want to get the page from. This should be in the format "www.google.com" or "localhost"
;   $page - IN - The the file you want to get. This should always start with a slash. Examples: "/" or "/somedirectory/submitform.php"
;   $socket - OPTIONAL IN - The socket opened by _HTTPConnect. If this is not supplied, the last socket opened with _HTTPConnect will be used.
;   $data - array of data to send in the post request. This should first be run through _HTTPEncodeString()
;   $filenames - array of paths for files to send
; Returns:
;   The number of bytes sent in the request.
; Author: Val Polyakh <scriptguru@gmail.com>
; Requires: Base64.au3
; Remarks:
;   Possible @errors:
;   1 - No socket supplied and no current socket exists
;   2 - Error sending to socket. Check @extended for Windows API WSAGetError return
; ===================================================================
Func _HTTPPost_files($host, $page, $socket, $data, $filenames)
    
    Local $b="---------------------------0123456789012"
    Local $fh,$image,$str,$picdata,$fieldname, $arr, $header
    Local $command
    
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    $command=""
;$command &= $data&@CRLF
    
    For $i=0 To (UBound($data)-1)
        $arr=StringSplit($data[$i],"=",2)
        $command &= "--"& $b &@CRLF &"Content-Disposition: form-data; name="& $arr[0] & @CRLF&@CRLF & $arr[1] &@CRLF
        
    Next
    
    For $i=0 To (UBound($filenames)-1)
        $arr=StringSplit($filenames[$i],"\",2);get filename
        $fieldname=$arr[UBound($arr)-1]
        $arr=StringSplit($fieldname,".",2);chop extension
        $fieldname=$arr[0]
        $fh=FileOpen($filenames[$i], 16)
        $image=FileRead($fh)
        FileClose($fh)
        $str=_Base64Encode($image)
        $command &="--"& $b _
            &@CRLF&"Content-Disposition: form-data; name=" & $fieldname &"; filename="& $filenames[$i] _
            &@CRLF&"Content-Type: application/upload" _
            &@CRLF&"Content-Transfer-Encoding: base64"&@CRLF&@CRLF _
            &$str &@CRLF
    Next

    $command &= @CRLF&"--"& $b &"--"&@CRLF

    Dim $datasize = StringLen($command)
    $header = "POST "&$page&" HTTP/1.1"&@CRLF
    $header &= "Host: " &$host&@CRLF
    $header &= "User-Agent: "&$_HTTPUserAgent&@CRLF
    $header &= "Connection: close"&@CRLF
    $header &= "Content-Type: multipart/form-data; boundary=" & $b & @CRLF
    $header &= "Content-Length: "&$datasize&@CRLF
    $header &= ""&@CRLF

    $command= $header & $command
    Dim $bytessent = TCPSend($socket, $command)
    
    If $bytessent == 0 Then
        SetExtended(@error)
        SetError(2)
        return 0
    EndIf
    
    SetError(0)
    Return $bytessent
    
EndFunc

One more thing you should know is format of data you need to send.

For example if your data in url-encoded format looks like xxx=1&yyy=2&zzz=3 then you should pass to this function array as follows ["xxx=1","yyy=2","zzz=3"]

Edited by scriptguru

Share this post


Link to post
Share on other sites
random

scriptguru, I'm trying to use your function but something doesnt work.

have a look at my case:

i have apache + php installed correctly (other php apps currently running on the server)

a php page to receive the uploded file : uploader.php

a html page with form to test uploading through browser: form.html

the upload works. I find the uploaded file inside the "/upload" folder.

now, i want to code an AU3 script UPLOADER.au3, to send the file without using browser.

uploader.php

<%
$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['filename']['name']); 

if( move_uploaded_file($_FILES['filename']['tmp_name'], $target_path) ) {
    echo "The file ".  basename( $_FILES['filename']['name']). 
    " has been uploaded";
    
    echo "<p>Parameter ..... P= " . htmlspecialchars($_POST['p']); 
} 
else{
    echo "There was an error uploading the file, please try again!";
    }
%>

form.html

<form enctype="multipart/form-data"   action="uploader.php"   method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="200000000" />
P = <input type="text" name="p" value="1" /><p>
Choose a file to upload: <input name="filename" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

the UPLOADER.au3 so far:

#include <Base64.au3>
#include <_HTTP.au3>
#include <_HTTPPost_files.au3>
#NoTrayIcon
$BYTES=0
$host='localhost'
$port=80
$page='/php_file_upload/uploader.php'

$SOCKET = _HTTPConnect($host, $port)    ; #1
dim $data[1]
dim $filenames[1]

$data[0] = _HTTPEncodeString( "p=1000000" )

$filenames[0] = "C:\Tools\Autoit\alex\UPLOADER\hello2.exe" ; this is a very small file 73 bytes
; being .exe has nothing to do. i tried with .zip, txt, and other types with no success

$BYTES= _HTTPPost_files($host, $page, $socket, $data, $filenames ) ; #2

$closeit = _HTTPClose($socket)  ; #3

the script executes, ..... #1, #2, #3 return success codes , no @error, no @extended

the $BYTES variable ends with some value > 0

but the file is not uploaded into "/uploads"

Share this post


Link to post
Share on other sites
kenunloaded

scriptguru, I'm trying to use your function but something doesnt work.

have a look at my case:

i have apache + php installed correctly (other php apps currently running on the server)

a php page to receive the uploded file : uploader.php

a html page with form to test uploading through browser: form.html

the upload works. I find the uploaded file inside the "/upload" folder.

now, i want to code an AU3 script UPLOADER.au3, to send the file without using browser.

uploader.php

<%
$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['filename']['name']); 

if( move_uploaded_file($_FILES['filename']['tmp_name'], $target_path) ) {
    echo "The file ".  basename( $_FILES['filename']['name']). 
    " has been uploaded";
    
    echo "<p>Parameter ..... P= " . htmlspecialchars($_POST['p']); 
} 
else{
    echo "There was an error uploading the file, please try again!";
    }
%>

form.html

<form enctype="multipart/form-data"   action="uploader.php"   method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="200000000" />
P = <input type="text" name="p" value="1" /><p>
Choose a file to upload: <input name="filename" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

the UPLOADER.au3 so far:

#include <Base64.au3>
#include <_HTTP.au3>
#include <_HTTPPost_files.au3>
#NoTrayIcon
$BYTES=0
$host='localhost'
$port=80
$page='/php_file_upload/uploader.php'

$SOCKET = _HTTPConnect($host, $port); #1
dim $data[1]
dim $filenames[1]

$data[0] = _HTTPEncodeString( "p=1000000" )

$filenames[0] = "C:\Tools\Autoit\alex\UPLOADER\hello2.exe"; this is a very small file 73 bytes
; being .exe has nothing to do. i tried with .zip, txt, and other types with no success

$BYTES= _HTTPPost_files($host, $page, $socket, $data, $filenames ); #2

$closeit = _HTTPClose($socket) ; #3

the script executes, ..... #1, #2, #3 return success codes , no @error, no @extended

the $BYTES variable ends with some value > 0

but the file is not uploaded into "/uploads"

which base64.au3 file is it.

Share this post


Link to post
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
Sign in to follow this  

×