这个代码是从网上拷的,说是可以上传文件,可是我运行结果怎么就没能上传成功呢?那有问题吗?
(我启动的是tomcat服务器)
#include <afxdtctl.h>
#include <Windows.h>
#include <WinINet.h>
#include <stdio.h>
BOOL UpdateFile(HINTERNET hConnect, TCHAR *upFile);
#define BUFFSIZE 500
void main( int argc, char **argv )
{
DWORD dwPostSize; 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, "http://127.0.0.1", 8000,//我的服务器端口
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
if (!hConnect)
printf( "Failed to connect\n" );
else
{
UpdateFile(hConnect,"F:\\post.htm");
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 UpdateFile(HINTERNET hConnect, TCHAR *upFile)
{
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
BOOL bRead, bRet; BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); HINTERNET hRequest = HttpOpenRequest (hConnect, "PUT",
"/page.htm", NULL, NULL, NULL, 0, 0);
if (!hRequest)
{
printf("Failed to open request handle: %lu\n", GetLastError ());
return FALSE;
} HANDLE hFile = CreateFile (upFile, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("\nFailed to open local file %s.", upFile);
return FALSE;
} BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
printf ("File size is %d\n", BufferIn.dwBufferTotal ); if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
{
printf( "Error on HttpSendRequestEx %lu\n",GetLastError() );
return FALSE;
} DWORD sum = 0;
do
{
if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
&dwBytesRead, NULL)))
{
printf ("\nReadFile failed on buffer %lu.",GetLastError());
break;
}
if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
&dwBytesWritten)))
{
printf ("\nInternetWriteFile failed %lu", GetLastError());
break;
}
sum += dwBytesWritten;
}
while (dwBytesRead == sizeof(pBuffer)) ; CloseHandle (hFile);
printf ("Actual written bytes: %d\n", sum); if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}
return TRUE;
}
另外,我的机子是通过代理上网的,每次运行的时候防火墙都会提示访问代理服务器。
运行过程中没有出错,就是没找到上传的文件,奇怪!
和这段代码相关的网页:
http://support.microsoft.com/kb/184352/EN-US/
http://support.microsoft.com/kb/177188/EN-US/
(我启动的是tomcat服务器)
#include <afxdtctl.h>
#include <Windows.h>
#include <WinINet.h>
#include <stdio.h>
BOOL UpdateFile(HINTERNET hConnect, TCHAR *upFile);
#define BUFFSIZE 500
void main( int argc, char **argv )
{
DWORD dwPostSize; 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, "http://127.0.0.1", 8000,//我的服务器端口
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
if (!hConnect)
printf( "Failed to connect\n" );
else
{
UpdateFile(hConnect,"F:\\post.htm");
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 UpdateFile(HINTERNET hConnect, TCHAR *upFile)
{
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
BOOL bRead, bRet; BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); HINTERNET hRequest = HttpOpenRequest (hConnect, "PUT",
"/page.htm", NULL, NULL, NULL, 0, 0);
if (!hRequest)
{
printf("Failed to open request handle: %lu\n", GetLastError ());
return FALSE;
} HANDLE hFile = CreateFile (upFile, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("\nFailed to open local file %s.", upFile);
return FALSE;
} BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
printf ("File size is %d\n", BufferIn.dwBufferTotal ); if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
{
printf( "Error on HttpSendRequestEx %lu\n",GetLastError() );
return FALSE;
} DWORD sum = 0;
do
{
if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
&dwBytesRead, NULL)))
{
printf ("\nReadFile failed on buffer %lu.",GetLastError());
break;
}
if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
&dwBytesWritten)))
{
printf ("\nInternetWriteFile failed %lu", GetLastError());
break;
}
sum += dwBytesWritten;
}
while (dwBytesRead == sizeof(pBuffer)) ; CloseHandle (hFile);
printf ("Actual written bytes: %d\n", sum); if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}
return TRUE;
}
另外,我的机子是通过代理上网的,每次运行的时候防火墙都会提示访问代理服务器。
运行过程中没有出错,就是没找到上传的文件,奇怪!
和这段代码相关的网页:
http://support.microsoft.com/kb/184352/EN-US/
http://support.microsoft.com/kb/177188/EN-US/
http://search.csdn.net/Expert/topic/955/955970.xml?temp=.3590814
HINTERNET hConnect = InternetConnect(hSession, "http://127.0.0.1",80,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
居然也没说没连上,真奇怪!后面的操作都好像很正常。
不要放在系统盘下面
#include <afxdtctl.h>
#include <Windows.h>
#include <WinINet.h>
#include <stdio.h>
BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize,CString strLocalFile);
BOOL Upload(CString bstrLocalFile,CString bstrServerIP,CString strServerPort,CString bstrRemoteFile);
#define BUFFSIZE 500void main( int argc, char **argv )
{ if (argc < 5)
{
printf("Usage: Bigpost <LocalFile> <ServerIP><ServerPort><ReomteFile>\n");
printf("<LocalFile> is the local file to POST\n");
printf("<ServerIP> is the server's IP to POST to\n");
printf("<ServerPort> is the server's Port to POST to\n");
printf("<ReomteFile> is the virtual path to POST to\n");
exit(0);
}
Upload(argv[1],argv[2],argv[3],argv[4]);
}
BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize,CString strLocalFile)
{
DWORD dwRead;
BYTE* buffer;
printf("Local file:%s\n",strLocalFile);
FILE* fLocal;
if((fLocal=fopen(strLocalFile,"rb"))==NULL){
printf("Can't open the file:%s,maybe it doesn't exist!\n",strLocalFile);
return false;
}
fseek(fLocal,0L,SEEK_END);
dwRead=ftell(fLocal);
rewind(fLocal);
buffer=(BYTE *)malloc(dwRead);
if(!buffer){
printf("not enough memory!\n");
return false;
}
printf("length of file:%d\n",dwRead);
dwRead=fread(buffer,1,dwRead,fLocal);
dwPostSize=dwRead; INTERNET_BUFFERS BufferIn;
DWORD dwBytesWritten;
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;
}
bRet=TRUE;
if(bRet=InternetWriteFile( hRequest, buffer, dwPostSize, &dwBytesWritten))
printf( "\r%d bytes sent.", dwPostSize);
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;
}
fclose(fLocal);
free(buffer);
return TRUE;
}BOOL Upload(CString strLocalFile,CString strServerIP,CString strServerPort,CString strRemoteFile){
DWORD dwPostSize=0;
int intServerPort=atoi(strServerPort);
HINTERNET hSession = InternetOpen( "HttpSendRequestEx", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if(!hSession)
{
printf("Failed to open session\n");
return FALSE;
}
HINTERNET hConnect = InternetConnect(hSession, strServerIP, intServerPort,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
if (!hConnect){
printf( "Failed to connect\n" );
return FALSE;
}else{
HINTERNET hRequest = HttpOpenRequest(hConnect, "PUT", strRemoteFile,
NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (!hRequest){
printf( "Failed to open request handle\n" );
}else{
if(UseHttpSendReqEx(hRequest, dwPostSize,strLocalFile))
{
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" );
return FALSE;
}
printf( "\nFinished.\n" );
return TRUE;
}
我在命令行测试如下:
C:\>upload C:\upload.exe 192.1.1.18 80 test.exe
Local file:C:\upload.exe
length of file:692224
692224 bytes sent.
The following was returned by the server:Finished.如有问题请多指点,谢谢!
Linking...
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/xf.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
是怎么回事
然后试试,应该没问题了