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

how to send a zip file using wininet in my vc++ application

本文关键字:wininet 发送 zip 文件 我的 vc++ 应用程序      更新时间:2023-10-16

我能够通过将这两个变量存储在头中来发送一个txt文件。。。

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

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

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

通过这个,我可以上传一个txt文件。。现在我想上传一个zip文件。。。我需要一些关于如何做到这一点的帮助。。。thnx提前。。。

使用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

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

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

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

...call InternetOpen...
...call InternetConnect...
...call HttpOpenRequest...
// your binary data
char data[] = "x01x02x03x04.....
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的示例示例#2验证文件上传

希望它能帮助