如何使用 wininet 传输可由 php 脚本读取的文件?

发布于 2024-07-06 10:06:01 字数 745 浏览 12 评论 0原文

我想使用 wininet 将文本文件传输到网络服务器,就像使用将文件发布到服务器的网络表单传输文件一样。

根据我收到的答案,我尝试了以下代码:

 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));");

test.php 脚本正在运行,但它似乎没有获取正确的数据。

有人可以给我任何额外的帮助或可以看的地方吗? 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱了心跳 2024-07-13 10:06:01

让我们一步一步地进行。

首先是涉及的 HTTP 标头:

  1. Content-Type:multipart/form-data
  2. Content-Length:<这取决于内容的字节总和>

然后您必须使用 POST 表单的内容构建一个字符串。 假设您有名为 file 的输入:

文件=文件名.txt
<您现在在回车符后添加文件内容>

您计算该字符串的长度并将其放在上面的 Content-Length 中。

好的,一个完整的 HTTP 请求将如下所示:

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

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

现在 PHP 手册中的一些代码:

<?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>";

?>

了解我,我可能已经弄乱了内容的格式,但这是总体思路。

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.

画中仙 2024-07-13 10:06:01

将上面的表单数据和标题更改为以下内容解决了问题:

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

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";
像你 2024-07-13 10:06:01

这里是涉及到的事情。 基本上,您必须创建对网址的 HTTP 请求,将信息附加到请求,然后发送它。 在您的情况下,该请求必须是 POST 请求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文