WinInet模拟HTTP POST上传二进制文件流
BOOL CDllValidateDlg::PostHttpPage(CString &result,CString &PageName,CString &postData) // 请求Http.{ CInternetSession session("SighAgent"); try { INTERNET_PORT nPort = 80; DWORD dwRet = 0; /*以下为wininet 网络请求流程*/ CHttpConnection* pServer = session.GetHttpConnection(SERVER_IP, nPort); CHttpFile* pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,PageName); //打开一个请求 CString strHeaders = "Content-Type: application/x-www-form-urlencoded"; // 请求头 //开始发送请求 string data_post = MultiByteToUtf8(postData.GetBuffer(0)); pFile->SendRequest(strHeaders,(LPVOID)data_post.data(),data_post.size()); //发送请求,注意此为堵塞函数 pFile->QueryInfoStatusCode(dwRet); //发送请求后,就查询发送的结果 if (dwRet == HTTP_STATUS_OK) //这个代表请求正确 { CString str; while(pFile->ReadString(str)) //开始返回返回结果 { char *pStr = (char*)str.GetBuffer(str.GetLength()); //取得str对象的原始字符串 result = result + str; } result = Utf8ToMultiByte(result.GetBuffer(0)).c_str(); } else { SAFE_DELETE(pFile); SAFE_DELETE(pServer); return FALSE; } SAFE_DELETE(pFile); SAFE_DELETE(pServer); } catch (CInternetException* pEx) { //catch errors from WinInet TCHAR pszError[200]; pEx->GetErrorMessage(pszError, 200); OutputDebugString(pszError); return FALSE; } session.Close(); return TRUE;}
我在程序里用到上面这个PostHttpPage函数,来和服务器交互数据。
之前的一些数据上传都是字符串或整型数,没出现过问题。
现在想用这同一个函数,上传二进制形式的文件流,没有成功。请问我下面的做法是哪里错了?
//二进制导入图片文件 char buffer[102400]; //BUF_SIZE大小自己定义 FILE * pFile = fopen("photo.bmp", "rb"); int img = fread(buffer, sizeof(char), 102400, pFile);// char stzimg[100];// itoa(img,stzimg,10);// AfxMessageBox(stzimg);//二进制导入图片文件结束 CString post_data; post_data.Format("matchid='%s'&matchname='%s'&aid='%d'&ticket='%d'" "&name='%s'&gender='%d'&folk='%s'&birthday='%s'&addr='%s'&cardid='%s'&expire='%s'&agency='%s'&face='%s'", matchID,matchName,AID,ticket, Name,abs(stricmp(Gender,"女")),Folk,BirthDay,Address,Code, ExpireEnd, Agency,buffer); //请求的附加参数 CString result; //返回的结果 CString post_page = "test_id_validater/update_num.php"; //请求的php PostHttpPage(result,post_page,post_data);
回复讨论(解决方案)
二进制考虑用BASE64编码等,然后Post
你确定html协议是这样传二进制文件的吗,先用wireshark查看下
我在网上找到上传文件的代码是这个样子的:
#include <windows.h> #include <wininet.h> #include <iostream> #define ERROR_OPEN_FILE 10 #define ERROR_MEMORY 11 #define ERROR_SIZE 12 #define ERROR_INTERNET_OPEN 13 #define ERROR_INTERNET_CONN 14 #define ERROR_INTERNET_REQ 15 #define ERROR_INTERNET_SEND 16 using namespace std; int main() { // Local variables static char *filename = "test.txt"; //Filename to be loaded static char *type = "image/jpg"; static char boundary[] = "pippo"; //Header boundary static char nameForm[] = "uploadedfile"; //Input form name static char iaddr[] = "localhost"; //IP address static char url[] = "test.php"; //URL char hdrs[255]; //Headers char * buffer; //Buffer containing file + headers char * content; //Buffer containing file FILE * pFile; //File pointer long lSize; //File size size_t result; // Open file pFile = fopen ( filename , "rb" ); if (pFile==NULL) return ERROR_OPEN_FILE; // obtain file size: fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile); // allocate memory to contain the whole file: content = (char*) malloc (sizeof(char)*lSize); if (content == NULL) return ERROR_MEMORY; // copy the file into the buffer: result = fread (content,1,lSize,pFile); if (result != lSize) return ERROR_SIZE; // terminate fclose (pFile); //allocate memory to contain the whole file + HEADER buffer = (char*) malloc (sizeof(char)*lSize + 2048); //print header sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary); sprintf(buffer,"--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",boundary,nameForm,filename); sprintf(buffer,"%sContent-Type: %s\r\n\r\n",buffer,type); sprintf(buffer,"%s%s\r\n",buffer,content); sprintf(buffer,"%s--%s--\r\n",buffer,boundary); //Open internet connection HINTERNET hSession = InternetOpen("WinSock",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if(hSession==NULL) return ERROR_INTERNET_OPEN; HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1); if(hConnect==NULL) return ERROR_INTERNET_CONN; HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",url, NULL, NULL, (const char**)"*/*\0", 0, 1); if(hRequest==NULL) return ERROR_INTERNET_REQ; BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer)); if(!sent) return ERROR_INTERNET_SEND; //close any valid internet-handles InternetCloseHandle(hSession); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); return 0; }
这应该是把文件以二进制形式传给test.php吧。那么在test.php那边怎么接收这个二进制,并写入mysql数据库的某个字段呢?
它可以和别的整型数及字串在同一次post中提交吗?
这应该是把文件以二进制形式传给test.php吧。那么在test.php那边怎么接收这个二进制,并写入mysql数据库的某个字段呢?
要看对应的php是如何处理的。然后你发送对应格式的数据
这应该是把文件以二进制形式传给test.php吧。那么在test.php那边怎么接收这个二进制,并写入mysql数据库的某个字段呢?
要看对应的php是如何处理的。然后你发送对应格式的数据
对应PHP文件是用$bian_liang_ming = $_REQUEST['xxxx'];
来获取传过去的参数的。这种情况如何传递二进制数据流啊?
你是玩C的,你应该清楚的知道字符串是依靠尾部附加的 '\0' 来判断结束的,那你如何能保证图片数据中不出现'\0' 而导致字符串提前结束呢?
你是玩C的,你应该清楚的知道字符串是依靠尾部附加的 '\0' 来判断结束的,那你如何能保证图片数据中不出现'\0' 而导致字符串提前结束呢?
我其实并不知道确切该怎么做,所以随便套用了之前传递整型数的办法,知道是不对的。
c++ 应用 curl 库函数实现,这样少了协议的处理,事情变得单纯了
也可以用 sock 库函数实现
你 #3 的代码就是 sock 应用的一个例子

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. Open Kugou Music and click on your profile picture. 2. Click the settings icon in the upper right corner. 3. Click [Upload Music Works]. 4. Click [Upload Works]. 5. Select the song and click [Next]. 6. Finally, click [Upload].

