如何使用 wininet 传输可由 php 脚本读取的文件?
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们一步一步地进行。
首先是涉及的 HTTP 标头:
然后您必须使用 POST 表单的内容构建一个字符串。 假设您有名为 file 的输入:
您计算该字符串的长度并将其放在上面的 Content-Length 中。
好的,一个完整的 HTTP 请求将如下所示:
现在 PHP 手册中的一些代码:
了解我,我可能已经弄乱了内容的格式,但这是总体思路。
Let's take this one step at a time.
First the HTTP headers Involved:
Then you have to build a string with the contents of a POST Form. Lets assume you have the input named file:
You calculate the length of this string and put on the Content-Length above.
Ok a complete HTTP Request would look like this:
Now some code from the PHP manual:
Knowing me I've probably messed the format for the content but this is the general idea.
将上面的表单数据和标题更改为以下内容解决了问题:
Changing the form data and headers that I had above to the following solved the problem:
这里是涉及到的事情。 基本上,您必须创建对网址的 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.