我如何使用C保存网页++? Windows或Linux系统 [英] How do I save a webpage using C++? Windows or Linux System
本文介绍了我如何使用C保存网页++? Windows或Linux系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道如何保存在Windows和/或Linux中使用C ++的网页。
步骤1)这是我目前的code打开网页:
的ShellExecute(NULL,打开,websiteURL,NULL,NULL,SW_SHOWNORMAL);
第2步)这是一步,我会保存打开的网页为.txt
您的帮助在这里。
步骤3)这是我将其保存为.txt之后关闭该网页尝试;但是,目前没有工作。
的ShellExecute(NULL,关闭,websiteURL,NULL,NULL,SW_SHOWNORMAL);
解决方案
这是Windows版本。这是UNI code,但结果是ASCII。
的#includeWINDOWS.H
#包括Winhttp.h
#包括LT&;&iostream的GT;
#包括LT&;串GT;
#包括LT&;&的fstream GT;的#pragma评论(LIB,winhttp.lib)BOOL ReadWebPage(标准::字符串&安培;源,布尔安全,常量为wchar_t * URL,常量为wchar_t *动词)
{
来源=错误;
BOOL bResults = FALSE;
HINTERNET hsession = NULL;
HINTERNET hconnect = NULL;
HINTERNET hrequest = NULL; hsession = WinHttpOpen(L实施例,WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);
如果转到错误(hsession!); hconnect = WinHttpConnect(hsession,网址,确保INTERNET_DEFAULT_HTTPS_PORT:INTERNET_DEFAULT_HTTP_PORT,0);
如果转到错误(hconnect!); hrequest = WinHttpOpenRequest(hconnect,LGET,动词,NULL,WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,安全WINHTTP_FLAG_SECURE:0);
如果转到错误(hrequest!); 如果(!WinHttpSendRequest(hrequest,WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0,0,0))
转到错误; 如果(!WinHtt preceiveResponse(hrequest,NULL))
转到错误; 来源=;
为(DWORD大小= 1,下载= 1;下载大于0)
{
WinHttpQueryDataAvailable(hrequest,&安培;大小);
如果中断(大小!);
标准::字符串的buf(大小+ 1,0);
WinHtt preadData(hrequest,(LPVOID)及的buf [0],大小,和放大器;下载);
buf.resize(下载);
源+ = BUF;
}错误:
如果(hrequest)WinHttpCloseHandle(hrequest);
如果(hconnect)WinHttpCloseHandle(hconnect);
如果(hsession)WinHttpCloseHandle(hsession); 返回bResults;
}诠释的main()
{
标准::字符串SRC; // false表示没有安全网站(HTTP),真正的是安全(HTTPS)
ReadWebPage(SRC,假的,Lwww.stackoverflow.comL的问题/ 29547368); //性病::法院LT&;< SRC<<的std :: ENDL;
的std :: ofstream的F(C:\\\\ \\\\温度的test.txt);
F<< SRC; 返回0;
}
I need to know how to save a webpage using C++ on a Windows and/or Linux.
Step 1) This is my current code that opens the webpage:
ShellExecute(NULL, "open", websiteURL, NULL, NULL, SW_SHOWNORMAL);
Step 2) This is the step where I will save the webpage that is opened as a .txt
Your help here.
Step 3) This is my attempt at closing the webpage after saving it as a .txt; However, it does not work currently.
ShellExecute(NULL, "close", websiteURL, NULL, NULL, SW_SHOWNORMAL);
解决方案
This is Windows version. It's unicode but the result is 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 = "";
for (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 is for 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;
}
这篇关于我如何使用C保存网页++? Windows或Linux系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文