Click here to Skip to main content
16,010,351 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: multi-select in CTreeCtrl Pin
Xiao-heihei18-Jul-02 22:47
sussXiao-heihei18-Jul-02 22:47 
GeneralRe: multi-select in CTreeCtrl Pin
Xiao-heihei18-Jul-02 22:50
sussXiao-heihei18-Jul-02 22:50 
GeneralRe: multi-select in CTreeCtrl Pin
Xiao-heihei18-Jul-02 22:50
sussXiao-heihei18-Jul-02 22:50 
GeneralRe: multi-select in CTreeCtrl Pin
Tibor Blazko18-Jul-02 23:02
Tibor Blazko18-Jul-02 23:02 
GeneralDownloading file Pin
Anonymous18-Jul-02 19:54
Anonymous18-Jul-02 19:54 
GeneralRe: Downloading file Pin
Roman Nurik18-Jul-02 20:33
Roman Nurik18-Jul-02 20:33 
GeneralRe: Downloading file Pin
Anonymous18-Jul-02 21:13
Anonymous18-Jul-02 21:13 
GeneralRe: Downloading file Pin
Scott H. Settlemier19-Jul-02 7:23
Scott H. Settlemier19-Jul-02 7:23 
I like the MFC WININET classes.
Here's how I generally write a simple download routine:

#include <afxinet.h>
#include <memory>
#include <stdexcept>

using namespace std;

typedef auto_ptr<CHttpConnection> apCHttpConnection;
typedef auto_ptr<CHttpFile> apCHttpFile;
typedef auto_ptr<CFtpConnection> apCFtpConnection;
typedef auto_ptr<CInternetFile> apCInternetFile;

void Download(CFile& Out,CString Agent,CString URL,CString Usr,CString Pwd,const char* PostData,HANDLE hTermRequestEvent)
{
CInternetSession Session(Agent,0);
int nTries=MAXNUMTRIES_HTTP_DOWNLOAD;
CString ErrorMsg;
_RetryDL:
try
{
CString Server,Object;
INTERNET_PORT Port;
DWORD ServiceType;
apCHttpConnection apConnection(0);
try
{
VERIFY(::AfxParseURL(URL,ServiceType,Server,Object,Port) && ServiceType==AFX_INET_SERVICE_HTTP);
apConnection=apCHttpConnection(Session.GetHttpConnection(Server,Port));
}
catch(CInternetException* pe)
{
DWORD err=pe->m_dwError;
pe->Delete();
if (err!=ERROR_SUCCESS)
ErrorMsg.ReleaseBuffer(::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,err,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),ErrorMsg.GetBuffer(1024),1024,NULL)?-1Blush | :O );
apConnection=apCHttpConnection(0);
}
if (!apConnection.get())
throw runtime_error(LPCSTR("Can't connect online: "+(ErrorMsg.IsEmpty()?"check your connection.":ErrorMsg)));
apCHttpFile apFile(apConnection->OpenRequest(PostData?CHttpConnection::HTTP_VERB_POST:CHttpConnection::HTTP_VERB_GET,Object,0,1,0,0,INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE));
if (!apFile.get())
throw runtime_error("Unable to create HTTP request. (is memory low?)");
CString Headers="";
if (PostData)
Headers="Content-Type: application/x-www-form-urlencoded";
_ReSendRequest:
if (!Usr.IsEmpty())
VERIFY(apFile->SetOption(INTERNET_OPTION_USERNAME,(void*)(LPCSTR)Usr,Usr.GetLength()));
if (!Pwd.IsEmpty())
VERIFY(apFile->SetOption(INTERNET_OPTION_PASSWORD,(void*)(LPCSTR)Pwd,Pwd.GetLength()));
if (hTermRequestEvent && WaitForSingleObject(hTermRequestEvent,0)==WAIT_OBJECT_0)
return;
if (!apFile->SendRequest(Headers,Headers.GetLength(),(LPVOID)PostData,PostData?strlen(PostData)Blush | :O ))
throw runtime_error("Unable to send HTTP request. (is connection ok?)");
DWORD Status=0;
if (!apFile->QueryInfoStatusCode(Status))
{
DWORD err=::GetLastError();
if (err!=ERROR_SUCCESS)
ErrorMsg.ReleaseBuffer(::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,err,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),ErrorMsg.GetBuffer(1024),1024,NULL)?-1Blush | :O );
throw runtime_error(ErrorMsg.IsEmpty()?"Can't query status.":LPCSTR(ErrorMsg));
}
switch (Status)
{
case HTTP_STATUS_PROXY_AUTH_REQ:
if (apFile->ErrorDlg(0,ERROR_INTERNET_INCORRECT_PASSWORD,FLAGS_ERROR_UI_FILTER_FOR_ERRORS|FLAGS_ERROR_UI_FLAGS_GENERATE_DATA|FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS)!=ERROR_INTERNET_FORCE_RETRY)
throw runtime_error("Can't connect through proxy server.");
goto _ReSendRequest;
case HTTP_STATUS_OK:
break;
case HTTP_STATUS_NOT_MODIFIED:
case HTTP_STATUS_DENIED:
default:
{
CString S;
S.Format("HTTP STATUS CODE=%u",Status);
throw runtime_error(LPCSTR(S));
}
}
DWORD ContentLength=0,D=sizeof(DWORD);
apFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER,&ContentLength,&D);
BYTE Buf[1024];
unsigned Bytes;
Out.SeekToBegin();
while (Bytes=apFile->Read(Buf,1024))
{
if (hTermRequestEvent && WaitForSingleObject(hTermRequestEvent,0)==WAIT_OBJECT_0)
return;
Out.Write(Buf,Bytes);
// Update Progress Bar or what have you
}
}
catch(CException* e)
{
if (--nTries>0)
{
e->Delete();
goto _RetryDL;
}
throw;
}
catch(...)
{
if (--nTries>0)
goto _RetryDL;
throw;
}
}


