首页 > 编程资源分享区 > C/C++源代码共享 > 检测HTTP上的资源是否存在
2006
07-08

检测HTTP上的资源是否存在

BOOL ExistHttpFile(LPCTSTR lpHttpFile)
{
CInternetSession session(“Client”,PRE_CONFIG_INTERNET_ACCESS);
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString strServerName(“”),strObject(“”);
INTERNET_PORT nPort;
DWORD dwServiceType;
BOOL bRight=FALSE;
try
{
  if (!AfxParseURL(lpHttpFile, dwServiceType, strServerName, strObject, nPort) ||
             dwServiceType != INTERNET_SERVICE_HTTP)    throw(1);
  pServer = session.GetHttpConnection(strServerName, nPort);
  pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
           strObject, NULL, 1, NULL, NULL,
           INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT);
  pFile->AddRequestHeaders(“Client”);
  pFile->SendRequest();
  DWORD dwRet;
  pFile->QueryInfoStatusCode(dwRet);
  if (dwRet == HTTP_STATUS_DENIED) throw(1);
  CString strNewLocation(“”);
  pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
  // 是否重定向
  if (dwRet == HTTP_STATUS_MOVED ||
   dwRet == HTTP_STATUS_REDIRECT ||
   dwRet == HTTP_STATUS_REDIRECT_METHOD)
         throw(1);
  if(strNewLocation.Find(“200 OK”) != -1)             bRight=TRUE;
  else if(strNewLocation.Find(“Content-Type:”) !=-1 ) bRight=TRUE;
  pFile->Close();
  pServer->Close();
}
catch(…)
{
    bRight=FALSE;
}
if(pFile)    delete pFile;
if(pServer)  delete pServer;
session.Close();

return bRight;
}


留下一个回复