如何在我的vc ++应用程序中使用wininet发送zip文件

时间:2011-06-20 06:53:40

标签: c++ visual-c++ networking wininet

通过让这两个变量存储头文件,我能够发送一个txt文件......

static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; 
sprintf(frmdata,"-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploaded_file\";filename=\"%s\"\r\nContent-Type:application/octet-stream\r\n\r\n",temp_upload_data->file_name);

我将txt文件数据添加到frmdata变量的末尾。我正在读取模式下打开txt文件。 我正在使用此功能发送请求

sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));

通过这个我能够上传一个txt文件.. 现在我想上传一个zip文件...... 我需要一些帮助来解决这个问题...... thnx提前...

2 个答案:

答案 0 :(得分:0)

使用HttpSendRequestEx之类的内容:

CString sHeaders(_T("Content-Type: application/x-www-form-urlencoded"));
BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
BufferIn.Next = NULL; 
BufferIn.lpcszHeader = sHeaders;
BufferIn.dwHeadersLength = sHeaders.GetLength();
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;                
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwPostSize; // Size of file
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;

然后使用InternetWriteFileHttpEndRequest发送请求。另请注意,您必须使用POST呼叫HttpOpenRequest

答案 1 :(得分:0)

这些代码对我有用 首先是标题部分:

static char hdrs[] =    "Content-Type: multipart/form-data; boundary=AaB03x";   
static char head[] =    "--AaB03x\r\n"
                        "Content-Disposition: form-data; name=\"userfile\"; filename=\"test.bin\"\r\n"
                        "Content-Type: application/octet-stream\r\n"
                        "\r\n";
static char tail[] =    "\r\n"
                        "--AaB03x--\r\n";

以及下面的主要代码...大部分是在发送数据的head[]tail[] send(write)之间。
我已将错误检查和代码从InternetOpen()移除到HttpOpenRequest(),因为它们在其他WinINet示例中已知:

...call InternetOpen...
...call InternetConnect...
...call HttpOpenRequest...

// your binary data
char data[] = "\x01\x02\x03\x04.....
DWORD dataSize = ...

// prepare headers
HttpAddRequestHeaders(hRequest, 
                      hdrs, -1, 
                      HTTP_ADDREQ_FLAG_REPLACE | 
                      HTTP_ADDREQ_FLAG_ADD); 

// send the specified request to the HTTP server and allows chunked transfers
INTERNET_BUFFERS bufferIn;

memset(&bufferIn, 0, sizeof(INTERNET_BUFFERS));

bufferIn.dwStructSize  = sizeof(INTERNET_BUFFERS);
bufferIn.dwBufferTotal = strlen(head) + dataSize + strlen(tail);

HttpSendRequestEx(hRequest, &bufferIn, NULL, HSR_INITIATE, 0);

// write data to an open Internet file

// 1. stream header
InternetWriteFile(hRequest, (const void*)head, strlen(head), &bytesWritten);

// 2. stream contents (binary data)
InternetWriteFile(hRequest, (const void*)data, dataSize, &bytesWritten);
// or a while loop for call InternetWriteFile every 1024 bytes...

// 3. stream tailer
InternetWriteFile(hRequest, (const void*)tail, strlen(tail), &bytesWritten);

// end a HTTP request (initiated by HttpSendRequestEx)
HttpEndRequest(hRequest, NULL, HSR_INITIATE, 0);

关于获取'userfile'的php代码,请参考php的例子Example #2 Validating file uploads

希望有所帮助