CInternetSession的简单使用例子

来源:互联网 发布:淘宝收藏代码在线生成 编辑:程序博客网 时间:2024/06/07 19:48

1. CInternetSession的简单使用

Cpp代码  收藏代码
  1. CInternetSession session;  
  2. CHttpFile *file = NULL;  
  3. CString strURL = " http://www.google.com";  
  4. CString strHtml = "”;   //存放网页数据  
  5.   
  6. try{  
  7.        file = (CHttpFile*)session.OpenURL(strURL);  
  8. }catch(CInternetException * m_pException){  
  9.        file = NULL;  
  10.        m_pException->m_dwError;  
  11.        m_pException->Delete();  
  12.        session.Close();  
  13.        MessageBox("CInternetException");  
  14. }  
  15. CString strLine;  
  16. if(file != NULL){  
  17.        while(file->ReadString(strLine) != NULL){  
  18.        strHtml += strLine;  
  19.        }  
  20. }else{  
  21.        MessageBox("fail");  
  22. }  
  23. session.Close();  
  24. file->Close();  
  25. delete file;  
  26. file = NULL;  

  

 

2. CInternetSession的代理与超时使用

Cpp代码  收藏代码
  1. CInternetSession session;  
  2. CHttpFile *file = NULL;    
  3.   
  4. INTERNET_PROXY_INFO proxyinfo;  
  5. proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;  
  6. proxyinfo.lpszProxy ="211.104.243.73:8080";  
  7. proxyinfo.lpszProxyBypass = NULL;  
  8.   
  9. session.SetOption(INTERNET_OPTION_PROXY,(LPVOID)&proxyinfo,  
  10. sizeof(INTERNET_PROXY_INFO));  
  11. session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 5000);      // 5秒的连接超时  
  12. session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000);           // 1秒的发送超时  
  13. session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 7000);        // 7秒的接收超时  
  14. session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000);     // 1秒的发送超时  
  15. session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 7000);       // 7秒的接收超时  
  16. session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);          // 1次重试  
  17.   
  18. try{  
  19.        file = (CHttpFile*)session.OpenURL("http://www.163.com",1,  
  20. INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE);  
  21. }catch(CInternetException * m_pException){  
  22.        file = NULL;  
  23.        m_pException->m_dwError;  
  24.        m_pException->Delete();  
  25.        session.Close();  
  26.        MessageBox("CInternetException");  
  27. }  
  28.   
  29. CString strLine;  
  30. if(file != NULL){  
  31.        while(file->ReadString(strLine) != NULL){  
  32.               MessageBox(strLine);  
  33.        }  
  34. }else{  
  35.        MessageBox("fail");  
  36. }  
  37. file->Close();  
  38. session.Close();  

  

 

 

3. CInternetSessionPOST使用

Cpp代码  收藏代码
  1. CInternetSession m_InetSession(_T("session"),  
  2.        0,  
  3.        INTERNET_OPEN_TYPE_PRECONFIG,  
  4.        NULL,  
  5.        NULL,  
  6.        INTERNET_FLAG_DONT_CACHE);     //设置不缓冲  
  7. CHttpConnection* pServer = NULL;  
  8. CHttpFile* pFile = NULL;  
  9. CString strHtml = "";  
  10. CString strRequest = "name=123&pwd=321\r\n"//POST过去的数据  
  11. CString strHeaders = "Accept: */*\r\nReferer: http://www.goodwaiter.com/\r\nAccept-Language: zh-cn\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";  
  12.   
  13. try{  
  14.        INTERNET_PORT nPort; //端口  
  15.      nPort=80;  
  16.        pServer = m_InetSession.GetHttpConnection("www.goodwaiter.com", nPort);  
  17.        pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/");  
  18.        pFile->AddRequestHeaders(strHeaders);  
  19.        pFile->SendRequestEx(strRequest.GetLength());  
  20.        pFile->WriteString(strRequest); //重要-->m_Request 中有"name=aaa&name2=BBB&..."  
  21.        pFile->EndRequest();  
  22.   
  23.        DWORD dwRet;  
  24.        pFile->QueryInfoStatusCode(dwRet);  
  25.   
  26.        if (dwRet == HTTP_STATUS_OK){  
  27.               CString strLine;  
  28.               while ((nRead = pFile->ReadString(strLine))>0)  
  29.               {  
  30.                      strHtml += strLine;  
  31.               }  
  32.        }      
  33.        delete pFile;  
  34.        delete pServer;  
  35. }  
  36. catch (CInternetException* e){  
  37.        e->m_dwContext;  
  38. }  
0 0
原创粉丝点击