如何文件通过http的post方法上传到服务器? 文件通过http的post方法上传到服务器?求例子,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #include <Windows.h>#include <WinINet.h>#include <stdio.h>BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize);#define BUFFSIZE 500void main( int argc, char **argv ){ DWORD dwPostSize; if (argc < 4) { printf("Usage: Bigpost <Size> <Server> <Path>\n"); printf("<Size> is the number of KB to POST\n"); printf("<Server> is the server to POST to\n"); printf("<Path> is the virtual path to POST to\n"); exit(0); } if ( ((dwPostSize = strtoul(argv[1],NULL,10)) == 0) || (dwPostSize >= 2047999) ) { printf("%s is invalid size. Valid sizes are from 1 to 2047999\n", argv[1]); exit(0); } printf( "Test of POSTing %luKB with WinInet\n", dwPostSize); dwPostSize *= 1024; // Convert KB to bytes HINTERNET hSession = InternetOpen( "HttpSendRequestEx", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if(!hSession) { printf("Failed to open session\n"); exit(0); } HINTERNET hConnect = InternetConnect(hSession, argv[2], INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL); if (!hConnect) printf( "Failed to connect\n" ); else { HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", argv[3], NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0); if (!hRequest) printf( "Failed to open request handle\n" ); else { if(UseHttpSendReqEx(hRequest, dwPostSize)) { char pcBuffer[BUFFSIZE]; DWORD dwBytesRead; printf("\nThe following was returned by the server:\n"); do { dwBytesRead=0; if(InternetReadFile(hRequest, pcBuffer, BUFFSIZE-1, &dwBytesRead)) { pcBuffer[dwBytesRead]=0x00; // Null-terminate buffer printf("%s", pcBuffer); } else printf("\nInternetReadFile failed"); }while(dwBytesRead>0); printf("\n"); } if (!InternetCloseHandle(hRequest)) printf( "Failed to close Request handle\n" ); } if(!InternetCloseHandle(hConnect)) printf("Failed to close Connect handle\n"); } if( InternetCloseHandle( hSession ) == FALSE ) printf( "Failed to close Session handle\n" ); printf( "\nFinished.\n" );}BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize){ INTERNET_BUFFERS BufferIn; DWORD dwBytesWritten; int n; BYTE pBuffer[1024]; BOOL bRet; BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur BufferIn.Next = NULL; BufferIn.lpcszHeader = NULL; BufferIn.dwHeadersLength = 0; BufferIn.dwHeadersTotal = 0; BufferIn.lpvBuffer = NULL; BufferIn.dwBufferLength = 0; BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize BufferIn.dwOffsetLow = 0; BufferIn.dwOffsetHigh = 0; if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0)) { printf( "Error on HttpSendRequestEx %d\n",GetLastError() ); return FALSE; } FillMemory(pBuffer, 1024, 'D'); // Fill buffer with data bRet=TRUE; for(n=1; n<=(int)dwPostSize/1024 && bRet; n++) { if(bRet=InternetWriteFile( hRequest, pBuffer, 1024, &dwBytesWritten)) printf( "\r%d bytes sent.", n*1024); } if(!bRet) { printf( "\nError on InternetWriteFile %lu\n",GetLastError() ); return FALSE; } if(!HttpEndRequest(hRequest, NULL, 0, 0)) { printf( "Error on HttpEndRequest %lu \n", GetLastError()); return FALSE; } return TRUE;} 利用HTTP方式上传http://www.xiaozhou.net/cooldog/blogview.asp?logID=57 参考的rfc文档rfc2068(HTTP1.1)rfc1867(html form) 用socket创建一个连接然后post http头部信息 \r\n\r\n文件内容\r\n关键是头部信息一定要写正确,可以用软件跟踪一下ie上传的时候所填写的头部信息其中部分是不需要的。 http://search.csdn.net/Expert/topic/1433/1433869.xml?temp=.4139826 如果是XML文件可以考虑用XMLHttpMSXML2::IXMLHTTPRequestPtr pXMLHTTPRequest;pXMLHTTPRequest->open("post","getVehicles.asp",false);pXMLHTTPRequest->send(pXMLDoc->xml); 两个edit数组相乘问题 (急啊)regsvr32注册失败 打印发现的重大问题(急!!!!!!!) 刚工作,遇难题! SOS:老师要求做一个串口通信的程序 奇怪的错误!!! 大家帮忙!怎么获得ListCtrl控件的某一项的索引号和内容?多谢! 如何把Menu显示在对话框的左上角?(在线等候) help!!!!!!! 这是怎么个回事? 关于ado建数据库的一段代码,怎么也调不通,请求帮助,急!!!!!!!!!!!!! 请问VC++6.0有没有升极版,它与VC++6.0有什么区别?
#include <WinINet.h>
#include <stdio.h>BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize);
#define BUFFSIZE 500void main( int argc, char **argv )
{
DWORD dwPostSize; if (argc < 4)
{
printf("Usage: Bigpost <Size> <Server> <Path>\n");
printf("<Size> is the number of KB to POST\n");
printf("<Server> is the server to POST to\n");
printf("<Path> is the virtual path to POST to\n");
exit(0);
} if ( ((dwPostSize = strtoul(argv[1],NULL,10)) == 0) || (dwPostSize >= 2047999) )
{
printf("%s is invalid size. Valid sizes are from 1 to 2047999\n", argv[1]);
exit(0);
} printf( "Test of POSTing %luKB with WinInet\n", dwPostSize); dwPostSize *= 1024; // Convert KB to bytes HINTERNET hSession = InternetOpen( "HttpSendRequestEx", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if(!hSession)
{
printf("Failed to open session\n");
exit(0);
}
HINTERNET hConnect = InternetConnect(hSession, argv[2], INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
if (!hConnect)
printf( "Failed to connect\n" );
else
{
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", argv[3],
NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (!hRequest)
printf( "Failed to open request handle\n" );
else
{
if(UseHttpSendReqEx(hRequest, dwPostSize))
{
char pcBuffer[BUFFSIZE];
DWORD dwBytesRead; printf("\nThe following was returned by the server:\n");
do
{ dwBytesRead=0;
if(InternetReadFile(hRequest, pcBuffer, BUFFSIZE-1, &dwBytesRead))
{
pcBuffer[dwBytesRead]=0x00; // Null-terminate buffer
printf("%s", pcBuffer);
}
else
printf("\nInternetReadFile failed");
}while(dwBytesRead>0);
printf("\n");
}
if (!InternetCloseHandle(hRequest))
printf( "Failed to close Request handle\n" );
}
if(!InternetCloseHandle(hConnect))
printf("Failed to close Connect handle\n");
}
if( InternetCloseHandle( hSession ) == FALSE )
printf( "Failed to close Session handle\n" ); printf( "\nFinished.\n" );
}BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize)
{
INTERNET_BUFFERS BufferIn;
DWORD dwBytesWritten;
int n;
BYTE pBuffer[1024];
BOOL bRet; BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur
BufferIn.Next = NULL;
BufferIn.lpcszHeader = NULL;
BufferIn.dwHeadersLength = 0;
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0; if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0))
{
printf( "Error on HttpSendRequestEx %d\n",GetLastError() );
return FALSE;
} FillMemory(pBuffer, 1024, 'D'); // Fill buffer with data bRet=TRUE;
for(n=1; n<=(int)dwPostSize/1024 && bRet; n++)
{
if(bRet=InternetWriteFile( hRequest, pBuffer, 1024, &dwBytesWritten))
printf( "\r%d bytes sent.", n*1024);
}
if(!bRet)
{
printf( "\nError on InternetWriteFile %lu\n",GetLastError() );
return FALSE;
} if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
} return TRUE;
}
rfc2068(HTTP1.1)
rfc1867(html form)
关键是头部信息一定要写正确,可以用软件跟踪一下ie上传的时候所填写的头部信息
其中部分是不需要的。
MSXML2::IXMLHTTPRequestPtr pXMLHTTPRequest;
pXMLHTTPRequest->open("post","getVehicles.asp",false);
pXMLHTTPRequest->send(pXMLDoc->xml);