bool m_signed =false; // peoplewant25jan@yahoo.com if you need help , welcome
static DWORD WINAPI DownThread(LPVOID pParameter)//CHTTPDownlogTestDlg
{
CDownloadZipDlg* pThis = (CDownloadZipDlg *)pParameter;
HINTERNET session = NULL;
HINTERNET connect = NULL;
HINTERNET http = NULL;
TCHAR url[MAX_PATH] = {0};
TCHAR server[MAX_PATH] = {0};
TCHAR file[MAX_PATH] = {0};
TCHAR name[MAX_PATH] = _T("C:\\MyDownLog\\");
BYTE lpReadeBuffer[1024 * 8] = {0};
TCHAR header[8] = {0};
TCHAR lpLength[20] = _T("Îļþ´óС:");
DWORD dwTimeout = 15000;
DWORD dwReq = 0;
DWORD dwBufferLength = 500;
DWORD dwIndex = 0;
CString str;
TCHAR *p;
pThis->GetDlgItemText(IDC_EDIT1, url, MAX_PATH);
if(_tcslen(url) < 7)
goto theEnd;
_tcsncpy(header, url, 7);
if(_tcsicmp(header, _T("http://")) != 0)
goto theEnd;
_tcscpy(server, url + 7);
p = _tcschr(server, '/');
if(NULL != p)
{
*p = '\0';
_tcscat(name, _tcsrchr(url, '/') + 1);
}
_tcscpy(file, url + _tcslen(server) + 8);
session = InternetOpen(_T("Mozilla/4.0 (compatible)"), INTERNET_OPEN_TYPE_DIRECT , NULL, NULL, 0);
if(NULL == session)
{
AfxMessageBox(_T("´´½¨»á»°Ê§°Ü"));
goto theEnd;
}
connect = InternetConnect(session, server, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if(NULL == connect)
{
AfxMessageBox(_T("½¨Á¢Á¬½Óʧ°Ü"));
goto theSession;
}
// CHAR* pAcceptTypes = "*/*";
http = HttpOpenRequest(connect, "GET", file, _T("HTTP/1.1"), NULL,/* (LPCSTR *)&pAcceptTypes*/NULL, INTERNET_FLAG_RELOAD, 0);
if(NULL == http)
{
AfxMessageBox(_T("ÇëÇó»á»°Ê§°Ü"));
goto theConnect;
}
if( HttpSendRequest(http, NULL, 0, NULL, 0) )
{
//dwBufferLength = 4;//// InternetGetLastResponseInfo
http://www.dreamincode.net/forums/topic/101532-download-file-from-url/
if( !HttpQueryInfo(http, HTTP_QUERY_CONTENT_LENGTH,// | HTTP_QUERY_FLAG_NUMBER,
&lpReadeBuffer, &dwBufferLength, NULL)) //dwBufferLengthÒ»¶¨ÒªÓгõʼ´óС
{
AfxMessageBox(_T("»ñÈ¡Îļþ´óСʧ°Ü"));
goto theHttp;
}
pThis->m_progress.SetRange(0, 100);
DWORD allLength = atol((char*)lpReadeBuffer);
memset(header, 0, sizeof(TCHAR) * 8);
_itot(allLength, header, 10);
_tcscat(lpLength, header);
_tcscat(lpLength, "Bit");
pThis->SetDlgItemText(IDC_STATIC_L, lpLength);
FILE *ffile = fopen(name, "wb+");
if(NULL == ffile)
{
AfxMessageBox(_T("´´½¨Îļþʧ°Ü"));
goto theHttp;
}
do{
InternetReadFile(http, lpReadeBuffer, 1024 * 8, &dwBufferLength);
dwReq += dwBufferLength;
if(dwBufferLength > 0)
fwrite(lpReadeBuffer, 1, dwBufferLength, ffile);
int i = (int)((float)dwReq / (float)allLength * (float)100);
pThis->m_progress.SetPos(i);
//Sleep(100);
}while(dwBufferLength > 0);
fclose(ffile);
}
str.Format(_T("ÏÂÔØÍê³É:%s"), name);
AfxMessageBox(str);
theHttp:
InternetCloseHandle(http);
theConnect:
InternetCloseHandle(connect);
theSession:
InternetCloseHandle(session);
theEnd:
pThis->m_signed = false;
return 0;
}
void CSimpleftpclientDlg::OnButton1()
{
if(false == m_signed)
{
m_signed = true;
HANDLE handle = CreateThread(NULL, 0, DownThread, (LPVOID)this, 0, NULL);
CloseHandle(handle);
}
else
{
AfxMessageBox(_T("ÏÂÔؽø³ÌÕýÔÚÔËÐÐ"));
}
}