MattC
asked on
using CinternetSession
here's what I got:
#include "stdafx.h"
#include <afxinet.h>
#include "http.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// The one and only application object
CWinApp theApp;
using namespace std;
void connectTo(char* site, char* page){
CInternetSession* pNet;
CHttpConnection* pConn;
CHttpFile* pFile;
DWORD dwRet;
UINT nRead;
char* szBuff[4096];
CString source;
pNet = new CInternetSession(NULL, 1, PRE_CONFIG_INTERNET_ACCESS , NULL, NULL, INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD);
try{
pConn = pNet->GetHttpConnection(si te);
cout<<"connection made to: "<<site<<endl;
}
catch(...){
cout<<"connection failed to: "<<site<<endl;
}
pFile = pConn->OpenRequest(NULL, page); // Open an HTTP request.
try{
pFile->SendRequest( ); // Try sending the request.
pFile->QueryInfoStatusCode (dwRet);
cout<<"request made to: "<<page<<endl;
}
catch(CInternetException *e){
cout<<"request failed to "<<page<<" error code:"<<e->m_dwError<<endl ;
}
if(dwRet == HTTP_STATUS_OK){
while (nRead = pFile->Read(szBuff, 4096) > 0){
source.Insert(source.GetLe ngth()+1,( char*)szBu ff);
}
cout<<(LPCTSTR) source<<endl;//i think the bug is here
}
delete pConn;
delete pFile;
pNet->Close();
delete pNet;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHa ndle(NULL) , NULL, ::GetCommandLine(), 0)){
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else{
//connectTo("matts-laptop/ testsite/" ,"default. asp" );//no "http://"
connectTo("www.yahoo.com","index.html" );//no "http://"
}
return nRetCode;
}
when it runs it should print the returned html from the page, which it does....plus a whole heap of extra characters, any ideas????
#include "stdafx.h"
#include <afxinet.h>
#include "http.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
void connectTo(char* site, char* page){
CInternetSession* pNet;
CHttpConnection* pConn;
CHttpFile* pFile;
DWORD dwRet;
UINT nRead;
char* szBuff[4096];
CString source;
pNet = new CInternetSession(NULL, 1, PRE_CONFIG_INTERNET_ACCESS
try{
pConn = pNet->GetHttpConnection(si
cout<<"connection made to: "<<site<<endl;
}
catch(...){
cout<<"connection failed to: "<<site<<endl;
}
pFile = pConn->OpenRequest(NULL, page); // Open an HTTP request.
try{
pFile->SendRequest( ); // Try sending the request.
pFile->QueryInfoStatusCode
cout<<"request made to: "<<page<<endl;
}
catch(CInternetException *e){
cout<<"request failed to "<<page<<" error code:"<<e->m_dwError<<endl
}
if(dwRet == HTTP_STATUS_OK){
while (nRead = pFile->Read(szBuff, 4096) > 0){
source.Insert(source.GetLe
}
cout<<(LPCTSTR) source<<endl;//i think the bug is here
}
delete pConn;
delete pFile;
pNet->Close();
delete pNet;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHa
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else{
//connectTo("matts-laptop/
connectTo("www.yahoo.com","index.html" );//no "http://"
}
return nRetCode;
}
when it runs it should print the returned html from the page, which it does....plus a whole heap of extra characters, any ideas????
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
could you explain what this line means, i just started on this stuff today =(
ZeroMemory ( szBuff, sizeof ( szBuff));
ZeroMemory ( szBuff, sizeof ( szBuff));
As we're just optimizing :o)
if(dwRet == HTTP_STATUS_OK){
while (nRead = pFile->Read(szBuff, 4096) > 0){
szBuff [ nRead + 1] = '/0'; ));// 'Read()' does not zero-terminate the buffer!!!
source += CString ( szBuff);
}
cout<<(LPCTSTR) source<<endl;
}
if(dwRet == HTTP_STATUS_OK){
while (nRead = pFile->Read(szBuff, 4096) > 0){
szBuff [ nRead + 1] = '/0'; ));// 'Read()' does not zero-terminate the buffer!!!
source += CString ( szBuff);
}
cout<<(LPCTSTR) source<<endl;
}
>>could you explain what this line means, i just started
>>on this stuff today =(
>> ZeroMemory ( szBuff, sizeof ( szBuff));
It zeroes out a memory area. A 'C'-style string is characterized by a terminating binary '0', and as the 'Read()' operation does not terminate the buffer with it, the strings you're adding are 'longer' - that's the 'extra characters' you noticed.
>>on this stuff today =(
>> ZeroMemory ( szBuff, sizeof ( szBuff));
It zeroes out a memory area. A 'C'-style string is characterized by a terminating binary '0', and as the 'Read()' operation does not terminate the buffer with it, the strings you're adding are 'longer' - that's the 'extra characters' you noticed.
ASKER
>>>szBuff [ nRead + 1] = '/0'; ));
should be
szBuff [ nRead + 1] = "/0";
=)
ASKER
if(dwRet == HTTP_STATUS_OK){
while (nRead = pFile->Read(szBuff, 4096) > 0){
szBuff [ nRead + 1] = "/0";
source += CString ( szBuff);
}
cout<<(LPCTSTR) source<<endl;
}
compiler complain that:
C:\Documents\Programming\h ttp\http.c pp(63) : error C2440: 'type cast' : cannot convert from 'char *[4096]' to 'class CString'
while (nRead = pFile->Read(szBuff, 4096) > 0){
szBuff [ nRead + 1] = "/0";
source += CString ( szBuff);
}
cout<<(LPCTSTR) source<<endl;
}
compiler complain that:
C:\Documents\Programming\h
>>>>>szBuff [ nRead + 1] = '/0'; ));
>>should be
>>szBuff [ nRead + 1] = "/0";
No, it should read
szBuff [ nRead + 1] = '\0'; // backslash!
'\0' is a character, "\0" is a pointer to a zero-terminated string. What we want to do is inserting a null byte at the end of the stream...
BTW:
char* szBuff[4096];
is wrong, that declares an array of pointers - you probably wanted to use
char szBuff[4096];
As we need to write the terminating null byte, take that into consideration:
char szBuff[4096 + 1];
>>should be
>>szBuff [ nRead + 1] = "/0";
No, it should read
szBuff [ nRead + 1] = '\0'; // backslash!
'\0' is a character, "\0" is a pointer to a zero-terminated string. What we want to do is inserting a null byte at the end of the stream...
BTW:
char* szBuff[4096];
is wrong, that declares an array of pointers - you probably wanted to use
char szBuff[4096];
As we need to write the terminating null byte, take that into consideration:
char szBuff[4096 + 1];
>>C:\Documents\Programming \http\http .cpp(63) : error
>>C2440: 'type cast' : cannot convert from 'char *[4096]'
>>to 'class CString'
That's related to my last comment - it should read
char szBuff[4096 + 1];
>>C2440: 'type cast' : cannot convert from 'char *[4096]'
>>to 'class CString'
That's related to my last comment - it should read
char szBuff[4096 + 1];
ASKER
but it doesn't print any of the html now. it did with the Zeromemory thing, but I do want to learn memory efficient coding
also it only works if I request an .asp page. as soon as i try and get an html page it dwRet give me: 405
HTTP_STATUS_BAD_METHOD 405 The method used is not allowed.
what the heck does that mean
also it only works if I request an .asp page. as soon as i try and get an html page it dwRet give me: 405
HTTP_STATUS_BAD_METHOD 405 The method used is not allowed.
what the heck does that mean
You might want to check the sample at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample98/html/_sample_mfc_tear.asp ('TEAR: "Tearing" HTML Pages Off the Internet'):
"The TEAR sample shows how to write an MFC console application that uses WININET.DLL to communicate with the Internet. The sample shows how to form an HTTP request using CHttpFile against CHttpConnection and CInternetSession objects."
This sample should also be available on your VC++ CD.
"The TEAR sample shows how to write an MFC console application that uses WININET.DLL to communicate with the Internet. The sample shows how to form an HTTP request using CHttpFile against CHttpConnection and CInternetSession objects."
This sample should also be available on your VC++ CD.
ASKER
unfortunately I'm not at home.
I'm thinking it's either how I'm setting up:
pNet = new CInternetSession(NULL, 1, PRE_CONFIG_INTERNET_ACCESS , NULL, NULL, INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD);
or:
pNet->GetHttpConnection(si te);
??
I'm thinking it's either how I'm setting up:
pNet = new CInternetSession(NULL, 1, PRE_CONFIG_INTERNET_ACCESS
or:
pNet->GetHttpConnection(si
??
ASKER
s'ok it was
pFile = pConn->OpenRequest("GET", page); // Open an HTTP request.
instead of
pFile = pConn->OpenRequest(NULL, page); // Open an HTTP request.
pFile = pConn->OpenRequest("GET", page); // Open an HTTP request.
instead of
pFile = pConn->OpenRequest(NULL, page); // Open an HTTP request.
ASKER
so currently it will only work like:
if(dwRet == HTTP_STATUS_OK){
ZeroMemory ( szBuff, sizeof ( szBuff));
while (nRead = pFile->Read(szBuff, 4096) > 0){
//szBuff [ nRead + 1] = '\0';
//source += CString ( szBuff);
source.Insert(source.GetLe ngth(),(ch ar*)szBuff );
ZeroMemory ( szBuff, sizeof ( szBuff));// 'Read()' does not zero-terminate the buffer!!!
}
cout<<"HTML RECEIVED: "<<(LPCTSTR) source<<endl;
}else{
cout<<dwRet<<endl;
}
and not
if(dwRet == HTTP_STATUS_OK){
while (nRead = pFile->Read(szBuff, 4096) > 0){
szBuff [ nRead + 1] = '\0';
source += CString ( szBuff);
}
cout<<(LPCTSTR) source<<endl;
}
if(dwRet == HTTP_STATUS_OK){
ZeroMemory ( szBuff, sizeof ( szBuff));
while (nRead = pFile->Read(szBuff, 4096) > 0){
//szBuff [ nRead + 1] = '\0';
//source += CString ( szBuff);
source.Insert(source.GetLe
ZeroMemory ( szBuff, sizeof ( szBuff));// 'Read()' does not zero-terminate the buffer!!!
}
cout<<"HTML RECEIVED: "<<(LPCTSTR) source<<endl;
}else{
cout<<dwRet<<endl;
}
and not
if(dwRet == HTTP_STATUS_OK){
while (nRead = pFile->Read(szBuff, 4096) > 0){
szBuff [ nRead + 1] = '\0';
source += CString ( szBuff);
}
cout<<(LPCTSTR) source<<endl;
}
this is from an active x i wrote, it may help
void CActiveXChatCtrl::OnButton ()
{
/* the session must be created with a name or it asserts,
the documentation says otherwise!, typical */
CInternetSession session("session");
/* we can use the base class as we dont need the extra
functionality that CHttpFile offers (whats being returned in
our case here is a CHttpFile) but we will use the base class */
CStdioFile *file = session.OpenURL(m_url);
/* read in 100 bytes, this will call CHttpFile's Read member */
char buffer[100];
int l = file->Read(buffer,sizeof(b uffer));
/* tak on a NULL */
buffer[l] = '\0';
/* set the window text of the edit control */
m_pEdit->SetWindowText(buf fer);
/* delete the file object */
delete file;
/* the session will drop out of scope now and close */
}
void CActiveXChatCtrl::OnButton
{
/* the session must be created with a name or it asserts,
the documentation says otherwise!, typical */
CInternetSession session("session");
/* we can use the base class as we dont need the extra
functionality that CHttpFile offers (whats being returned in
our case here is a CHttpFile) but we will use the base class */
CStdioFile *file = session.OpenURL(m_url);
/* read in 100 bytes, this will call CHttpFile's Read member */
char buffer[100];
int l = file->Read(buffer,sizeof(b
/* tak on a NULL */
buffer[l] = '\0';
/* set the window text of the edit control */
m_pEdit->SetWindowText(buf
/* delete the file object */
delete file;
/* the session will drop out of scope now and close */
}
m_url, is the url, its an object member, populated already
ASKER
well I wanted to use CString becuase it has lots of useful methods
m_url is a CString
ASKER
will a CStdioFile object let me query the pages,header info as well as the pages source?
no,
CHttpFile *file = session.OpenURL(m_url);
should work
CHttpFile *file = session.OpenURL(m_url);
should work
ASKER
what is the advantage of:
pNet = new CInternetSession(NULL, 1, PRE_CONFIG_INTERNET_ACCESS , NULL, NULL, INTERNET_FLAG_DONT_CACHE
| INTERNET_FLAG_RELOAD);
pConn = pNet->GetHttpConnection(si te);
pFile = pConn->OpenRequest(NULL, page);
pFile->SendRequest( );
Over
CHttpFile *file = session.OpenURL(m_url);
pNet = new CInternetSession(NULL, 1, PRE_CONFIG_INTERNET_ACCESS
| INTERNET_FLAG_RELOAD);
pConn = pNet->GetHttpConnection(si
pFile = pConn->OpenRequest(NULL, page);
pFile->SendRequest( );
Over
CHttpFile *file = session.OpenURL(m_url);
its alot easier to code but less powerfull, if horses for courses, use what suits
ASKER
in what way less powerful, is there a bigger performance hit or just less flexible
well you dont have the flexability
if(dwRet == HTTP_STATUS_OK){
while (nRead = pFile->Read(szBuff, 4096) > 0){
szBuff [ nRead + 1] = '/0'; ));// 'Read()' does not zero-terminate the buffer!!!
source.Insert(source.GetLe
}
cout<<(LPCTSTR) source<<endl;
}