导航

<2005年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

随笔分类

随笔档案

文章档案

相册

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;
}
posted on 2005-01-14 12:01 杨老师的茅屋 阅读(1313) 评论(1)  编辑 收藏
评论
  • # re: 如何检测HTTP上的资源是否存在
    Arch_Seraphim
    Posted @ 2005-04-17 22:49
    好文,正是我需要的!谢谢了
标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]

统计