Click here to Skip to main content
15,897,371 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: AfxParseURL Pin
Cedric Moonen16-May-06 20:55
Cedric Moonen16-May-06 20:55 
GeneralRe: AfxParseURL Pin
shuchigo_jane16-May-06 21:05
shuchigo_jane16-May-06 21:05 
GeneralRe: AfxParseURL Pin
Cedric Moonen16-May-06 21:09
Cedric Moonen16-May-06 21:09 
GeneralRe: AfxParseURL Pin
ThatsAlok16-May-06 21:12
ThatsAlok16-May-06 21:12 
GeneralRe: AfxParseURL Pin
shuchigo_jane16-May-06 21:16
shuchigo_jane16-May-06 21:16 
GeneralRe: AfxParseURL Pin
Cedric Moonen16-May-06 21:20
Cedric Moonen16-May-06 21:20 
GeneralRe: AfxParseURL Pin
Mila02516-May-06 21:32
Mila02516-May-06 21:32 
GeneralRe: AfxParseURL Pin
alan top17-May-06 1:43
alan top17-May-06 1:43 
{ ShowBanner();

if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
cerr << _T("MFC Failed to initialize.\n");
return 1;
}

if (argc < 2 || !ParseOptions(argc, argv) || pszURL == NULL)
ShowUsage();

int nRetCode = 0;

CTearSession session(_T("TEAR - MFC Sample App"), dwAccessType);
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
try
{
// check to see if this is a reasonable URL

CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;

if (!AfxParseURL(pszURL, dwServiceType, strServerName, strObject, nPort) ||
dwServiceType != INTERNET_SERVICE_HTTP)
{
cerr << _T("Error: can only use URLs beginning with http://") << endl;
ThrowTearException(1);
}

if (bProgressMode)
{
cerr << _T("Opening Internet...");
VERIFY(session.EnableStatusCallback(TRUE));
}

pServer = session.GetHttpConnection(strServerName, nPort);

pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->SendRequest();

DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);

// if access was denied, prompt the user for the password

if (dwRet == HTTP_STATUS_DENIED)
{
DWORD dwPrompt;
dwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD,
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL);

// if the user cancelled the dialog, bail out

if (dwPrompt != ERROR_INTERNET_FORCE_RETRY)
{
cerr << _T("Access denied: Invalid password\n");
ThrowTearException(1);
}

pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
}

CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);

// were we redirected?
// these response status codes come from WININET.H

if (dwRet == HTTP_STATUS_MOVED ||
dwRet == HTTP_STATUS_REDIRECT ||
dwRet == HTTP_STATUS_REDIRECT_METHOD)
{
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);

int nPlace = strNewLocation.Find(_T("Location: "));
if (nPlace == -1)
{
cerr << _T("Error: Site redirects with no new location") << endl;
ThrowTearException(2);
}

strNewLocation = strNewLocation.Mid(nPlace + 10);
nPlace = strNewLocation.Find('\n');
if (nPlace > 0)
strNewLocation = strNewLocation.Left(nPlace);

// close up the redirected site

pFile->Close();
delete pFile;
pServer->Close();
delete pServer;

if (bProgressMode)
{
cerr << _T("Caution: redirected to ");
cerr << (LPCTSTR) strNewLocation << endl;
}

// figure out what the old place was
if (!AfxParseURL(strNewLocation, dwServiceType, strServerName, strObject, nPort))
{
cerr << _T("Error: the redirected URL could not be parsed.") << endl;
ThrowTearException(2);
}

if (dwServiceType != INTERNET_SERVICE_HTTP)
{
cerr << _T("Error: the redirected URL does not reference a HTTP resource.") << endl;
ThrowTearException(2);
}

// try again at the new location
pServer = session.GetHttpConnection(strServerName, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->SendRequest();

pFile->QueryInfoStatusCode(dwRet);
if (dwRet != HTTP_STATUS_OK)
{
cerr << _T("Error: Got status code ") << dwRet << endl;
ThrowTearException(2);
}
}

cerr << _T("Status Code is ") << dwRet << endl;

TCHAR sz[1024];
while (pFile->ReadString(sz, 1023))
{
if (bStripMode)
StripTags(sz);
cout << sz;
}

// NOTE: Since HTTP servers normally spit back plain text, the
// above code (which reads line by line) is just fine. However,
// other data sources (eg, FTP servers) might provide binary data
// which should be handled a buffer at a time, like this:

#if 0
while (nRead > 0)
{
sz[nRead] = '\0';
if (bStripMode)
StripTags(sz);
cout << sz;
nRead = pFile->Read(sz, 1023);
}
#endif

pFile->Close();
pServer->Close();
}
catch (CInternetException* pEx)
{
// catch errors from WinINet

TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);

cerr << _T("Error: (") << pEx->m_dwError << _T(") ");
cerr << szErr << endl;

nRetCode = 2;
pEx->Delete();
}
catch (CTearException* pEx)
{
// catch things wrong with parameters, etc

nRetCode = pEx->m_nErrorCode;
TRACE1("Error: Exiting with CTearException(%d)\n", nRetCode);
pEx->Delete();
}

if (pFile != NULL)
delete pFile;
if (pServer != NULL)
delete pServer;
session.Close();

return nRetCode;
}

alantop
GeneralRe: AfxParseURL Pin
David Crow17-May-06 3:28
David Crow17-May-06 3:28 
QuestionBasic Code Review CheckList in C++ Pin
itkid16-May-06 20:11
itkid16-May-06 20:11 
GeneralRe: Basic Code Review CheckList in C++ Pin
Maxwell Chen16-May-06 20:27
Maxwell Chen16-May-06 20:27 
AnswerRe: Basic Code Review CheckList in C++ Pin
ThatsAlok16-May-06 21:13
ThatsAlok16-May-06 21:13 
GeneralRe: Basic Code Review CheckList in C++ Pin
itkid16-May-06 23:15
itkid16-May-06 23:15 
AnswerRe: Basic Code Review CheckList in C++ Pin
markkuk17-May-06 0:31
markkuk17-May-06 0:31 
QuestionReading USB ports Pin
satsumatable16-May-06 19:56
satsumatable16-May-06 19:56 
AnswerRe: Reading USB ports Pin
Cedric Moonen16-May-06 20:29
Cedric Moonen16-May-06 20:29 
Questionhelp me Pin
ramyasangeet16-May-06 19:52
ramyasangeet16-May-06 19:52 
GeneralRe: help me Pin
Maxwell Chen16-May-06 20:21
Maxwell Chen16-May-06 20:21 
GeneralRe: help me Pin
ramyasangeet16-May-06 21:19
ramyasangeet16-May-06 21:19 
GeneralRe: help me Pin
Maxwell Chen16-May-06 21:49
Maxwell Chen16-May-06 21:49 
AnswerRe: help me Pin
Hamid_RT17-May-06 1:37
Hamid_RT17-May-06 1:37 
Questionwindows regisrty Pin
iLL eFFect16-May-06 19:33
iLL eFFect16-May-06 19:33 
AnswerRe: windows regisrty Pin
Michael Dunn16-May-06 21:06
sitebuilderMichael Dunn16-May-06 21:06 
QuestionContext menu for Recycle Bin Pin
rajeevktripathi16-May-06 19:31
rajeevktripathi16-May-06 19:31 
QuestionTransperent Icon BackGround Pin
Scorpio16-May-06 19:30
Scorpio16-May-06 19:30 

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.