Dev C++ Wininet Upload file using HTTP(Dev C++ Wininet 使用 HTTP 上传文件)
问题描述
我想上传C: est.txt"到网络服务器,当我运行程序时,文件没有上传,我没有收到任何错误.
I want to upload "C: est.txt" to webserver, when I am running program, file is not uploading and I am not getting any error.
完整的C++代码可以在这里找到
网络服务器上的 php 代码可以在这里找到:"http://student114.110mb.com/上传.txt"或者"http://student114.110mb.com/upload.php"
and php code on webserver can be find here: "http://student114.110mb.com/upload.txt" or "http://student114.110mb.com/upload.php"
请在我做错的地方帮助我
kindly help me where I am doing wrong
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>
#pragma comment(lib,"wininet.lib")
using namespace std;
int main()
{
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858
Content-Disposition: form-data; name="uploadedfile"; filename="C: est.txt"
Content-Type: text/plain
file contents here
-----------------------------7d82751e2bc0858--";
static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";
HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession==NULL)
{
cout<<"Error: InternetOpen";
}
HINTERNET hConnect = InternetConnect(hSession, _T("localhost"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if(hConnect==NULL)
{
cout<<"Error: InternetConnect";
}
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("upload.php"), NULL, NULL, (const char**)"*/* ", 0, 1);
if(hRequest==NULL)
{
cout<<"Error: HttpOpenRequest";
}
BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
if(!sent)
{
cout<<"Error: HttpSendRequest";
}
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return 0;
}
推荐答案
我能够让你的代码工作.
I was able to make your code work.
首先你提供的链接上的代码和你发布的代码不一样:
First of all the code on the link you provided and the code you posted is not the same:
InternetConnect(hSession, _T("localhost"), ...
InternetConnect(hSession, _T("http://student114.110mb.com"), ...
您必须在此处传递主机名或 IP 地址,因此localhost"很好,但http://student114.110mb.com" 不是.如果您传递 URL,您将收到 12005 错误代码 [请参阅 msdn 上的 WinINet 错误代码].
You must pass an host name or ip address here so "localhost" is good but "http://student114.110mb.com" isn't. If you pass an URL you will get the 12005 error code [see WinINet error codes on msdn].
另一个问题是 frmdata 字符串.您应该将 C: est.txt 中的反斜杠加倍,否则您将在字符串中得到一个制表符 .分隔符前后的 也应替换为 ,因为 RFC 1521 和大多数其他 Internet 协议使用 CRLF 作为行分隔符.
Another problem is the frmdata string. You should double the backslash in C: est.txt or you will get a tab character in your string. The before and after the delimiters should also be replaced by because RFC 1521 and most other internet protocols use CRLF as a line delimiter.
这是我使用的字符串.
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858
Content-Disposition: form-data; name="uploadedfile"; filename="C:\test.txt"
Content-Type: text/plain
file contents here
-----------------------------7d82751e2bc0858--
";
最后,PHP 代码不起作用,因为您在应该使用 $_FILES["uploadedfile"] 的地方使用了 $_FILES["file"].uploadedfile"通常对应于 <input type="file"> 的名称.HTML 中的标记,但在您的情况下,它是在 frmdata[] 字符串的 name= 参数中指定的.
Finally the PHP code doesn't work because you use $_FILES["file"] where you should be using $_FILES["uploadedfile"]. "uploadedfile" would typically correspond to the name of an <input type="file"> tag in HTML but in your case it is specified in the name= parameter of the frmdata[] string.
这是我用来测试的 PHP 代码
Here's the PHP code I have used to test this
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "/files/my_file");
当您处理像这样复杂的客户端/服务器交互时,单独测试每个部分会有所帮助.例如,你可以.
When you work on complex client/server interaction like this it helps to test each part separately. You could for instance.
写一个简单的 HTML 上传表单到测试你的 php 脚本
Write a simple HTML upload form to test your php script
让您的程序将其请求发送到netcat 并检查输出
Have your program send its request to netcat and examine the output
这篇关于Dev C++ Wininet 使用 HTTP 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Dev C++ Wininet 使用 HTTP 上传文件
基础教程推荐
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01