Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在我的vc++应用程序中使用wininet发送zip文件_C++_Visual C++_Networking_Wininet - Fatal编程技术网

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

如何在我的vc++应用程序中使用wininet发送zip文件,c++,visual-c++,networking,wininet,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_fi

我能够发送一个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提前…

使用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[] =    "--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";
下面的主要代码…大多数是在发送头[]和尾[]之间发送的,您可以编写数据。 我已将错误检查和代码从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示例

希望能有帮助