GeneralRe: Downloading file Pin
Michael Dunn19-Jul-02 16:54
sitebuilderMichael Dunn19-Jul-02 16:54 
GeneralSHFileOperation failed, why. Pin
ttzzgg_8071318-Jul-02 19:47
ttzzgg_8071318-Jul-02 19:47 
GeneralRe: SHFileOperation failed, why. Pin
Mike Upton18-Jul-02 22:40
Mike Upton18-Jul-02 22:40 
GeneralRe: SHFileOperation failed, why. Pin
ttzzgg_8071319-Jul-02 1:51
ttzzgg_8071319-Jul-02 1:51 
Generalwave file editing please help me Pin
anju18-Jul-02 18:10
anju18-Jul-02 18:10 
GeneralEvents in edit control Pin
harish kota18-Jul-02 17:52
harish kota18-Jul-02 17:52 
GeneralRe: Events in edit control Pin
includeh1018-Jul-02 23:14
includeh1018-Jul-02 23:14 
GeneralEvents in edit control Pin
KVHK18-Jul-02 17:52
KVHK18-Jul-02 17:52 
GeneralRe: Events in edit control Pin
Chris Losinger18-Jul-02 18:02
professionalChris Losinger18-Jul-02 18:02 
GeneralRe: Events in edit control Pin
harish kota18-Jul-02 18:41
harish kota18-Jul-02 18:41 
QuestionCan i use CSocket in thread? Pin
gandi18-Jul-02 15:29
gandi18-Jul-02 15:29 
AnswerRe: Can i use CSocket in thread? Pin
Nish Nishant18-Jul-02 15:42
sitebuilderNish Nishant18-Jul-02 15:42 
GeneralRe: Can i use CSocket in thread? Pin
gandi18-Jul-02 15:57
gandi18-Jul-02 15:57 
AnswerRe: Can i use CSocket in thread? Pin
Anonymous19-Jul-02 3:01
Anonymous19-Jul-02 3:01 
AnswerRe: Technically possible, but... Pin
Masaaki Onishi19-Jul-02 9:42
Masaaki Onishi19-Jul-02 9:42 
QuestionWhich user interface? Pin
Steve L.18-Jul-02 14:58
Steve L.18-Jul-02 14:58 
AnswerRe: Which user interface? Pin
Gary Kirkham18-Jul-02 15:29
Gary Kirkham18-Jul-02 15:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.