如何检查网站是否包含c++中的字符串
How do I check if a website contains a string in c++?
所以我在很大程度上是一个c#程序员,但我希望切换到c++,我正在寻找如何用c++阅读网站的答案,然后检查它是否有特定的字符串,这是我的c#代码供参考。
string stringexample = "active";
WebClient wb = new WebClient();
string LIST = wb.DownloadString("URL");
if (LIST.Contains(stringexample))
您可以使用以下步骤:
- 使用HTTP请求页面
- 将响应存储到
std::string
中 - 使用
std::string::find
这里的技巧部分是步骤1。C++没有标准的HTTP客户端。它也没有标准的网络API。您可以在此处找到HTTP规范:https://www.rfc-editor.org/rfc/rfc2616您可以使用它来实现HTTP客户端。但是,与所有编程任务一样,使用现有的实现可以节省大量工作。
标准C++没有网络实用程序,但您可以使用boost::asio
库下载网页内容并搜索字符串"active"
。
一种方法:
boost::asio::ip::tcp::iostream stream("www.example.com", "http");
stream << "GET /something/here HTTP/1.1rn";
stream << "Host: www.example.comrn";
stream << "Accept: */*rn";
stream << "Connection: closernrn";
stream.flush();
std::ostringstream ss;
ss << stream.rdbuf();
std::string str{ ss.str() };
if (auto const n = str.find("active") != std::string::npos)
std::cout << "foundn";
else
std::cout << "nopen";
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 编译包含字符串的代码时遇到问题
- c++库的公共头文件中应该包含什么
- 将包含C样式数组的对象初始化为成员变量(C++)
- 是否需要删除包含对象的"pair"?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 使用mongocxx驱动程序时包含头文件问题
- 如何在h文件中包含.o对象文件
- 在混合代码库中将C转换为C++时出现许多包含错误
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- cmath抛出错误C2062、C2059、C2143和C2447.cmath包含在矢量文件中
- 为什么您需要C++头文件的包含保护
- 无法在UE4中包含BP类到CPP类
- g++ 说函数不存在,即使包含正确的标头
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- Visual C++GC接口如何启用它以及要包含哪个库
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)