Learning experience using Wininet classes
The following code excerpts how to create a simple browser, download a web page, operate a file with FTP and find a gopher file. They are not a complete example, which does not contain an abnormality, just a summary of the recent writer.
1 Create a very simple browser #include
2 Download a web page #include
pServer = session.GetHttpConnection (strServerName, nPort); pFile = pServer-> OpenRequest (CHttpConnection :: HTTP_VERB_GET, strObject); pFile-> AddRequestHeaders (szHeaders); pFile-> SendRequest (); pFile-> QueryInfoStatusCode (dwRet);
IF (dwret == http_status_ok) {uint nread = pfile-> read (szbuff, 1023); while (nread> 0) {file: // read file}} DELETE PFILE; DELETE PSERVER;} catch (cinternetException * PEX) {File: // Capture Wininet's Error} session.close ();
3 Operate a file with FTP #include
PCONN = session.GetftpConnect (LPSZSERVERNAME); File: // Gets file IF (! PConn-> getfile (pstrremotefile, pstrlocalfile) File: // Display an error delete PCONN; session.close ();
4 Receive a gopher directory #include
pConn = session.GetGopherConnection ( "gopher: //gopher.yousite.com/"); pFile = new CGopherFileFind (pConn); BOOL bFound = pFile-> FindFile (lpszFileToFind); while (bFound) {bFound = pFile-> FindNextFile (); File: // Re-founded the file attribute} delete pfile; delete pconn; session.close (); When using the Wininet class library, users can use the member function of the application's CinternetSession object to get status information . If the user inherits its own CinternetSession object, written a callback overstatuscallback and allowing the status, the MFC will use the progress information of all the activities of the International Interconnection Network as a parameter, call the user's onstatusCallback function.