📄 httpandftptestdlg.cpp
字号:
// HttpAndFtpTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "HttpAndFtpTest.h"
#include "HttpAndFtpTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHttpAndFtpTestDlg dialog
CHttpAndFtpTestDlg::CHttpAndFtpTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHttpAndFtpTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHttpAndFtpTestDlg)
m_strStatic = _T("");
m_strSavePath = _T("");
m_hSession = NULL;
m_pszFileBuffer = NULL;
m_bQuerySize = TRUE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHttpAndFtpTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHttpAndFtpTestDlg)
DDX_Control(pDX, IDC_PROGRESS1, m_ProgressCtrl);
DDX_Text(pDX, IDC_STATIC_PROGRESS, m_strStatic);
DDX_Text(pDX, IDC_EDIT_SAVEPATH, m_strSavePath);
DDX_Check(pDX, IDC_CHECK_NOQUERYSIZE, m_bQuerySize);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHttpAndFtpTestDlg, CDialog)
//{{AFX_MSG_MAP(CHttpAndFtpTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_HTTP_DOWNLOAD, OnHttpDownload)
ON_BN_CLICKED(IDC_FTP_DOWNLOAD, OnFtpDownload)
ON_BN_CLICKED(IDC_BUTTON_URLDOWN, OnButtonUrldown)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_SHOWRESULT, OnButtonShowresult)
ON_BN_CLICKED(IDC_BUTTON_HELP, OnButtonHelp)
ON_BN_CLICKED(IDC_BUTTON_CREATEDOWNLOADFOLDER, OnButtonCreatedownloadfolder)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHttpAndFtpTestDlg message handlers
BOOL CHttpAndFtpTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
BOOL bSuccess = InitializeHintnet(&m_hSession);
if (!bSuccess) {
MessageBox("Initialize Hintnet fail!");
return TRUE;
}
memset(&m_URLS,0,sizeof(URLS));
m_URLS.ppszUrls = new char*[500];
GetUrlsFromTxt("urlset.txt");
SetTimer(1253,10,NULL);
char szSavePath[100];
GetPrivateProfileString("Pubic","SavePath","",szSavePath,100,"./HttpandFtptest.ini");
m_strSavePath = szSavePath;
UpdateData(FALSE);
m_strResultFiles = m_strSavePath+"\\"+"result.txt";
return TRUE; // return TRUE unless you set the focus to a control
}
void CHttpAndFtpTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CHttpAndFtpTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
UINT ReceiveDataThread(LPVOID lpParam);
BOOL bHasRun = FALSE;
enum
{
INTERNET_ERROR_OPENURL=1,
INTERNET_ERROR_FILEOPEN,
INTERNET_ERROR_READFILE,
INTERNET_ERROR_OPEN
};
UINT InternetGetFile (HINTERNET IN hConnect, // Handle from InternetOpen()
LPCSTR szFileName)
{
// DWORD dwByteToRead = 0;
// DWORD dwSizeOfRq = 4;
//
// if (!HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
// (LPVOID)&dwByteToRead, &dwSizeOfRq, NULL))
// {
// dwByteToRead = 0;
// }
//////////////////////////////////////////////////////////////////////////
/*
SYSTEMTIME m_stLastModify;
dwSizeOfRq = sizeof(SYSTEMTIME);
if (!HttpQueryInfo(hConnect, HTTP_QUERY_LAST_MODIFIED | HTTP_QUERY_FLAG_SYSTEMTIME,
(LPVOID)&m_stLastModify, &dwSizeOfRq, NULL))
{
DWORD dwError = GetLastError();
strTemp.Format("Query Modified Error!ErrorCode:%d",dwError);
AfxMessageBox(strTemp);
}
SYSTEMTIME systime;
SystemTimeToTzSpecificLocalTime(NULL,&m_stLastModify,&systime);
/// strTemp.Format("%d%d%d %02d:%02d:%02d",systime.wYear,systime.wMonth,systime.wDay,systime.wHour,systime.wMinute,systime.wSecond);
// TRACE(strTemp);
////*//////////////////////////////////////////////////////////////////////
FILE * pFile;
if ( !(pFile = fopen (szFileName, "wb" ) ) )
{
return INTERNET_ERROR_FILEOPEN;
}
VOID* szTemp[16384];
DWORD dwSize;
while (TRUE)
{
// Keep coping in 16 KB chunks, while file has any data left.
// Note: bigger buffer will greatly improve performance.
if (!InternetReadFile (hConnect, szTemp, 16384, &dwSize) )
{
fclose (pFile);
return INTERNET_ERROR_READFILE;
}
if (!dwSize)
break; // Condition of dwSize=0 indicate EOF. Stop.
else
fwrite(szTemp, sizeof (char), dwSize , pFile);
} // do
fflush (pFile);
fclose (pFile);
return 0;
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CHttpAndFtpTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CHttpAndFtpTestDlg::urlDownload()
{
CString strMessage;
WriteMsgIntoFile(m_strResultFiles,"----------------UrlDownload Strat----------------");
CTime time = CTime::GetCurrentTime();
strMessage = time.Format("%Y%m%d %H:%M:%S ");
strMessage = " Start time:" + strMessage;
WriteMsgIntoFile(m_strResultFiles,strMessage);
CString strTemp;
CString strDownloadUrl;
CString strSavePath;
strSavePath = m_strSavePath + "UrlDowload\\";
int nIndex;
char *pszBuf = NULL;
DWORD dwTotalTime,dwTotalLength;
dwTotalTime = GetTickCount();
dwTotalLength = 0;
UINT nProtocolFlag = 0;
for (int i = 0;i<m_URLS.nUrlNum;i++) {
DWORD dwTick = GetTickCount();
strMessage.Format(" The %dth URL:%s",i+1,m_URLS.ppszUrls[i]);
WriteMsgIntoFile(m_strResultFiles,strMessage);
strDownloadUrl = m_URLS.ppszUrls[i];
strDownloadUrl.MakeLower();
nIndex = strDownloadUrl.ReverseFind('/');
pszBuf = strDownloadUrl.GetBuffer(strDownloadUrl.GetLength())+nIndex+1;
strTemp.Format("%s%s",strSavePath,pszBuf);
if (strncmp(strDownloadUrl,"http",4) == 0)
{
nProtocolFlag = 1;
}
if (strncmp(strDownloadUrl,"ftp",3) == 0)
{
nProtocolFlag = 2;
}
if (!nProtocolFlag) {
strMessage.Format(" URL worng!");
WriteMsgIntoFile(m_strResultFiles,strMessage);
continue;
}
DWORD dwByteToRead = 0;
DWORD dwSizeOfRq = 4;
if ((1 == nProtocolFlag)&&m_bQuerySize) {
CHAR szHead[] = "Accept: */*\r\n\r\n";
HINTERNET hConnect;
if ( !(hConnect = InternetOpenUrl( m_hSession, strDownloadUrl, szHead,
lstrlenA (szHead), INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD |INTERNET_FLAG_EXISTING_CONNECT, 0)))
{
DWORD dwError = GetLastError();
CString strTemp;
strTemp.Format("InternetOpenUrl Error!ErrorCode:%d",dwError);
AfxMessageBox(strTemp);
return;
}
if (!HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
(LPVOID)&dwByteToRead, &dwSizeOfRq, NULL))
{
dwByteToRead = 0;
}
else
{
dwTotalLength += dwByteToRead;
}
InternetCloseHandle(hConnect);
}
else if (m_bQuerySize)
{
char *pszLoctionServer;
char *pszLoctionFilePath;
int nStartPos = strDownloadUrl.Find("//");
CString strTemp2;
if (nStartPos != -1)
{
pszLoctionServer = strDownloadUrl.GetBuffer(2)+nStartPos+2;
pszLoctionFilePath = StrChr(pszLoctionServer,'/');
if (pszLoctionFilePath)
{
*pszLoctionFilePath = '\0';
pszLoctionFilePath++;
}
else
{
strTemp2.Format("The %dth URL is Wrong, please check it!",i+1);
AfxMessageBox(strTemp2);
}
}
else
{
strTemp2.Format("The %dth URL is Wrong, please check it!",i+1);
AfxMessageBox(strTemp2);
}
HINTERNET hFTPSession = ::InternetConnect(
m_hSession, // Handle from a previous
pszLoctionServer, // Server we wish to connect to.
INTERNET_DEFAULT_FTP_PORT, // Use appropriate port.
NULL, // Username, can be NULL.
NULL, // Password, can be NULL.
INTERNET_SERVICE_FTP, // Flag to use FTP services.
0, // Flags (see SDK docs).
(DWORD)0); // SEE DISCUSSION ON THIS PARAM.
if (!hFTPSession) {
DWORD dwError = GetLastError();
strTemp2.Format("ConnectError!ErrorCode:%d",dwError);
AfxMessageBox(strTemp);
return;
}
WIN32_FIND_DATA sWFD;
HINTERNET hFileConnection = ::FtpFindFirstFile(
hFTPSession,
pszLoctionFilePath,
&sWFD,
0,
0);
if (hFileConnection)
{
dwByteToRead = sWFD.nFileSizeHigh * MAXDWORD + sWFD.nFileSizeLow;
}
dwTotalLength += dwByteToRead;
InternetCloseHandle(hFTPSession);
}
URLDownloadToFile(0,strDownloadUrl,strTemp,0,0);
dwTick = GetTickCount() - dwTick;
strMessage.Format(" File Length : %d\tCosting time : %d\tSpeed\t:%.3f",dwByteToRead,
dwTick,(float(dwByteToRead))/dwTick);
WriteMsgIntoFile(m_strResultFiles,strMessage);
}
dwTotalTime = GetTickCount() -dwTotalTime;
strMessage.Format(" Total length: %d\tTotal time:%d\tAverage Speed:%.3f",dwTotalLength,
dwTotalTime,(float(dwTotalLength))/dwTotalTime);
WriteMsgIntoFile(m_strResultFiles,strMessage);
WriteMsgIntoFile(m_strResultFiles,"----------------UrlDownload End------------------\n");
}
void CHttpAndFtpTestDlg::httpDownload()
{
CString strMessage;
WriteMsgIntoFile(m_strResultFiles,"----------------HttpDownload Strat---------------");
CTime time = CTime::GetCurrentTime();
strMessage = time.Format("%Y%m%d %H:%M:%S ");
strMessage = " Start time:" + strMessage;
WriteMsgIntoFile(m_strResultFiles,strMessage);
CString strTemp;
CString strDownloadUrl;
CString strSavePath;
strSavePath = m_strSavePath + "HttpDowload\\";
int nIndex;
char *pszBuf = NULL;
DWORD dwTotalTime,dwTotalLength;
dwTotalTime = GetTickCount();
dwTotalLength = 0;
for (int i = 0;i<m_URLS.nUrlNum;i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -