www.pudn.com > evc_http.rar > testDlg.cpp


// testDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "test.h" 
#include "testDlg.h" 
#include "wininet.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CTestDlg dialog 
 
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CTestDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CTestDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CTestDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CTestDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CTestDlg, CDialog) 
	//{{AFX_MSG_MAP(CTestDlg) 
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CTestDlg message handlers 
 
BOOL CTestDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// 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 
} 
 
BOOL DownloadMp3(LPTSTR lpUrl, LPTSTR lpFileName, BOOL bSave) 
{ 
	HINTERNET	hSession; 
	HINTERNET	hConnect; 
	HINTERNET	hService; 
	HANDLE		hFile; 
	LPTSTR		pBuffer; 
	BYTE		pBuffTmp[1024]; 
	DWORD		dwWritten; 
	DWORD		dwBytesRead; 
	DWORD		dwSize = 0; 
	LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL}; 
 
	// Win32 Internet functions init 
	hSession = InternetOpen(TEXT("Mp3DownloaderCE"), INTERNET_OPEN_TYPE_PRECONFIG,  
		NULL, 0, 0); 
 
	if (!hSession) 
		return FALSE; 
 
	// Reading HTTP URL 
	hConnect = InternetConnect(hSession, lpUrl, INTERNET_INVALID_PORT_NUMBER, 
		NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); 
 
	if (!hConnect) 
	{ 
		InternetCloseHandle(hSession); 
		return FALSE; 
	} 
     
	// Open an HTTP request handle 
	hService = HttpOpenRequest(hConnect, TEXT("GET"), NULL, HTTP_VERSION, 
		NULL, (LPCTSTR *)AcceptTypes,  
		INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, 0); 
 
	if (!hService) 
	{ 
		InternetCloseHandle(hSession); 
		InternetCloseHandle(hConnect); 
		return FALSE; 
	} 
	// Send a request to the HTTP server 
	HttpSendRequest(hService, NULL, 0, NULL, 0); 
 
	pBuffer = (LPTSTR)malloc(sizeof(BYTE) * 1024); 
 
	// Read data 
	while (InternetReadFile(hService, pBuffTmp, 1024, &dwBytesRead)) 
	{ 
		if (dwBytesRead == 0) 
			break; 
 
		memcpy(pBuffer + dwSize, pBuffTmp, dwBytesRead); 
		dwSize += dwBytesRead; 
		pBuffer = (LPTSTR)realloc(pBuffer, dwSize + 1024); 
//		g_nProgress++; 
	} 
 
	// Create file if necessary 
	if (bSave) 
	{ 
		hFile = CreateFile(lpFileName, GENERIC_WRITE, FILE_SHARE_READ, 
			NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
 
		if (hFile == INVALID_HANDLE_VALUE) 
			return FALSE; 
 
		if (!(WriteFile(hFile, pBuffer, dwSize, &dwWritten, NULL))) 
			return FALSE; 
 
		CloseHandle(hFile); 
	} 
 
	// Close Internet handle 
	InternetCloseHandle(hSession); 
	InternetCloseHandle(hConnect); 
	InternetCloseHandle(hService); 
 
	free(pBuffer); 
	pBuffer = NULL; 
		 
	return TRUE; 
} 
 
void CTestDlg::OnButton1()  
{ 
		// TODO: Add your control notification handler code here 
	BOOL ret; 
	//AfxMessageBox(_T("BEGIN")); 
	ret= DownloadMp3(_T("http://www.zxcxc.com/wp-content/uploads/2007/11/zjl-qhc.mp3"), _T("1234.mp3"), true); 
	//ret= DownloadMp3(_T("http://www.zxcxc.com/wp-content/uploads/2007/11/zjl-qhc.mp3"), _T("1234.mp3"), true); 
	if(ret) 
		AfxMessageBox(_T("OK")); 
	else 
		AfxMessageBox(_T("ERR")); 
	 
}