How to simulate the browser to login in https website using c++ based on Linux? -
everybody,my goal logging in https website , downloading webpage using c++ background service program based on linux. detail needs follow: (1)connect "https://www.space-track.org/auth/login" (2)enter username , password in order login in successful (3)post formdata website (4)downloading webpage.
now,my method using mfc::cinternetsession(code follow. in ms-windows),but it's not successful. there must exist problems in codes. hope can me solve problem. maybe can come better solutions using c++ simulate browser based on linux. thank much!
url = "https://www.space-track.org/auth/login/"; nport = internet_default_http_port; cstring strheaders = _t("content-type: application/x-www-form-urlencoded"); if (afxparseurl(url,dwsevicetype,strservername,strtarget,nport) == 0) return false; cinternetsession sess; sess.setoption(internet_option_connect_timeout,1000*20); sess.enablestatuscallback(true); chttpconnection* phttpconnect = sess.gethttpconnection(strservername,nport); chttpfile* phttpfile = phttpconnect->openrequest(chttpconnection::http_verb_post, strtarget,null,1,null,null,internet_flag_secure); cstring strusername = "*****"; cstring strpassword = "*****"; cstring struserinfo; struserinfo.format(_t("identity=%s&password=%s"),strusername,strpassword); try { bool bresult =phttpfile->sendrequest(strheaders,(lpvoid)(lpctstr)struserinfo,struserinfo.getlength()* sizeof(tchar)); //bool bresult =phttpfile->sendrequest(strheaders); } catch (cinternetexception* pexception) { pexception->m_dwerror; pexception->delete(); } phttpfile->setreadbuffersize(2048); cstring str; cstring strgetdata; while(phttpfile->readstring(strgetdata)) { str +="\r\n"; str +=strgetdata; } cstring filename("index.html"); cfile file(filename,cfile::modecreate | cfile::modewrite); file.write(str,str.getlength()); file.close(); phttpfile->close(); delete phttpfile; phttpconnect->close(); delete phttpconnect; sess.close(); return true;
there couple of linux libraries implement http client api, can used implement http/https requests in c or c++.
the grand-daddy of them w3c's own libwww:
a more recent http/https client library libcurl:
either 1 of them can used implement http/https client in c or c++. however, in cases, before using them need have understanding of http/https protocols work; https when comes certificate validation , verification.
both of these libraries common, , linux distributions have them packaged. have 1 or both of them installed already.
Comments
Post a Comment