CInternetSession的簡單使用

來源:互聯網
上載者:User

1. CInternetSession的簡單使用

CInternetSession session;

CHttpFile *file = NULL;

CString strURL = " http://www.20abcd.com";

CString strHtml = "”;   //存放網頁資料

 

try{

       file = (CHttpFile*)session.OpenURL(strURL);

}catch(CInternetException * m_pException){

       file = NULL;

       m_pException->m_dwError;

       m_pException->Delete();

       session.Close();

       MessageBox("CInternetException");

}

CString strLine;

if(file != NULL){

       while(file->ReadString(strLine) != NULL){

       strHtml += strLine;

       }

 

}else{

       MessageBox("fail");

}

 

session.Close();

file->Close();

delete file;

file = NULL;

 

2. CInternetSession的代理與逾時使用

CInternetSession session;

CHttpFile *file = NULL;  

 

INTERNET_PROXY_INFO proxyinfo;

proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;

proxyinfo.lpszProxy ="211.104.243.73:8080";

proxyinfo.lpszProxyBypass = NULL;

session.SetOption(INTERNET_OPTION_PROXY,(LPVOID)&proxyinfo,

sizeof(INTERNET_PROXY_INFO));

 

session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 5000);      // 5秒的連線逾時

session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000);           // 1秒的發送逾時

session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 7000);        // 7秒的接收逾時

session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000);     // 1秒的發送逾時

session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 7000);       // 7秒的接收逾時

session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);          // 1次重試

 

try{

       file = (CHttpFile*)session.OpenURL("http://www.163.com",1,

INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE);

}catch(CInternetException * m_pException){

       file = NULL;

       m_pException->m_dwError;

       m_pException->Delete();

       session.Close();

       MessageBox("CInternetException");

       return;

}

CString strLine;

if(file != NULL){

       while(file->ReadString(strLine) != NULL){

              MessageBox(strLine);

       }

}else{

       MessageBox("fail");

}

file->Close();

session.Close();

 

3. CInternetSession的POST使用

CInternetSession m_InetSession(_T("session"),

       0,

       INTERNET_OPEN_TYPE_PRECONFIG,

       NULL,

       NULL,

       INTERNET_FLAG_DONT_CACHE);     //設定不緩衝

CHttpConnection* pServer = NULL;

CHttpFile* pFile = NULL;

CString strHtml = "";

CString strRequest = "name=123&pwd=321\r\n"; //POST過去的資料

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)";

 

try{

       INTERNET_PORT nPort; //連接埠

       nPort=80;

       pServer = m_InetSession.GetHttpConnection("www.goodwaiter.com", nPort);

       pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/");

       pFile->AddRequestHeaders(strHeaders);

 

       pFile->SendRequestEx(strRequest.GetLength());

       pFile->WriteString(strRequest); //重要-->m_Request 中有"name=aaa&name2=BBB&..."

       pFile->EndRequest();

       DWORD dwRet;

       pFile->QueryInfoStatusCode(dwRet);

 

       if (dwRet == HTTP_STATUS_OK){

              CString strLine;

              while ((nRead = pFile->ReadString(strLine))>0)

              {

                     strHtml += strLine;

              }

       }    

       delete pFile;

       delete pServer;

}

catch (CInternetException* e){

       e->m_dwContext;

}  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.