s-->\r\n”,缓冲区,边界); //开放互联网连接 HINTERNET hSession=InternetOpen(“WinSock”,INTERNET\u OPEN\u TYPE\u prefig,NULL,NULL,0); 如果(hSession==NULL)返回错误\u互联网\u打开; HINTERNET hConnect=InternetConnect(会话、iaddr、INTERNET\u默认\u HTTP\u端口、NULL、NULL、INTERNET\u服务\u HTTP、0、1); 如果(hConnect==NULL)返回错误\u INTERNET\u CONN; HINTERNET hRequest=HttpOpenRequest(hConnect,(const char*)“POST”,url,NULL,NULL,(const char**)”*/*\0,0,1); 如果(hRequest==NULL)返回错误\u互联网\u请求; BOOL sent=HttpSendRequest(hRequest、hdrs、strlen(hdrs)、buffer、strlen(buffer)); 如果(!sent)返回错误\u INTERNET\u SEND; //关闭所有有效的internet句柄 InternetCloseHandle(hsSession); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); 返回0; },file,post,upload,wininet,File,Post,Upload,Wininet" /> s-->\r\n”,缓冲区,边界); //开放互联网连接 HINTERNET hSession=InternetOpen(“WinSock”,INTERNET\u OPEN\u TYPE\u prefig,NULL,NULL,0); 如果(hSession==NULL)返回错误\u互联网\u打开; HINTERNET hConnect=InternetConnect(会话、iaddr、INTERNET\u默认\u HTTP\u端口、NULL、NULL、INTERNET\u服务\u HTTP、0、1); 如果(hConnect==NULL)返回错误\u INTERNET\u CONN; HINTERNET hRequest=HttpOpenRequest(hConnect,(const char*)“POST”,url,NULL,NULL,(const char**)”*/*\0,0,1); 如果(hRequest==NULL)返回错误\u互联网\u请求; BOOL sent=HttpSendRequest(hRequest、hdrs、strlen(hdrs)、buffer、strlen(buffer)); 如果(!sent)返回错误\u INTERNET\u SEND; //关闭所有有效的internet句柄 InternetCloseHandle(hsSession); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); 返回0; },file,post,upload,wininet,File,Post,Upload,Wininet" />

通过POST上传文件 我有必要在C++中实现一个Web服务器上的文件上传,我成功地使用了以下代码: #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; } #包括 #包括 #包括 #定义错误\u打开\u文件10 #定义错误\u存储器11 #定义错误\u大小12 #定义错误\u互联网\u打开13 #定义错误\u互联网\u连接14 #定义错误\u互联网\u请求15 #定义错误\u INTERNET\u发送16 使用名称空间std; int main() { //局部变量 静态字符*filename=“test.txt”;//要加载的文件名 静态字符*type=“image/jpg”; 静态字符边界[]=“pippo”;//头边界 静态字符名称表单[]=“uploadedfile”;//输入表单名称 静态字符iaddr[]=“localhost”;//IP地址 静态字符url[]=“test.php”;//url char hdrs[255];//头 char*buffer;//包含文件+头的缓冲区 char*content;//包含文件的缓冲区 FILE*pFile;//文件指针 long lSize;//文件大小 结果的大小; //打开文件 pFile=fopen(文件名,“rb”); 如果(pFile==NULL)返回错误\u打开\u文件; //获取文件大小: fseek(pFile,0,SEEK_END); lSize=ftell(pFile); 倒带(pFile); //分配内存以包含整个文件: 内容=(char*)malloc(sizeof(char)*lSize); if(content==NULL)返回错误\u内存; //将文件复制到缓冲区: 结果=fread(内容,1,lSize,pFile); if(result!=lSize)返回错误大小; //终止 fclose(pFile); //分配内存以包含整个文件+头 缓冲区=(char*)malloc(sizeof(char)*lSize+2048); //打印页眉 sprintf(hdrs,“内容类型:多部分/表单数据;边界=%s”,边界); sprintf(缓冲区--%s\r\n内容处理:表单数据;名称=\%s\;文件名=\%s\“\r\n”、边界、名称表单、文件名); sprintf(缓冲区,“%s内容类型:%s\r\n\r\n”,缓冲区,类型); sprintf(缓冲区,“%s%s\r\n”,缓冲区,内容); sprintf(缓冲区,“%s-->s-->\r\n”,缓冲区,边界); //开放互联网连接 HINTERNET hSession=InternetOpen(“WinSock”,INTERNET\u OPEN\u TYPE\u prefig,NULL,NULL,0); 如果(hSession==NULL)返回错误\u互联网\u打开; HINTERNET hConnect=InternetConnect(会话、iaddr、INTERNET\u默认\u HTTP\u端口、NULL、NULL、INTERNET\u服务\u HTTP、0、1); 如果(hConnect==NULL)返回错误\u INTERNET\u CONN; HINTERNET hRequest=HttpOpenRequest(hConnect,(const char*)“POST”,url,NULL,NULL,(const char**)”*/*\0,0,1); 如果(hRequest==NULL)返回错误\u互联网\u请求; BOOL sent=HttpSendRequest(hRequest、hdrs、strlen(hdrs)、buffer、strlen(buffer)); 如果(!sent)返回错误\u INTERNET\u SEND; //关闭所有有效的internet句柄 InternetCloseHandle(hsSession); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); 返回0; }

