browser - How do I save a webpage using C++? Windows or Linux System -
i need know how save webpage using c++ on windows and/or linux.
step 1) current code opens webpage:
shellexecute(null, "open", websiteurl, null, null, sw_shownormal);
step 2) step save webpage opened .txt
your here.
step 3) attempt @ closing webpage after saving .txt; however, not work currently.
shellexecute(null, "close", websiteurl, null, null, sw_shownormal);
this windows version. it's unicode result ascii.
#include "windows.h" #include "winhttp.h" #include <iostream> #include <string> #include <fstream> #pragma comment(lib, "winhttp.lib") bool readwebpage(std::string &source, bool secure, const wchar_t *url, const wchar_t *verb) { source = "error"; bool bresults = false; hinternet hsession = null; hinternet hconnect = null; hinternet hrequest = null; hsession = winhttpopen(l"example", winhttp_access_type_default_proxy, winhttp_no_proxy_name, winhttp_no_proxy_bypass, 0); if (!hsession) goto error; hconnect = winhttpconnect(hsession, url, secure ? internet_default_https_port : internet_default_http_port, 0); if (!hconnect) goto error; hrequest = winhttpopenrequest(hconnect, l"get", verb, null, winhttp_no_referer, winhttp_default_accept_types, secure ? winhttp_flag_secure : 0); if (!hrequest) goto error; if (!winhttpsendrequest(hrequest, winhttp_no_additional_headers, 0, winhttp_no_request_data, 0, 0, 0)) goto error; if (!winhttpreceiveresponse(hrequest, null)) goto error; source = ""; (dword size = 1, downloaded = 1; downloaded > 0;) { winhttpquerydataavailable(hrequest, &size); if (!size) break; std::string buf(size + 1, 0); winhttpreaddata(hrequest, (lpvoid)&buf[0], size, &downloaded); buf.resize(downloaded); source += buf; } error: if (hrequest) winhttpclosehandle(hrequest); if (hconnect) winhttpclosehandle(hconnect); if (hsession) winhttpclosehandle(hsession); return bresults; } int main() { std::string src; //false indicates not-secure website (http), true secure (https) readwebpage(src, false, l"www.stackoverflow.com", l"questions/29547368"); //std::cout << src << std::endl; std::ofstream f("c:\\temp\\test.txt"); f << src; return 0; }