MFC captures the source code of the network to a local file
Int http_get_data (cstring strurl, cstring fileaddr)
{
Cinternetsession session;
Chttpconnection * phttpconnection = NULL;
Chttpfile * phttpfile = NULL;
Cstring strserver, strobject;
Internet_port wport;
DWORD dwtype;
If (! Afxparseurl (strurl, dwtype, strserver, strobject, wport ))
{
Return 1; // URL parsing error
}
Phttpconnection = session. gethttpconnection (strserver, wport );
Phttpfile = phttpconnection-> openrequest (chttpconnection: http_verb_get, strobject );
Phttpfile-> sendrequest ();
DWORD dwret;
Phttpfile-> queryinfostatuscode (dwret );
If (dwret = http_status_ OK)
{
Cfile file;
File. Open (fileaddr, cfile: modecreate | cfile: modewrite | cfile: sharedenynone, 0 );
Char * pszbuffer = new char [1024];
Uint nread = phttpfile-> Read (pszbuffer, 1024 );
While (nread> 0)
{
File. Write (pszbuffer, nread );
Nread = phttpfile-> Read (pszbuffer, 1024 );
}
File. Close ();
Delete [] pszbuffer;
}
If (phttpfile! = NULL)
{
Phttpfile-> close ();
Delete phttpfile;
Phttpfile = 0;
}
If (phttpconnection! = NULL)
{
Phttpconnection-> close ();
Delete phttpconnection;
Phttpconnection = 0;
}
Session. Close ();
Return 0;
}