httpdownloaddlg.cpp

来自「实现WIN CE下文件的下载,小试牛刀,希望有用」· C++ 代码 · 共 256 行

CPP
256
字号
// httpdownloadDlg.cpp : implementation file
//

#include "stdafx.h"
#include "httpdownload.h"
#include "httpdownloadDlg.h"

//#include "Wininet.h"
//#include <mmsystem.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHttpdownloadDlg dialog
void CStrToChar(LPTSTR str,char * aim)
{
	if (wcslen(str)<=0) {
		return;
	}
	for (int i=0;i<wcslen(str);i++)
	{
		aim[i]=str[i];
	}
	return;
}
DWORD WINAPI OnTestStatus(LPVOID lpvoid)
{
	CHttpdownloadDlg *pDlg = (CHttpdownloadDlg*)lpvoid;
	while(TRUE)
	{	
		Sleep(10);
		if (WaitForSingleObject(pDlg->m_CloseEvent,500)==WAIT_OBJECT_0)
		{
			break ;
		}
		pDlg->down();	
	}
	return 0;
}
CHttpdownloadDlg::CHttpdownloadDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHttpdownloadDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHttpdownloadDlg)
	m_jishu = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHttpdownloadDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHttpdownloadDlg)
	DDX_Text(pDX, IDC_EDIT1, m_jishu);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHttpdownloadDlg, CDialog)
	//{{AFX_MSG_MAP(CHttpdownloadDlg)
	ON_BN_CLICKED(IDC_BUT_DOWNLOAD, OnButDownload)
	ON_BN_CLICKED(IDC_BUT_OK, OnButOk)
	ON_BN_CLICKED(IDC_BUT_QIDONG, OnButQidong)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHttpdownloadDlg message handlers

BOOL CHttpdownloadDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	SetWindowPos(&wndTopMost,0,0,240,320,SWP_SHOWWINDOW);
	CString str1,str2;
	str1=L"http://119.123.230.32/spSalesTBS1.0/DbTest.rar";//服务器资源地址
	str2=_T("\\Storage Card\\DbTest.rar");
	GetDlgItem(IDC_FILE_URL)->SetWindowText(str1);
	GetDlgItem(IDC_FILE_DIR)->SetWindowText(str2);
	m_CloseEvent=INVALID_HANDLE_VALUE;
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}
void CHttpdownloadDlg::down()
{
	CString str;
	str=_T("0KB/s");
	GetDlgItem(IDC_SPEED)->SetWindowText(str);
	str=_T("0%");
	GetDlgItem(IDC_PRECENT)->SetWindowText(str);
	CString url,filename;
	GetDlgItemText(IDC_FILE_URL,url);
	GetDlgItem(IDC_FILE_DIR)->GetWindowText(filename);
//	GetDlgItemText(IDC_LOCAL_DIRECTORY,filename);
	if(url.IsEmpty())
	{
		AfxMessageBox(L"请输入Internet文件的URL!");
		return;
	}
	if(filename.IsEmpty())
	{
		AfxMessageBox(L"请输入需要保存的文件路径!");
		return;
	}
	if(InternetGetFileDown(url,filename)==0)
	{
		//AfxMessageBox(L"下载成功!");
		m_jishu++;
		UpdateData(FALSE);
	}
/*	else
	{
		AfxMessageBox(L"下载失败!");
	}	*/
}
void CHttpdownloadDlg::OnButDownload() 
{
	CString url,filename;
	GetDlgItemText(IDC_FILE_URL,url);
	GetDlgItem(IDC_FILE_DIR)->GetWindowText(filename);
//	GetDlgItemText(IDC_LOCAL_DIRECTORY,filename);
	if(url.IsEmpty())
	{
		AfxMessageBox(L"请输入Internet文件的URL!");
		return;
	}
	if(filename.IsEmpty())
	{
		AfxMessageBox(L"请输入需要保存的文件路径!");
		return;
	}
	if(InternetGetFileDown(url,filename)==0)
	{
		AfxMessageBox(L"下载成功!");
	}
	else
	{
		AfxMessageBox(L"下载失败!");
	}	
}

