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

2019-08-22 13:51发布

I was able to send a txt file by having these 2 variables storing header...

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);

I am adding txt file data to frmdata variable at its end.i am opening txt file in read mode. and i am using this function to send request

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

by this i am able to upload a txt file.. now i want to upload a zip file... I need some help on how to do this... thnx in advance...

2条回答
太酷不给撩
2楼-- · 2019-08-22 14:33

Use HttpSendRequestEx and something like :

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;

Then use InternetWriteFile and HttpEndRequest to send the request. Also note you have to call HttpOpenRequest with POST.

查看更多
smile是对你的礼貌
3楼-- · 2019-08-22 14:45

This some codes work for me
First the header parts:

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";

and main codes below...most is between sending the head[] and tail[] you send(write) the data.
I had removed error checking and codes from InternetOpen() to HttpOpenRequest() for they were known in other WinINet examples:

...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);

About the php codes to get 'userfile', please ref to php's example Example #2 Validating file uploads

Hope it helps

查看更多
登录 后发表回答