如何在我的vc++应用程序中使用wininet发送zip文件
how to send a zip file using wininet in my vc++ application
我能够通过将这两个变量存储在头中来发送一个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;
然后使用InternetWriteFile
和HttpEndRequest
来发送请求。还要注意,您必须使用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验证文件上传
希望它能帮助
相关文章:
- 从命令行c++发送文本文件名
- 发送一个带有libcurl C++问题的帖子请求:s
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 如何在C++中将函数发送到另一个进程
- boost xml parsingl将xml的路径作为变量发送
- 如何将数组从Objective-C发送到C++函数
- 如何使用ESP8266向谷歌主页发送命令
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- 为什么默认复制函数在按值发送参数时不调用?
- 程序不断发送垃圾邮件号码?
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如何使用 Google Test 向测试添加元数据 / 如何将数据从 Google Test 发送到 TestEven
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- Qt:如何使不兼容的发送方/接收方参数兼容?
- (Winsock) UDP 接收工作正常,但同一套接字的发送失败
- MSG_WAITALL的套接字发送得到了 22 EINVAL
- 将文件发送到 HTTP 服务器 (C++ Wininet)
- Wininet 收到发送的标头
- WinInet:如何用c++发送多个http get请求
- 如何在我的vc++应用程序中使用wininet发送zip文件