通过POST上传文件 我有必要在C++中实现一个Web服务器上的文件上传,我成功地使用了以下代码: #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; } #包括 #包括 #包括 #定义错误\u打开\u文件10 #定义错误\u存储器11 #定义错误\u大小12 #定义错误\u互联网\u打开13 #定义错误\u互联网\u连接14 #定义错误\u互联网\u请求15 #定义错误\u INTERNET\u发送16 使用名称空间std; int main() { //局部变量 静态字符*filename=“test.txt”;//要加载的文件名 静态字符*type=“image/jpg”; 静态字符边界[]=“pippo”;//头边界 静态字符名称表单[]=“uploadedfile”;//输入表单名称 静态字符iaddr[]=“localhost”;//IP地址 静态字符url[]=“test.php”;//url char hdrs[255];//头 char*buffer;//包含文件+头的缓冲区 char*content;//包含文件的缓冲区 FILE*pFile;//文件指针 long lSize;//文件大小 结果的大小; //打开文件 pFile=fopen(文件名,“rb”); 如果(pFile==NULL)返回错误\u打开\u文件; //获取文件大小: fseek(pFile,0,SEEK_END); lSize=ftell(pFile); 倒带(pFile); //分配内存以包含整个文件: 内容=(char*)malloc(sizeof(char)*lSize); if(content==NULL)返回错误\u内存; //将文件复制到缓冲区: 结果=fread(内容,1,lSize,pFile); if(result!=lSize)返回错误大小; //终止 fclose(pFile); //分配内存以包含整个文件+头 缓冲区=(char*)malloc(sizeof(char)*lSize+2048); //打印页眉 sprintf(hdrs,“内容类型:多部分/表单数据;边界=%s”,边界); sprintf(缓冲区--%s\r\n内容处理:表单数据;名称=\%s\;文件名=\%s\“\r\n”、边界、名称表单、文件名); sprintf(缓冲区,“%s内容类型:%s\r\n\r\n”,缓冲区,类型); sprintf(缓冲区,“%s%s\r\n”,缓冲区,内容); sprintf(缓冲区,“%s-->s-->\r\n”,缓冲区,边界); //开放互联网连接 HINTERNET hSession=InternetOpen(“WinSock”,INTERNET\u OPEN\u TYPE\u prefig,NULL,NULL,0); 如果(hSession==NULL)返回错误\u互联网\u打开; HINTERNET hConnect=InternetConnect(会话、iaddr、INTERNET\u默认\u HTTP\u端口、NULL、NULL、INTERNET\u服务\u HTTP、0、1); 如果(hConnect==NULL)返回错误\u INTERNET\u CONN; HINTERNET hRequest=HttpOpenRequest(hConnect,(const char*)“POST”,url,NULL,NULL,(const char**)”*/*\0,0,1); 如果(hRequest==NULL)返回错误\u互联网\u请求; BOOL sent=HttpSendRequest(hRequest、hdrs、strlen(hdrs)、buffer、strlen(buffer)); 如果(!sent)返回错误\u INTERNET\u SEND; //关闭所有有效的internet句柄 InternetCloseHandle(hsSession); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); 返回0; },file,post,upload,wininet,File,Post,Upload,Wininet,服务器端有一个PHP脚本,它使用存储在$\u FILES变量中的信息执行文件保存。 该代码适用于TXT文件,但不适用于JPG(图像)文件:在本例中,文件已正确创建并命名为服务器端,但为空!我认为这与我读取二进制文件并将其写入字符串有关。 有什么建议吗 谢谢您不应该使用sprintf(缓冲区,“%s%s\r\n”,缓冲区,内容)在二进制数据上。它不是字符串,因此%s是错误的 您需要使用适当的内容传输编码对其进行base64编码,或者使用带有内容长度头的memcpy。谢谢,在通过POST请求发送数据

服务器端有一个PHP脚本,它使用存储在$\u FILES变量中的信息执行文件保存。 该代码适用于TXT文件,但不适用于JPG(图像)文件:在本例中,文件已正确创建并命名为服务器端,但为空!我认为这与我读取二进制文件并将其写入字符串有关。 有什么建议吗


谢谢

您不应该使用
sprintf(缓冲区,“%s%s\r\n”,缓冲区,内容)在二进制数据上。它不是字符串,因此
%s
是错误的


您需要使用适当的
内容传输编码对其进行base64编码,或者使用带有
内容长度
头的memcpy。

谢谢,在通过POST请求发送数据之前,我实现了文件的base64编码,现在它可以工作了!然而,我还必须从服务器端管理解码。这是通过web表单上传文件时通常不做的事情,为什么会有这种差异?是否在标题中添加了
内容传输编码:base64
?如果你这样做,你会发现服务器会自动为你解码。