zoukankan      html  css  js  c++  java
  • c++ Get方式获取页面源码(unicode编码)。

    
    
    CString CHttpClient::doGet(CString href) 
    { 
        std::string httpsource;
        CInternetSession session1(NULL,0); 
        CHttpFile* pHTTPFile=NULL; 
    
        try{ 
            pHTTPFile=(CHttpFile*)session1.OpenURL(href); 
        }catch(CInternetException* m_pException){ 
            //捕捉错误,否则在网络断开时连接导致内存问题
            pHTTPFile=NULL; 
            m_pException->m_dwError;
            m_pException->Delete();
            session1.Close(); 
            return _T("");
        } 
        if(pHTTPFile) 
        {
            char sRecived[2048]={0};//初始化,避免内存问题
    
            pHTTPFile->ReadString((LPTSTR)sRecived,1024);//避免使用CString导致乱码,unicode编码时一个字符用两个字节来编码,第二个参数使用字符数
            do
            {
                httpsource+=sRecived;
                memset(sRecived,0,2048);
                //httpsource.append(sRecived);
            } while (pHTTPFile->ReadString((LPTSTR)sRecived,1024)>0);
            pHTTPFile->Close(); 
            delete pHTTPFile; 
            delete[] sRecived;
        } 
        session1.Close();
        wstring result=UTF8ToUnicode(httpsource);//转码
        return result.c_str();
    } 
    
    std::string CHttpClient::UnicodeToUTF8( const std::wstring& str )
    {
     char*     pElementText;
     int    iTextLen;
     // wide char to multi char
     iTextLen = WideCharToMultiByte( CP_UTF8,
             0,
             str.c_str(),
             -1,
             NULL,
             0,
             NULL,
             NULL );
     pElementText = new char[iTextLen + 1];
     memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
     ::WideCharToMultiByte( CP_UTF8,
             0,
             str.c_str(),
             -1,
             pElementText,
             iTextLen,
             NULL,
             NULL );
     std::string strText;
     strText = pElementText;
     delete[] pElementText;
     return strText;
    }
    
    wstring CHttpClient::UTF8ToUnicode( const string& str )
    {
    	int  len = 0;
    	len = str.length();
    	int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,
    		0,
    		str.c_str(),
    		-1,
    		NULL,
    		0 ); 
    	wchar_t *  pUnicode; 
    	pUnicode = new  wchar_t[unicodeLen+1]; 
    	memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
    	::MultiByteToWideChar( CP_UTF8,
    		0,
    		str.c_str(),
    		-1,
    		(LPWSTR)pUnicode,
    		unicodeLen ); 
    	wstring  rt; 
    	rt = ( wchar_t* )pUnicode;
    	delete  pUnicode;
    	return  rt; 
    }
    
    
    

      

     
  • 相关阅读:
    CSU
    ACM-ICPC Beijing Online A The Book List
    约瑟夫环问题
    HDOJ-1124 Factorial 数论
    玲珑杯 ACM热身赛 #2.5 A 记忆化搜索+瞎搞
    CF 711B
    hdu-4289 最大流Dinic模板题
    最大流朴素算法
    HDU-3729 二分匹配 匈牙利算法
    二分图匹配-匈牙利算法
  • 原文地址:https://www.cnblogs.com/cthulhu/p/3411972.html
Copyright © 2011-2022 走看看