C++ 开发C++;Wininet使用HTTP上传文件
我想上传“C:\test.txt”到Web服务器,当我运行程序时,文件没有上传,我没有得到任何错误 Web服务器上的php代码可以在这里找到:“ 或 “” 请帮助我哪里做错了C++ 开发C++;Wininet使用HTTP上传文件,c++,http,file,upload,wininet,C++,Http,File,Upload,Wininet,我想上传“C:\test.txt”到Web服务器,当我运行程序时,文件没有上传,我没有得到任何错误 Web服务器上的php代码可以在这里找到:“ 或 “” 请帮助我哪里做错了 #include <windows.h> #include <wininet.h> #include <tchar.h> #include <iostream> #pragma comment(lib,"wininet.lib") using namespace std
#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\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\test.txt\"\nContent-Type: text/plain\n\nfile contents here\n-----------------------------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", 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;
}
#包括
#包括
#包括
#包括
#pragma注释(lib,“wininet.lib”)
使用名称空间std;
int main()
{
静态TCHAR frmdata[]=“-------------------------------------7d82751e2bc0858\n内容处理:表单数据;名称=\“uploadedfile\”;文件名=\“C:\test.txt\”\n内容类型:text/plain\n\n此处文件内容\n----------------------------7d82751e2bc0858-”;
静态TCHAR hdrs[]=“内容类型:多部分/表单数据;边界=------------------------------------7d82751e2bc0858”;
HINTERNET hSession=互联网开放(“MyAgent”,互联网开放类型,空,空,0);
if(hsSession==NULL)
{
我能让你的代码正常工作
首先,您提供的链接上的代码与您发布的代码不同:
InternetConnect(hSession, _T("localhost"), ...
InternetConnect(hSession, _T("http://student114.110mb.com"), ...
您必须在此处传递主机名或ip地址,因此“localhost”是好的,而“”不是。
如果您传递一个URL,您将得到12005错误代码[
另一个问题是frmdata字符串。应将C:\test.txt中的反斜杠加倍,否则字符串中会出现制表符\t。分隔符前后的\n也应替换为\r\n,因为RFC 1521和大多数其他internet协议使用CRLF作为行分隔符
这是我用过的绳子
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-----------------------------7d82751e2bc0858--\r\n";
最后,PHP代码不起作用,因为您使用了$_FILES[“file”],而您应该使用$_FILES[“uploadedfile”]。“uploadedfile”通常对应于HTML中标记的名称,但在您的情况下,它是在frmdata[]字符串的name=参数中指定的
下面是我用来测试这个的PHP代码
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "/files/my_file");
当您处理这样复杂的客户机/服务器交互时,单独测试每个部分会有所帮助
- 编写一个简单的HTML上传表单到
测试您的php脚本
- 让您的程序将其请求发送到
netcat并检查输出
如果您希望通过php脚本上传文件,则必须在不使用WinAPI的情况下发送完整请求
在过去,我也尝试像您一样发送这样的请求,使用所有api函数。
但是当我用wireshark嗅探它时,我得到了和你一样的解决方案:头被合并了,它不工作
因此,我再次使用winsock,如果您要发送的数据不是太大,它就可以工作
代码如下:
#include <iostream>
#include <winsock2.h>
#include <string>
using namespace std;
//link libwsock32.a
unsigned long WinsockStart()
{
WSADATA wsa;
unsigned long ulong;
struct hostent *host;
if(WSAStartup(MAKEWORD(2,2), &wsa) < 0)
{
cout << "Error WinsockStart()" << endl;
WSACleanup();
return 1;
}
if((host=gethostbyname("www.example.com"))<0)
{
cout << "Fehler gethostbyname()" << endl;
WSACleanup();
return 2;
}
ulong = *(unsigned long*) host->h_addr;
return ulong;
}
void error_exit(string text)
{
cout << text;
WSACleanup();
exit(EXIT_FAILURE);
}
int main()
{
SOCKET sock;
struct sockaddr_in addr;
unsigned long win=0;
int con = 0, gr=0, send_r=0, rec=0;
char header[2048], puffer[2018];
string to_send="hello world";
string name="test99.txt";
win=WinsockStart();
if(win==1||win==2)
error_exit("Error WinsockStart()");
addr.sin_family=AF_INET;
addr.sin_port=htons(80);
addr.sin_addr.s_addr = win;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock<0)
error_exit("Error socket()");
gr = (to_send.size()+name.size()+287);
sprintf(header, "POST /upload.php HTTP/1.1\r\n");
sprintf(header, "%sHost: www.example.com\r\n", header);
sprintf(header, "%sConnection: Keep-Alive\r\n", header);
sprintf(header, "%sContent-Type: multipart/form-data; boundary=---------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Length: %d\r\n", header, gr);
sprintf(header, "%s\r\n", header);
sprintf(header, "%s-----------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\n", header, name.c_str());
sprintf(header, "%sContent-Type: text/plain\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%s%s\r\n", header, to_send.c_str());
sprintf(header, "%s-----------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Disposition: form-data; name=\"post\"\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%supload\r\n\r\n", header);
sprintf(header, "%s-----------------------------90721038027008--\r\n\r\n\0", header);
con = connect(sock, (SOCKADDR*)&addr, sizeof(addr));
if(con < 0)
error_exit("Error connect()");
if(send_r=send(sock, header, strlen(header), 0)<0)
error_exit("Error send()");
while(rec=recv(sock, puffer, 2048, 0))
{
if(rec==0)
error_exit("Server quit");
printf("%s", puffer);
}
closesocket(sock);
WSACleanup();
return EXIT_SUCCESS;
}
#包括
#包括
#包括
使用名称空间std;
//链接libwsock32.a
未签名的长WinsockStart()
{
WSADATA wsa;
未签名的long-ulong;
结构主机*主机;
if(WSAStartup(MAKEWORD(2,2),&wsa)<0)
{
库特
最后一个“-”字符应位于“------------------------------------7d82751e2bc0858”边界之前,而不是之后
正确代码:
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-------------------------------7d82751e2bc0858\r\n";
你安装了fiddler并看到发送的是什么HTTP流量了吗?我先从这里开始。当我编辑源代码时,出现了错误“error:httpsendRequest12005”,请参见上面的超链接
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-------------------------------7d82751e2bc0858\r\n";