Learning experience using Wininet classes

zhaozj2021-02-17  38

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 // Assume that the URL name has been initialized CinternetSession session ("my session"); cstdiofile * pfile = null; // uses a URL and displays one Web While (LPSZURL = DisplayPage (...) {pfile = session.openurl (lpszurl); while (pfile-> read (szbuff, 1024)> 0) {// read file ...} delete pfile; session.close ();

2 Download a web page #include // Assuming the server, port number and URL name 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) {// read file}} delete pfile; delete pserver;} catch (cinternetException * pEX) {/ / Capture Wininet's Error} session.close ();

3 Operate a file with FTP #include // Suppose the server name and file name have initialized the CinternetSession Session ("My FTP Session"); cftpConnection * PConn = NULL;

PCONN = session.GetftpConnect (LPSZSERVERNAME); // Get file IF (! pConn-> getfile (pstrremotefile, pstrlocalfile) // Display an error Delete PCONN; session.close ();

4 Receive a gopher directory #include // assume that the file name has been initialized CINTERNETSESSION session ("My Gopher Session"); cgopherConnection * PConn = NULL; cgopherfilefind * Pfile;

pConn = session.GetGopherConnection ( "gopher.yousite.com"); pFile = new CGopherFileFind (pConn); BOOL bFound = pFile-> FindFile (lpszFileToFind); while (bFound) {bFound = pFile-> FindNextFile (); // Re-found file attribute} delete pfile; delete pconn; session.close (); When using the Wininet class library, users can use the member function onstatusCallback 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-30942.html

New Post(0)