1. Simple use of CInternetSession
CInternetSession session;
CHttpFile *file = NULL;
CString strURL = "http://www.20abcd.com";
CString strhtml = ""; Storing Web page data
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's proxy and timeout usage
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-Second Connection timeout
Session. SetOption (internet_option_send_timeout, 1000); 1-Second Send timeout
Session. SetOption (internet_option_receive_timeout, 7000); 7-Second Receive timeout
Session. SetOption (internet_option_data_send_timeout, 1000); 1-Second Send timeout
Session. SetOption (internet_option_data_receive_timeout, 7000); 7-Second Receive timeout
Session. SetOption (internet_option_connect_retries, 1); 1 Times retry
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. Post use of CInternetSession
CInternetSession m_inetsession (_t ("session"),
0,
Internet_open_type_preconfig,
Null
Null
Internet_flag_dont_cache); Set no buffering
chttpconnection* pserver = NULL;
chttpfile* pFile = NULL;
CString strhtml = "";
CString strrequest = "name=123&pwd=321\r\n"; Post past Data
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; Port
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); There are "Name=aaa&name2=bbb&" in important-->m_request.
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;
}