int CHttpdownloadDlg::InternetGetFileDown(CString szUrl,CString szFileName)
{
	DWORD dwFlags;
	InternetGetConnectedState(&dwFlags, 0);
	CHAR strAgent[64];
	sprintf(strAgent, "Agent%ld", GetTickCount());
	HINTERNET hOpen;
	if(!(dwFlags & INTERNET_CONNECTION_PROXY))
		hOpen = InternetOpenA(strAgent, INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
	else
		hOpen = InternetOpenA(strAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	if(!hOpen)
	{
		AfxMessageBox(L"Internet连接错误!");
		return -1;
	}
	DWORD dwSize;
//	CHAR   szHead[] = "Accept: */*\r\n\r\n";
	CString szHead=_T("Accept: */*\r\n\r\n");
	HINTERNET  hConnect;

	char ch[100];
	memset(ch,0,100);
	CStrToChar(szUrl.GetBuffer(0),ch);
	CFile file;
	if(!(hConnect = InternetOpenUrl( hOpen, szUrl, szHead,szHead.GetLength(), INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0)))
//	if ( !(hConnect = InternetOpenUrlA( hOpen, ch, szHead,sizeof(szHead), INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0)))
	{
	   AfxMessageBox(L"不能打开该URL!");
	   return -1;
	}
	if(file.Open(szFileName,CFile::modeWrite|CFile::modeCreate)==FALSE )
	{
	   AfxMessageBox(L"不能打开本地的文件!");
	  return -1;
	}
	DWORD dwByteToRead = 0;
	DWORD dwSizeOfRq = 4;
	DWORD dwBytes = 0;

    if (!HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, 
                  (LPVOID)&dwByteToRead, &dwSizeOfRq, NULL))
	{
		dwByteToRead = 0;
	}
	DWORD start;
	DWORD end;
	DWORD time;
	CString tempstring;
	time = 10;
	start = GetTickCount();
	char   *f_tmp=(char*)malloc(1024);
	memset(f_tmp,0,1024); 
	do
	{
	    if (!InternetReadFile (hConnect,(LPVOID)f_tmp,1023, &dwSize))
		{
			AfxMessageBox(L"读文件出错!");
			file.Close();
			return -1;
		}
		if (dwSize==0)
			break;
		else
			file.Write((LPVOID)f_tmp,dwSize);
		dwBytes+=dwSize;
		if(dwByteToRead)
		{
			tempstring.Format(L"%d%%",(dwBytes*100)/dwByteToRead);
			SetDlgItemText(IDC_PRECENT,tempstring);
		}
		FLOAT fSpeed = 0;
		fSpeed = (float)dwBytes;
		end = GetTickCount();
		time = end - start;
		if(time == 0)
			time = 10;
		fSpeed /= ((float)time)/1000.0f;
		fSpeed /= 1024.0f;
		tempstring.Format(L"%dKB/s",fSpeed);
		SetDlgItemText(IDC_SPEED,tempstring);		
	}while(TRUE);
	file.Close();
	InternetCloseHandle(hOpen);	
	return 0;
}

void CHttpdownloadDlg::OnButOk() 
{
	DeleteFile(_T("\\Storage Card\\DbTest.rar"));
	SetEvent(m_CloseEvent);
	if (WaitForSingleObject(thread,1000)==WAIT_TIMEOUT)
	{
		TerminateThread(thread,0);
	}
	m_CloseEvent=NULL;
	this->SendMessage(WM_CLOSE);
}

void CHttpdownloadDlg::OnButQidong() 
{
	m_CloseEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
	thread=CreateThread(NULL, 0, OnTestStatus, this, 0,&dwThreadID1);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?