Jump to content
Sign in to follow this  

HTTP Upload

Recommended Posts


I all, I've been reading and reading... and I'm sure this can't be all this hard.

I'd like to upload a file via http which will be recieved and managed using PHP.

I'm guessing the best way would be to use the WinHTTP Functions. (http://www.autoitscript.com/forum/index.php?showtopic=84133&st=0)

I get the besics of this, infact I've already written a program to send a post request and then used it's reply. But how do I send a whole file?

So the basic script is this...

#include "WinHTTP.au3"

$hOpen = _WinHttpOpen()

$hConnect = _WinHttpConnect($hOpen, "msdn.microsoft.com")

$hRequest = _WinHttpOpenRequest($hConnect, "GET", "en-us/library/aa384101(VS.85).aspx")



If _WinHttpQueryDataAvailable($hRequest) Then
    $header = _WinHttpQueryHeaders($hRequest)
    MsgBox(0, "Header", $header)




What do I modify to get this sending a file??

The open request should be POST and not guest. And I'll need _WinHttpWriteData($h_openRequest, "somedata=something").

Can you help. (I tried to keep this post simple for everyone)

Share this post

Link to post
Share on other sites


Maybe this can help

#include <HTTP.au3>
$host = "www.testbed.loc"
$port = "443"
$page = "test_script.php"
$vars = "action=upload"

$url = $page&"?"&_HTTPEncodeString($vars)
$socket = _HTTPConnect($host, $port)
$get = _HTTPPost_File($host,$url,$socket,"testuploadfile3.doc", "uploadedfile")
$recv = _HTTPRead($socket,1)
MsgBox(0, "Info", "Data received:" & @CRLF & $recv[4] & @CRLF)

Func _HTTPPost_File($host, $page, $socket = -1, $file = "", $fieldname = "")
    Dim $command
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
        $socket = $_HTTPLastSocket
    $contenttype = _HTTPPost_contenttype($file)
; Maybe this can be done easier/better?
    $boundary = "------"&Chr(Random(Asc("A"), Asc("Z"), 3))&Chr(Random(Asc("a"), Asc("z"), 3))&Chr(Random(Asc("A"), Asc("Z"), 3))&Chr(Random(Asc("a"), Asc("z"), 3))&Random(1, 9, 1)&Random(1, 9, 1)&Random(1, 9, 1)
    $fileopen = FileOpen($file, 0); Open in read only mode
    $fileread = FileRead($fileopen)
    $extra_commands = "--"&$boundary&@CRLF
    $extra_commands &= "Content-Disposition: form-data; name="""&$fieldname&"""; filename=""" & $file & """" &@CRLF
    $extra_commands &= "Content-Type: "&$contenttype&@CRLF&@CRLF
    $extra_commands &= $fileread&@CRLF
    $extra_commands &= "--"&$boundary&"--"
    Dim $datasize = StringLen($extra_commands)
    $command = "POST "&$page&" HTTP/1.1"&@CRLF
    $command &= "Host: " &$host&@CRLF
    $command &= "User-Agent: "&$_HTTPUserAgent&@CRLF
    $command &= "Connection: close"&@CRLF
    $command &= "Content-Type: multipart/form-data; boundary="&$boundary&@CRLF
    $command &= "Content-Length: "&$datasize&@CRLF&@CRLF
    $command &= $extra_commands
;debug info
    If FileExists("output.txt") Then FileDelete("output.txt")
    $file = FileOpen("output.txt", 1)
    FileWrite($file, $command)
;end debug info
    Dim $bytessent = TCPSend($socket, $command)
    If $bytessent == 0 Then
        return 0
    Return $bytessent

Func _HTTPPost_contenttype($file = "")
    $fileextension = StringRight($file,4)
; I blieve these are the only 2 types that matter when uploading
    Switch $fileextension
        Case ".txt"
            $contenttype = "text/plain"
        Case Else
            $contenttype = "application/octet-stream"
    Return $contenttype

First find the HTTP.au3 in the forum.



Share this post

Link to post
Share on other sites

Ok, I've given it a go, and all I seem to get is a "400 Bad Request (Invalid URL)"

I've simplified it down to this untill I get it working.

#include <HTTP.au3>
$host = "intranet"
$port = "80"
$page = ""
$vars = "action=upload"

$url = $page&"?"&_HTTPEncodeString($vars)
$socket = _HTTPConnect($host, $port)
$post = _HTTPPOST($host, $page, $socket)
$recv = _HTTPRead($socket,1)

Any thoughts?

Share this post

Link to post
Share on other sites


Well duh, look at the time this thread was posted, a few years ago.

Go for a better more current examples, but no cookie handling, also, looking at the example you proably tested, of course that's going to get errors, that website doesn't even exist.

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.