Jump to content

HTTP UDF's


OverloadUT
 Share

Recommended Posts

  • 2 weeks later...

I can post example here but your code seems to be OK - I don't know where is the trouble.

Local $data="url="& euc($url) &"&title=" & euc($title) & "&tags=" & $tags & "&access=" & $access & "&body=" & euc($cbd) & "&DMSID=" & $session
;alert($data)
    Local $filenames
    Local $socket=_HTTPConnect($deepmemo_host)
    If $make_shot Then
        _ScreenCapture_Capture("screenshot.png", 0, 0, -1, -1, false)
        Dim $filenames[1]=["screenshot.png"]
        _HTTPPost_files($deepmemo_host, $add_path, $socket, StringSplit($data,"&",2), $filenames)
    Else
        _HTTPPost($deepmemo_host, $add_path, $socket, $data)
    EndIf
    Local $response=_HTTPRead($socket, 0)

It is working for me.

Maybe the problem in absolute/relative file local path?

Also the problem maybe in server side (I'm not proficient in server-side programming so I cant give any advices).

BTW now I working on proxy support for this UDF.

When it will be done I'll upload whole UDF here.

Link to comment
Share on other sites

  • 1 month later...

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"

Maybe you have already resolved the problem

To make it work with your example, I did the following changes in _HTTPPost_function:

For $i=0 To (UBound($filenames)-1)

$arr=StringSplit($filenames[$i],"\",2);get filename

$fieldname=$arr[uBound($arr)-1]

$fileName = $fieldname ; keep trace short original file name

$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=filename; filename="& $fileName _

&@CRLF&"Content-Type: application/upload" _

&@CRLF&"Content-Transfer-Encoding: base64"&@CRLF&@CRLF _

&$str &@CRLF

Next

In uploader.php, the id for file upload is a fixed value as 'filename', so should it be in the post request

After that, the upload works for me, but I just got the Base64 encoded string in the file. I will study that (I'm quite new in http protocol)

Link to comment
Share on other sites

  • 4 weeks later...

The script works well on my Windows XP in French but the function _HTTPRead doesn't work on Windows XP Chinese. Some ideas ?

I found that the error was from TCPRecv($socket,16): I compared the packets received in Chinese Windows with the packets received in French Windows, some packets were wrong, sometimes there was missing byte, sometimes there was modified byte. I changed the receive length (the whole file can be held in) to TCPRecv($socket,2048) and it was OK

I did the test with Wireshark as tracer: the trace in Wireshark was OK

Link to comment
Share on other sites

  • 2 weeks later...
  • 5 months later...

As per post above, I'd love it if someone could give us a POST example. I've tried, but with no luck.

#include <http.au3>
#include <array.au3>

$host = "www"
$port = "80"
$page = "/test/post.php?test_get=got"
$post_data = _HTTPEncodeString("data1=123")

$socket = _HTTPConnect($host, $port)
$get_req = _HTTPGet($host, $page, $socket)
$post_req = _HTTPPost($host, $page, $socket, $post_data)
$returned_data = _HTTPRead($socket)


Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send($returned_data)

When I run the above:

1. the GET variables work

2. The POST don't work

3. It only returns 7 characters of the page. I've tried changing the first line of the return to different charicters but it doesn't make and difference. Strangely though, if you set the page to a wrong location you'll get the full out put of a 404 error. This same problem occurs both when returning as an array and a string.

Please help, I feel I'm so close! (Is this a false hope)

Link to comment
Share on other sites

Hi,

I got the same problem - POST isn't working for me ether.

It's annoying, the HTTP POST doesn't seem to be that difficult but I still can't get it to work.

Does anybody have a working solution for this? I don't want to use IE.au3 though.

Thanks

Ronny

Link to comment
Share on other sites

  • 2 weeks later...

Using this UDF to download .torrent files finds an interesting issue.

As .torrent files have part text, part binary content trying to _HTTPRead them will mix strings with binary data.

This is due to the way TCPRecv handles data.

For backwards compatibility reasons this function will try to return strings by default. If null (0x00) characters are received then the return value will be a binary type

To fix this issue you can use the following code

If $recv <> "" Then
            $bytesreceived = $bytesreceived + StringLen($recv)
            $timer = TimerInit()
            If IsBinary($recv) Then
                $data &= BinaryToString($recv, 0)
            Else
                $data &= $recv
            EndIf
;~          ConsoleWrite("Bytes downloaded: "&$bytesreceived&@CRLF)
        EndIf
Link to comment
Share on other sites

  • 2 weeks later...

for the new ones with HTTP i found this program than can help to understand the comunication between the server and the client:

http://www.ieinspector.com/httpanalyzer/

the program capture the http traffic and you can found the information that you need.

if someone needs help with this can ask me, and i will try to help.

i think that my english is poor... :) sorry

bye and thanks

Link to comment
Share on other sites

  • 2 weeks later...

Any one add the latest and working Get and post request examples.....

When i debug the examples given i am getting some errors....

and could any post the latest HTTP.au3 file also because i installed the "autoitV3" but i didn't see the HTTP.au3 file in the "include" folder

Link to comment
Share on other sites

  • 6 months later...
  • 1 month later...
  • 3 months later...

I've been using this UDF for over a year now, but now I need it to work with HTTPS. Is there any hope?

I need to do this with https too. :graduated: I tried the WinHTTP UDF posted as well but can not figure it out since there are not many examples on how to use it properly.
Link to comment
Share on other sites

  • 2 months later...

I hate to bump old topics but is this still relevant? or is this obsolete? Need to use this for an idea i had just so i can learn how to send and recieve info .. as i going to make a program (go figure) but needs to talk to a php file and well i just need to know if this is the best? or is there better .. looking for easy at this point:)

Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...

Excellent UDF. I want to share the file uploader I made, because it took some hours to put the pieces together, and I think other people will have the same problem.

Setup:

1. Download the latest AutoIt.

2. Setup a server with PHP, i.e. WampServer.

3. Get Base64.au3 from:

4. Get HTTP.au3 from:

5. Get HTTPPost_files.au3 from (copy code and paste to au3 file):

6. Create an "uploader/" dir inside the public dir (i.e. "www/") of your server

7. Find a small JPEG image (< 1MB) and save as "uploader/Image.jpg"

8. Copy and save this code as "uploader/Uploader.au3"... I got this code from another post :unsure:

#Include <Base64.au3>
#Include <HTTP.au3>
#Include <HTTPPost_files.au3>

$BYTES = 0
$host = 'localhost'
$port = 80
$page = '/uploader/upload.php'
$SOCKET = _HTTPConnect($host, $port)
dim $data[1]
dim $filenames[1]
$data[0] = _HTTPEncodeString( "p=1000000" )  ; just for testing
$filenames[0] = @ScriptDir & '\Image.jpg'
$BYTES = _HTTPPost_files($host, $page, $socket, $data, $filenames)
MsgBox(0, "Uploader", _HTTPRead($SOCKET))  ; debug
$closeit = _HTTPClose($socket)

9. Copy and save this code as "uploader/upload.php" (notice this is PHP code):

<?php
// Silence stupid errors.
error_reporting(E_ALL^E_NOTICE);

define('PICTURE_SIZE_ALLOWED', 1024*1024); // bytes
$filename = '';
$result_msg = 'OK';
$uploaded_file = 'Image';   // _HTTPPost_files() sets the file name as the field name

// Create upload dir.
$upload_dir = 'uploads/';
@mkdir($upload_dir);

if (isset($_FILES[$uploaded_file]))
{
    if ($_FILES[$uploaded_file]['error'] == UPLOAD_ERR_OK)  // no error
    {
        if(filesize($_FILES[$uploaded_file]['tmp_name']) <= PICTURE_SIZE_ALLOWED)
        {
// Retrieve uploaded file from temp dir.
            $filename = $_FILES[$uploaded_file]['name'];
            move_uploaded_file($_FILES[$uploaded_file]['tmp_name'], $upload_dir.$filename);

// Decode file from base64 overwriting... shouldn't the server do this automatically!?
            file_put_contents($upload_dir.$filename, base64_decode(file_get_contents($upload_dir.$filename)), LOCK_EX);
        }
        else
            $result_msg = 'ERROR: The uploaded file exceeds '.PICTURE_SIZE_ALLOWED.' bytes';
    }
    elseif ($_FILES[$uploaded_file]['error'] == UPLOAD_ERR_INI_SIZE)
        $result_msg = 'ERROR: The uploaded file exceeds the upload_max_filesize directive in php.ini';
    else
        $result_msg = 'Unknown error';
}

//echo '$_FILES = ', json_encode($_FILES), "\n\n";  // uncomment to debug uploaded files
echo $result_msg;
?>

10. Run Uploader.au3... will show "OK" if the file was sent and saved as "uploads/Image.jpg", otherwise will show an error

Regards!

-Marco

Link to comment
Share on other sites

  • 3 weeks later...

I am trying to do a simple script to open an Outlook Webmail site (https) then long in automatically. This is the closest Ive found to that but it gives me errors. I have the latest Scite and AutoIt, so that may have something to do with it since some of these posts are old. Can anyone point me to a script already built for this purpose? The Record function doesnt produce usable code for me.

Thank!!!

K

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"]

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