views:

1295

answers:

3

I would like to transfer a text file to a webserver using wininet as if the file was being transferred using a web form that posts the file to the server.

Based on answers I've received I've tried the following code:

 static TCHAR hdrs[] = "Content-Type: multipart/form-data\nContent-Length: 25";
 static TCHAR frmdata[] = "file=filename.txt\ncontent";

   HINTERNET hSession = InternetOpen("MyAgent",
      INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
   HINTERNET hConnect = InternetConnect(hSession, "example.com",
      INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
   HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "test.php", NULL, NULL, NULL, 0, 1);
   HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));");

The test.php script is being run, but it doesn't appear to be getting the correct data.

Could anyone give me any additional help or somewhere to look? Thanks.

A: 

Here's a general description of the things involved in that. Basically, you have to create an HTTP request to a web address, attach information to the request and then send it. The request must be a POST request in your case.

dguaraglia
+1  A: 

Let's take this one step at a time.

First the HTTP headers Involved:

  1. Content-Type: multipart/form-data
  2. Content-Length: <this depends on the sum of the bytes of the contents>

Then you have to build a string with the contents of a POST Form. Lets assume you have the input named file:

file=filename.txt
<You now add the content of the file after that carriage return>

You calculate the length of this string and put on the Content-Length above.

Ok a complete HTTP Request would look like this:

POST /file_upload.php HTTP/1.0
Content-type: multipart/form-data
Content-length: <calculated string's length: integer>

file=filename.txt
...File Content...

Now some code from the PHP manual:

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

Knowing me I've probably messed the format for the content but this is the general idea.

Gustavo Carreno
A: 

Changing the form data and headers that I had above to the following solved the problem:

  static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"file.txt\"\nContent-Type: text/plain\n\nfile contents  here\n-----------------------------7d82751e2bc0858--";
  static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";
Rob
See, I had my content format all wrong. I told you multipart but gave you another format :(
Gustavo Carreno
Yeah, but you definitely helped me look in the right direction :)
Rob