如果要想程序有断点下载功能,其实很简单,在使用HttpSendRequest发送请求之前,只需要在发送请求之前添加头信息:Range:bytes=1000-,则服务器会从文件的1000byte处开始传送文件直到结束,如果想下载文件的某一部分,则可使用Range:bytes=1000-2000。
TCHAR szSize[MAX_PATH] = {0}; //using this Content-Type POST == GET
// printf("file length222222 = %d/n", dwSizeExist);
wsprintf (szSize, L"Range:bytes=%d-", dwSizeExist);
HttpAddRequestHeaders(m_hRequest,
szSize,
-1,
HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
下面是上传代码,注意HttpAddRequestHeaders,它用来添加头信息,比如你上传的文件类型,编码方式等,我这里是因为文本文件采用了utf-8编码,所以才添加此头信息。具体类型根据自己情况来,也可参考http头信息
BOOL UploadFile( TCHAR *szFileName)
{
BOOL retVal = FALSE;
m_hSession = NULL;
m_hConnection = NULL;
m_hRequest = NULL;
char buffer [1024] = {0} ;
HANDLE createfile;
createfile=CreateFile(szFileName,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (createfile==INVALID_HANDLE_VALUE)
{
return retVal;
}
m_hSession = InternetOpen(HTTP_USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (m_hSession == NULL)
{
//printf("Internet open error!");
CloseHandle(createfile);
return retVal;
}
m_hConnection = InternetConnect(m_hSession, SERVER_IP, SERVER_PORT,L"",L"",INTERNET_SERVICE_HTTP, 0, 0);
if (m_hConnection == NULL)
{
//printf("InternetConnection Failure!");
InternetCloseHandle(m_hSession);
CloseHandle(createfile);
return retVal;
}
LPCTSTR accept[] = {
_T("*/*"),
NULL
};
m_hRequest = HttpOpenRequest(m_hConnection,L"POST",UPLOAD_CONTACTLIST_WEB,
HTTP_VERSION_1_1, NULL, accept, INTERNET_FLAG_DONT_CACHE, 0);
if (m_hRequest == NULL)
{
//printf("InternetRequest Failure!");
InternetCloseHandle(m_hSession);
InternetCloseHandle(m_hConnection);
CloseHandle(createfile);
return retVal;
}
TCHAR szContentType[] = L"Content-Type:application/x-www-form-urlencoded;charset=utf-8/r/n"; //using this Content-Type POST == GET
HttpAddRequestHeaders(m_hRequest,
szContentType,
-1,
HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);
DWORD dwRead;
DWORD nBytesRead;
INTERNET_BUFFERS BufferIn = {0};
FillMemory(&BufferIn, sizeof(BufferIn), 0);
BufferIn.dwBufferTotal = GetFileSize (createfile, NULL);
//这里是你要传送数据的总大小,比如一个文件的大小::GetFileSize(hFile, 0)
BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
BufferIn.dwBufferLength = sizeof(buffer);
//分次上传的话,每次上传数据的大小,比如4里面的dwBufSize
if(!HttpSendRequestEx( m_hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
{
//printf( "Error on HttpSendRequestEx %lu/n",GetLastError() );
InternetCloseHandle(m_hSession);
InternetCloseHandle(m_hConnection);
InternetCloseHandle(m_hRequest);
CloseHandle(createfile);
return retVal;
}
BOOL hwrite = FALSE; //check read file whether success!
while (1)
{
nBytesRead = 0;
hwrite=ReadFile(createfile,buffer,sizeof(buffer),&nBytesRead,NULL);
if (hwrite)
{
if (nBytesRead != 0)
{
dwRead = 0;
if (InternetWriteFile(m_hRequest, buffer, nBytesRead, &dwRead))
{
//printf("Send File length:%d !/n",dwRead);
}
}
else
{
//printf("Send File over!");
break;
}
}
}
if(!HttpEndRequest(m_hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu /n", GetLastError());
InternetCloseHandle(m_hSession);
InternetCloseHandle(m_hConnection);
InternetCloseHandle(m_hRequest);
CloseHandle(createfile);
return retVal ;
}
LPSTR lpszData; //buffer for the data
DWORD dwSize; //size of the data available
DWORD dwDownloaded;
FILE *hResult = NULL;
TCHAR szResultFileName[MAX_PATH] = {0};
GetModuleFileName(NULL, szResultFileName, MAX_PATH);
AslGetFilePath(szResultFileName);
wcscat(szResultFileName, L"result.json");
hResult = _tfopen(szResultFileName,L"wb");//use to check whether Upload succeed.
if (hResult == NULL)
{
AslWriteLog("Create result.json File error!");
}
while(1)
{
// The call to InternetQueryDataAvailable determines the amount of
// data available to download.
if (!InternetQueryDataAvailable(m_hRequest,&dwSize,0,0))
{
//printf("NO RESPONSE!");
break;
}
else
{
// Allocates a buffer of the size returned by InternetQueryDataAvailable
lpszData = new char[dwSize+1];
// Reads the data from the HINTERNET handle.
if(!InternetReadFile(m_hRequest,(LPVOID)lpszData,dwSize,&dwDownloaded))
{
delete[] lpszData;
break;
}
else
{
if (dwDownloaded == 0)
{
break;
}
// Adds a null terminator to the end of the data buffer
fwrite(lpszData, 1, dwSize,hResult);
// Delete the two buffers
delete[] lpszData;
// Check the size of the remaining data. If it is zero, break.
}
}
}
if(hResult !=NULL)
{
fclose(hResult);
if (JsonTanslate(szResultFileName))
{
retVal = TRUE;
}
hResult = NULL;
}
CloseHandle(createfile);
InternetSetStatusCallback( m_hSession, NULL );
InternetCloseHandle(m_hSession);
InternetCloseHandle(m_hConnection);
InternetCloseHandle(m_hRequest);
return retVal;
}
