inet.cpp
上传用户:zexelpump
上传日期:2007-01-04
资源大小:22k
文件大小:3k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

Visual C++

  1. /*
  2.     Simple wrapper class for checking validity of a URL
  3. */
  4. #include <windows.h>
  5. #include <io.h>
  6. #include "inet.h"
  7. WinInet::WinInet()
  8. {
  9.     m_hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  10. }
  11. WinInet::~WinInet()
  12. {
  13.     if ( m_hInternet != NULL )
  14.     {
  15.         InternetCloseHandle(m_hInternet);
  16.         m_hInternet = NULL;
  17.     }
  18. }
  19. BOOL WinInet::CheckLink(LPCSTR pcszURL)
  20. {
  21.     BOOL    bFound = FALSE;
  22.     DWORD   dwErr;
  23.     char    szURLHostName[INTERNET_MAX_HOST_NAME_LENGTH];
  24.     char    szURLPath[INTERNET_MAX_URL_LENGTH];
  25.     if ( IsConnected() )
  26.     {
  27.         URL_COMPONENTS urlInfo = { sizeof(URL_COMPONENTS) };
  28.         urlInfo.lpszHostName = szURLHostName;
  29.         urlInfo.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
  30.         urlInfo.lpszUrlPath = szURLPath;
  31.         urlInfo.dwUrlPathLength = INTERNET_MAX_URL_LENGTH;
  32.         //could be changed to return NOT_A_URL, success or failure
  33.         if ( !InternetCrackUrl(pcszURL, 0, 0, &urlInfo) )
  34.             return FALSE;
  35.         if ( urlInfo.nScheme == INTERNET_SCHEME_HTTP )
  36.         {
  37.             HINTERNET hURL = InternetConnect(m_hInternet, szURLHostName, INTERNET_INVALID_PORT_NUMBER,
  38.                                "", "", INTERNET_SERVICE_HTTP, 0, 0);
  39.             if ( hURL )
  40.             {
  41.                 LPSTR szarrAcceptTypes[2] = { "*/*", NULL };
  42.                 DWORD  dwFlags = INTERNET_FLAG_RELOAD           | 
  43.                                  INTERNET_FLAG_NO_CACHE_WRITE   | 
  44.                                  INTERNET_FLAG_KEEP_CONNECTION;
  45.                 HINTERNET hRequest = HttpOpenRequest(hURL, "GET", szURLPath,
  46.                                                      HTTP_VERSION, "", (LPCSTR*)szarrAcceptTypes, dwFlags, 0);
  47.                 if ( hRequest )
  48.                 {
  49.                     BOOL b = HttpSendRequest(hRequest, NULL, 0, NULL,0);
  50.                     if ( b )
  51.                     {
  52.                         DWORD dwStatus = 0;
  53.                         DWORD dwLength = sizeof(DWORD);
  54.                         if ( HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, (PVOID)&dwStatus, &dwLength, NULL) )
  55.                             bFound = (dwStatus < HTTP_STATUS_AMBIGUOUS);
  56.                         else
  57.                             dwErr = GetLastError();
  58.                     }
  59.                     InternetCloseHandle(hRequest);
  60.                 }
  61.                 InternetCloseHandle(hURL);
  62.             }
  63.         }
  64.         else if ( urlInfo.nScheme == INTERNET_SCHEME_FTP )
  65.         {
  66.             HINTERNET hFTP = InternetConnect(m_hInternet, szURLHostName, INTERNET_DEFAULT_FTP_PORT,
  67.                                "", "", INTERNET_SERVICE_FTP, 0, 0);
  68.             if ( hFTP )
  69.             {
  70.                 HINTERNET hFile = FtpOpenFile(hFTP, szURLPath, GENERIC_READ, INTERNET_FLAG_RELOAD | FTP_TRANSFER_TYPE_BINARY, 0);
  71.                 if ( hFile )
  72.                 {
  73.                     bFound = TRUE;
  74.                     InternetCloseHandle(hFile);
  75.                 }
  76.                 InternetCloseHandle(hFTP);
  77.             }
  78.         }
  79.         else if ( urlInfo.nScheme == INTERNET_SCHEME_FILE )
  80.         {
  81.             // check if the file exists
  82.             bFound = (access(szURLPath, 0x00) == 0x00);
  83.         }
  84.         else
  85.         {
  86.             //unrecognized URL scheme
  87.         }
  88.     }
  89.     return bFound;
  90. }