Solved

Can't make CHttpConnection work

Posted on 2009-05-05
3
924 Views
Last Modified: 2013-11-20
I'm trying to download web content, and I would like to try using the MFC wrapper. I keep getting "The URL is invalid", when using direct retrieval works fine. There is only bad documentation and no examples of usage. I'm sure I'm abusing the file specification in OpenRequest, but I can't find any permutation that works. Any help would be appreciated.
// GetYahooDataCA.cpp : Defines the entry point for the console application.
 
#include "stdafx.h"
#include <afxinet.h>
 
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
 
CWinApp theApp;
 
using namespace std;
 
// This function works perfectly...
void DisplayPage(LPCTSTR pszURL)
{
	CInternetSession session(_T("Yahoo Session"));
	CStdioFile* pFile = NULL;
	CHAR szBuff[1024];
	pFile = session.OpenURL(pszURL);
	while (pFile->Read(szBuff, 1024) > 0)
		printf_s("%-1023s", szBuff);
	delete pFile;
	session.Close();
}
 
// This one throws an exception on SendRequest();
static void DisplayHttpPage(LPCTSTR pszServerName, LPCTSTR pszFileName)
{
	CInternetSession session(_T("Yahoo Session"));
	try {
		CString strServerName;
		INTERNET_PORT nPort = 80;
		DWORD dwRet = 0;
 
		CHttpConnection*	pServer	= session.GetHttpConnection(pszServerName, nPort);
		CHttpFile*		pFile	= pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, pszFileName, pszServerName );
		pFile->SendRequest();		// throws "URL not found" exception here...
		pFile->QueryInfoStatusCode(dwRet);
 
		if (dwRet == HTTP_STATUS_OK) {
			CHAR szBuff[1024];
			while (pFile->Read(szBuff, 1024) > 0)
				printf_s("%-1023s", szBuff);
		}
		delete pFile;
		delete pServer;
	}
	catch (CInternetException* pEx) {		//catch errors from WinInet
		TCHAR pszError[64];
		pEx->GetErrorMessage(pszError, 64);
		_tprintf_s(_T("\n%-63s\n"), pszError);
	}
	session.Close();
}
 
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {	// initialize MFC and print an error on failure
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	} else {
 
		CString GE_URL ( "http://biz.yahoo.com" );
		CString GE_File( "/research/earncal/today.html" );
 
		DisplayPage( GE_URL+GE_File );
		DisplayHttpPage( GE_URL, GE_File );
	}
	return nRetCode;
}

Open in new window

0
Comment
Question by:qstat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 125 total points
ID: 24313246
Hi qstat,

I played a bit around with your code and got it running.

First it seems for 'GetHttpConnection' the passed servername must not be preceeded by the protocol type, so it has to be 'biz.yahoo.com' instead of 'http://biz.yahoo.com'.

Next for 'OpenRequest' the passed filename must not be preceeded by a '/', so it has to be 'research/earncal/today.html' instead of '/research/earncal/today.html'.

At last if you need to pass a referrer-URL to 'OpenRequest' it has to be preceeded by 'http://'.

So, as a conclusion you need to change following:

1. In 'DisplayHttpPage' nitialize the up to now unused variable 'strServerName' like this:
> CString strServerName = CString( "http://" ) + pszServerName;

2. Change the call to OpenRequest in order to use that server name as referrer:
> CHttpFile*              pFile   = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, pszFileName, strServerName );

3. Change the two strings for server- and file name in _tmain like this:
> CString GE_URL ( "biz.yahoo.com" );
> CString GE_File( "research/earncal/today.html" );

Hope that helps,

ZOPPO
0
 

Author Closing Comment

by:qstat
ID: 31578319
Thanks for the help. It would be nice if MS would give the slightest clue for correct usage.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 24373633
Hi qstat,

yes, I agree - it's often a long and exhausting work to find out how some things work, a combination of lot of searching the web and trial and error ...

Therefor we can be thankful for resources like experts exchange!

Have a nice day,

ZOPPO
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Named range not carried over 10 71
How to determine file rename from EventLog ? 2 62
sumDigits challenge 9 178
Trying to run powershell  in a batch file. How do I do this? 8 124
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question