Dev C++ Wininet Upload file using HTTP这里是wininet的例子。 我已经改变了,但我不能让它发挥作用!首先,有警告说char函数与TCHAR varibales一起使用。我用正确的那些替换了那些功能。
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>
#pragma comment(lib,"wininet.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
static TCHAR frmdata[] = L"-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nAAAAAAAAAAAAAAAAA\r\n-----------------------------7d82751e2bc0858--\r\n";
static TCHAR hdrs[] = L"Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";
HINTERNET hSession = InternetOpen(L"MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession==NULL)
{
cout<<"Error: InternetOpen";
}
HINTERNET hConnect = InternetConnect(hSession, L"localhost",INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if(hConnect==NULL)
{
cout<<"Error: InternetConnect";
}
HINTERNET hRequest = HttpOpenRequest(hConnect, L"POST",L"/upload/upload.php", NULL, NULL, (LPCWSTR *)"*/*\0", 0, 1);
if(hRequest==NULL)
{
cout<<"Error: HttpOpenRequest";
}
BOOL sent= HttpSendRequest(hRequest, hdrs, wcslen(hdrs), frmdata, wcslen(frmdata));
if(!sent)
{
cout<<"Error: HttpSendRequest";
}
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
}
[/代码]
但它仍然无法正常工作!这就是我用netcat获得的东西
connect to [127.0.0.1] from validation.sls.microsoft.com [127.0.0.1] 52503
POST /upload/upload.php HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7d82751e
bc0858
User-Agent: MyAgent
Host: 127.0.0.1
Content-Length: 210
Cache-Control: no-cache
Cookie: s_pers=%20s_fid%3D6D463DCA024AF939-1C2604BDB2B68135%7C1457266833122%3B%
0s_vs%3D1%7C1394110233124%3B%20s_nr%3D1394108433125-Repeat%7C1425644433125%3B
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d 8 2 7 5 1 e 2 b c
8 5 8
C o n t e n t - D i s p o s i t i o n : f o r m - d a t a ; n a m e = " f
l e " ; f i l e n a m e = " C : \ t
我只是不明白为什么会这样。我已经改变了这个字符串
sent= HttpSendRequest(hRequest, hdrs, wcslen(hdrs), frmdata, wcslen(frmdata)*sizeof(TCHAR) );
再次使用ncat捕获数据包。它看起来像这样:
connect to [127.0.0.1] from validation.sls.microsoft.com [127.0.0.1] 52570
POST /upload/upload.php HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7d82751e2
bc0858
User-Agent: MyAgent
Host: 127.0.0.1
Content-Length: 420
Cache-Control: no-cache
Cookie: s_pers=%20s_fid%3D6D463DCA024AF939-1C2604BDB2B68135%7C1457266833122%3B%2
0s_vs%3D1%7C1394110233124%3B%20s_nr%3D1394108433125-Repeat%7C1425644433125%3B
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d 8 2 7 5 1 e 2 b c
8 5 8
C o n t e n t - D i s p o s i t i o n : f o r m - d a t a ; n a m e = " f i
l e " ; f i l e n a m e = " C : \ t e s t . t x t "
C o n t e n t - T y p e : t e x t / p l a i n
A A A A A A A A A A A A A A A A A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d 8 2 7 5 1 e 2 b c
0 8 5 8 - -
但仍然有问题,因为我无法在upload.php脚本上上传数据(它是100%工作脚本,我用随机文件检查过它)。另外netcat中的数据看起来很奇怪,为什么字符之间有空格?也许是因为TCHAR数据?所以我仍然困惑。如何使它工作?我认为这个问题是TCHAR类型,但必须有办法让它适用于任何类型。请帮忙。也很抱歉我的英语,这不是我的母语。所有的帮助都会很有用!
答案 0 :(得分:1)
对于HttpSendRequest
来电,LPVOID lpOptional
不应该是unicode。
试试这个:
char pszData[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nAAAAAAAAAAAAAAAAA\r\n-----------------------------7d82751e2bc0858--\r\n";
BOOL sent= HttpSendRequest(hRequest, hdrs, wcslen(hdrs), pszData, strlen(pszData));