With the advent of the digital age, music platforms have become one of the main ways for people to obtain music. However, sometimes when we listen to songs, we find that there are no lyrics, which is very disturbing. Many people hope that lyrics can be displayed when listening to songs to better understand the content and emotions of the songs. QQ Music, as one of the largest music platforms in China, also provides users with the function of uploading lyrics, so that users can better enjoy music and feel the connotation of the songs. The following will introduce how to upload lyrics on QQ Music. first

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

Upload speed becomes very slow? I believe this is a problem that many friends will encounter when uploading things on their computers. If the network is unstable when using a computer to transfer files, the upload speed will be very slow. So how can I increase the network upload speed? Below, the editor will tell you how to solve the problem of slow computer upload speed. When it comes to network speed, we all know that the speed of opening web pages, download speed, and upload speed are also very critical. Especially some users often need to upload files to the network disk, so a fast upload speed will undoubtedly save you a lot of money. Less time, what should I do if the upload speed is slow? Below, the editor brings you pictures and texts on how to deal with slow computer upload speeds. How to solve the problem of slow computer upload speed? Click "Start--Run" or "Window key"

Binary arithmetic is an operation method based on binary numbers. Its basic operations include addition, subtraction, multiplication and division. In addition to basic operations, binary arithmetic also includes logical operations, displacement operations and other operations. Logical operations include AND, OR, NOT and other operations, and displacement operations include left shift and right shift operations. These operations have corresponding rules and operand requirements.

As long as the computer is equipped with a camera, it can take pictures, but some users still don't know how to take pictures and upload them. Now I will give you a detailed introduction to the method of taking pictures on the computer, so that users can upload the pictures wherever they want. How to take photos and upload them on a computer 1. Mac computer 1. Open Finder and click the application on the left. 2. After opening, click the Camera application. 3. Just click the photo button below. 2. Windows computer 1. Open the search box below and enter camera. 2. Then open the searched application. 3. Click the photo button next to it.

Solution: 1. Check the Content-Type in the request header; 2. Check the data format in the request body; 3. Use the appropriate encoding format; 4. Use the appropriate request method; 5. Check the server-side support.

HTTP Status Code 200: Explore the Meaning and Purpose of Successful Responses HTTP status codes are numeric codes used to indicate the status of a server's response. Among them, status code 200 indicates that the request has been successfully processed by the server. This article will explore the specific meaning and use of HTTP status code 200. First, let us understand the classification of HTTP status codes. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. Among them, 2xx indicates a successful response. And 200 is the most common status code in 2xx
