Learning experience using Wininet classes

zhaozj2021-02-16  55

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 file: // assumes that the name of the URL has been initialized the CinternetSession session ("My session"); cstdiofile * pfile = null; file: // uses a URL, And display a web page (LPSZURL = DisplayPage (...) {pfile = session.openurl (lpszurl); while (pfile-> read (szbuff, 1024)> 0) {file: // read file .. DELETE PFILE;} session.close ();

2 Download a web page #include file: // assumes the server, port number and URL names have initialized CinternetSession session ("my session"); ChttpConnection * psever = null; chttpfile * pfile = null; try { CString strservername; Internet_port nport;

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 file: // assume that the server name and file name have initialized the CinternetSession Session ("My FTP Session"); cftpConnection * PConn = NULL;

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 file: // assume that the file name has been initialized the CinternetSession Session ("My Gopher Session"); cgopherConnection * PConn = NULL; cgopherfilefind * Pfile;

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.

转载请注明原文地址:https://www.9cbs.com/read-20144.html

New Post(0)