Winnet獲取網頁HTML內容-Code

bool loadHtmlFile(const CString& strUrl, CString& strHtml)
 {
  bool bRet = false;
  CInternetSession sess;//建立會話
  sess.SetOption(INTERNET_OPTION_CONNECT_TIME, 10*1000, NULL);//似乎不起作用,微軟SDK的一個bug

  CHttpFile* fileGet = 0;
  InternetParam param;
  param.pSession = &sess;
  param.pHttpFile = &fileGet;
  param.strUrl = strUrl;
  HANDLE hThread = ::CreateThread(NULL, 0, openUrlProc, (LPVOID)&param, NULL, NULL);//自己開線程處理超時
  DWORD dwTimeOut = 10*1000;
  if (::WaitForSingleObject(hThread, dwTimeOut) == WAIT_TIMEOUT)
  {
   sess.Close();
   return false;
  }
  fileGet = *(param.pHttpFile);
  if(fileGet)
  {
   DWORD dwStatus;
   DWORD dwBuffLen = sizeof(dwStatus);
   BOOL bSuccess = fileGet->QueryInfo(
    HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
    &dwStatus, &dwBuffLen);
   if( bSuccess && dwStatus>= 200 && dwStatus<300 )
   {
    const int ContentSize = 1024000;//maximin size of text form html file.
    char* mbbuf = new char[ContentSize];
    int nRead = fileGet->Read(mbbuf, ContentSize-1);
    if(nRead > 0 && nRead < ContentSize)
    {
     mbbuf[nRead] = 0;
     int nWordCount = MultiByteToWideChar(CP_ACP, NULL, mbbuf, nRead, NULL, NULL);
     if(nWordCount > 0)
     {
      wchar_t* wstrUnicode = new wchar_t[nWordCount+1];
      wstrUnicode[nWordCount] = 0;
      MultiByteToWideChar(CP_ACP, NULL, mbbuf, nRead, wstrUnicode, nWordCount+1);
      strHtml = wstrUnicode;
      delete[] wstrUnicode;
      bRet = true;
     }
    }
    delete[] mbbuf;
   }
   fileGet->Close();
   delete fileGet;
  }
  sess.Close();
  return bRet;
 } 

UNIX主機和WINDOWS主機的回車問題

CString unixRetToWindowRet(const CString& strTxt)//
 {
  if(strTxt.GetLength() > 0)
  {
   wchar_t* buf = new wchar_t[strTxt.GetLength()*2+1];
   const wchar_t* pSrc = (LPCTSTR)strTxt;
   int nCount = 0;  
   if(pSrc[0] == '/n')
    buf[nCount++] = '/r';
   buf[nCount++] = pSrc[0];
   for(int i = 1; i < strTxt.GetLength(); i++)
   {
    if(*(pSrc+i) == '/n' && *(pSrc+i-1) != '/r')
     buf[nCount++] = '/r';
    buf[nCount++] = *(pSrc+i);
   }
   buf[nCount++] = 0;
   return buf;
  }
  return CString();
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章