C++ REST图片下载

GitHub - microsoft/cpprestsdk: The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.

handler.h

cpp 复制代码
//hander.h
#ifndef HANDLER_H
#define HANDLER_H
#include <iostream>
#include "stdafx.h"
//#include "../dbms/include/Dbms.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/http_listener.h>              // HTTP server
#include <cpprest/json.h>                       // JSON library
#include <cpprest/uri.h>                        // URI library
#include <cpprest/ws_client.h>                  // WebSocket client
#include <cpprest/containerstream.h>            // Async streams backed by STL containers
#include <cpprest/interopstream.h>              // Bridges for integrating Async streams with STL and WinRT streams
#include <cpprest/rawptrstream.h>               // Async streams backed by raw pointer to memory
#include <cpprest/producerconsumerstream.h>     // Async streams for producer consumer scenarios

using namespace std;
using namespace web;
using namespace http;
using namespace utility;
using namespace http::experimental::listener;


class handler
{
public:
	handler();
	handler(utility::string_t url);
	virtual ~handler();

	pplx::task<void>open() { return m_listener.open(); }
	pplx::task<void>close() { return m_listener.close(); }

protected:

private:
	void handle_get(http_request message);
	void handle_put(http_request message);
	void handle_post(http_request message);
	void handle_delete(http_request message);
	void handle_error(pplx::task<void>& t);
	http_listener m_listener;
};

#endif // HANDLER_H

handler.cpp

cpp 复制代码
//hander.cpp
#include "stdafx.h"
#include "handler.h"

handler::handler()
{
    //ctor
}
handler::handler(utility::string_t url) :m_listener(url)
{
    m_listener.support(methods::GET, std::bind(&handler::handle_get, this, std::placeholders::_1));
    m_listener.support(methods::PUT, std::bind(&handler::handle_put, this, std::placeholders::_1));
    m_listener.support(methods::POST, std::bind(&handler::handle_post, this, std::placeholders::_1));
    m_listener.support(methods::DEL, std::bind(&handler::handle_delete, this, std::placeholders::_1));

}
handler::~handler()
{
    //dtor
}

void handler::handle_error(pplx::task<void>& t)
{
    try
    {
        t.get();
    }
    catch (...)
    {
        // Ignore the error, Log it if a logger is available
    }
}


//
// Get Request 
//
void handler::handle_get(http_request message)
{
    ucout << message.to_string() << endl;

    auto paths = http::uri::split_path(http::uri::decode(message.relative_uri().path()));

    message.relative_uri().path();
    //Dbms* d  = new Dbms();
    //d->connect();

    concurrency::streams::fstream::open_istream(U("static/index.html"), std::ios::in).then([=](concurrency::streams::istream is)
    {
        message.reply(status_codes::OK, is, U("text/html"))
            .then([](pplx::task<void> t)
        {
            try {
                t.get();
            }
            catch (...) {
                //
            }
        });
    }).then([=](pplx::task<void>t)
    {
        try {
            t.get();
        }
        catch (...) {
            message.reply(status_codes::InternalError, U("INTERNAL ERROR "));
        }
    });

    return;

};

//
// A POST request
//
void handler::handle_post(http_request message)
{
    ucout << message.to_string() << endl;


    message.reply(status_codes::OK, message.to_string());
    return;
};

//
// A DELETE request
//
void handler::handle_delete(http_request message)
{
    ucout << message.to_string() << endl;

    wstring rep = U("WRITE YOUR OWN DELETE OPERATION");
    message.reply(status_codes::OK, rep);
    return;
};


//
// A PUT request 
//
void handler::handle_put(http_request message)
{
    ucout << message.to_string() << endl;
    wstring rep = U("WRITE YOUR OWN PUT OPERATION");
    message.reply(status_codes::OK, rep);
    return;
};

example.cpp

cpp 复制代码
// example.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include "stdafx.h"
#include "handler.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/http_listener.h>              // HTTP server
#include <cpprest/json.h>                       // JSON library
#include <cpprest/uri.h>                        // URI library
#include <cpprest/ws_client.h>                  // WebSocket client
#include <cpprest/containerstream.h>            // Async streams backed by STL containers
#include <cpprest/interopstream.h>              // Bridges for integrating Async streams with STL and WinRT streams
#include <cpprest/rawptrstream.h>               // Async streams backed by raw pointer to memory
#include <cpprest/producerconsumerstream.h>     // Async streams for producer consumer scenarios

using namespace std;
using namespace web;
using namespace http;
using namespace utility;
using namespace http::experimental::listener;




std::unique_ptr<handler> g_httpHandler;

void on_initialize(const string_t& address)
{


	uri_builder uri(address);


	auto addr = uri.to_uri().to_string();
	g_httpHandler = std::unique_ptr<handler>(new handler(addr));
	g_httpHandler->open().wait();

	ucout << utility::string_t(U("Listening for requests at: ")) << addr << std::endl;

	return;
}

void on_shutdown()
{
	g_httpHandler->close().wait();
	return;
}

#ifdef _WIN32
int wmain(int argc, wchar_t *argv[])
#else
int main(int argc, char *argv[])
#endif
{
	utility::string_t port = U("34568");
	if (argc == 2)
	{
		port = argv[1];
	}

	utility::string_t address = U("http://127.0.0.1:");
	address.append(port);

	on_initialize(address);
	std::cout << "Press ENTER to exit." << std::endl;

	std::string line;
	std::getline(std::cin, line);

	on_shutdown();
	return 0;
}

图片下载

cpp 复制代码
// crawlerImg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
//#include <cpprest/http_client.h>
//#include <cpprest/filestream.h>
//
//using namespace utility;                    // Common utilities like string conversions
//using namespace web;                        // Common features like URIs.
//using namespace web::http;                  // Common HTTP functionality
//using namespace web::http::client;          // HTTP client features
//using namespace concurrency::streams;       // Asynchronous streams
//
//int main(int argc, char* argv[])
//{
//	auto fileStream = std::make_shared<ostream>();
//
//	// Open stream to output file.
//	pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
//	{
//		*fileStream = outFile;
//
//		// Create http_client to send the request.
//		http_client client(U("http://www.bing.com/"));
//
//		// Build request URI and start the request.
//		uri_builder builder(U("/search"));
//		builder.append_query(U("q"), U("cpprestsdk github"));
//		return client.request(methods::GET, builder.to_string());
//	})
//
//		// Handle response headers arriving.
//		.then([=](http_response response)
//	{
//		printf("Received response status code:%u\n", response.status_code());
//
//		// Write response body into the file.
//		return response.body().read_to_end(fileStream->streambuf());
//	})
//
//		// Close the file stream.
//		.then([=](size_t)
//	{
//		return fileStream->close();
//	});
//
//	// Wait for all the outstanding I/O to complete and handle any exceptions
//	try
//	{
//		requestTask.wait();
//	}
//	catch (const std::exception &e)
//	{
//		printf("Error exception:%s\n", e.what());
//	}
//

	//同步方式
	//auto fileStream = std::make_shared<ostream>();
	//ostream outFile = fstream::open_ostream(U("results.html")).get();
	//*fileStream = outFile;

	 Create http_client to send the request.
	//http_client client(U("http://www.bing.com/"));

	 Build request URI and start the request.
	//uri_builder builder(U("/search"));
	//builder.append_query(U("q"), U("cpprestsdk github"));
	//http_response response = client.request(methods::GET, builder.to_string()).get();
	 Write response body into the file.
	//response.body().read_to_end(fileStream->streambuf()).get();
	//fileStream->close().get();
//	return 0;
//}

//访问一个网站并将该内容保存在results.html里面,这里用到了微软自家的PPL并行计算库


#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <memory>
#include <regex>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/containerstream.h>

// 请求并解析url
bool get_result(const std::string& url, const std::string& pattern, std::vector<std::string>& vec)
{
	try
	{
		web::http::client::http_client client(web::uri(utility::conversions::to_string_t(url)));
		web::http::http_response response = client.request(web::http::methods::GET).get();

		concurrency::streams::stringstreambuf buffer;
		response.body().read_to_end(buffer).get();
		std::string& str = buffer.collection();

		// 使用C++11提供的正则表达式库
		std::regex r(pattern);
		for (std::sregex_iterator iter(str.begin(), str.end(), r), end; iter != end; ++iter)
		{
			std::cout << iter->str() << std::endl;
			vec.emplace_back(iter->str());
		}
	}
	catch (std::exception& e)
	{
		std::cout << "Exception: " << e.what() << std::endl;
		return false;
	}

	return true;
}

// 获取图片
bool get_result(const std::string& url, std::string& picture)
{
	try
	{
		web::http::client::http_client client(web::uri(utility::conversions::to_string_t(url)));
		web::http::http_response response = client.request(web::http::methods::GET).get();

		concurrency::streams::stringstreambuf buffer;
		response.body().read_to_end(buffer).get();
		picture = buffer.collection();
	}
	catch (std::exception& e)
	{
		std::cout << "Exception: " << e.what() << std::endl;
		return false;
	}

	return true;
}

// 保存图片
bool write_to_file(const std::string& file_path, const std::string& data)
{
	try
	{
		std::ofstream file;
		file.open(file_path, std::ios::out | std::ios::binary);
		if (!file.good())
		{
			return false;
		}
		file.write(data.c_str(), data.size());
		file.close();
	}
	catch (std::exception& e)
	{
		std::cout << "Exception: " << e.what() << std::endl;
		return false;
	}

	return true;
}

int main()
{
	// [1] 请求每一页,将子页面的url保存在sub_url_vec里面
	std::vector<std::string> sub_url_vec;
	std::string pattern = "/desk/[0-9]+.htm";
	for (int i = 1; i <= 32; ++i)
	{
		// 创意主题
		std::string url = "http://www.51ztzj.com/dbizhi/category_27_" + std::to_string(i) + ".htm#content_anchor";
		std::cout << "Start get " << i << " page, url: " << url << std::endl;
		// 请求并解析url
		if (!get_result(url, pattern, sub_url_vec))
		{
			std::cout << "Get " << i << " page failed" << std::endl;
		}
	}

	// 最终的图片url:http://img.51ztzj.com//upload/image/20130220/2013022014_670x419.jpg
	// [2] 将子页面的图片url解析出来放入picture_url_vec
	std::vector<std::string> picture_url_vec;
	pattern = "http://img.51ztzj.com//upload/image/.+/.+_670x419.jpg";
	for (std::size_t i = 0; i < sub_url_vec.size(); ++i)
	{
		std::string url = "http://www.51ztzj.com" + sub_url_vec[i];
		std::cout << "Start get " << i + 1 << " sub page, url: " << url << std::endl;
		// 请求并解析url
		if (!get_result(url, pattern, picture_url_vec))
		{
			std::cout << "Get " << i + 1 << " sub page failed" << std::endl;
		}
	}

	// [3] 最后遍历picture_url_vec,然后一个一个的下载图片
	for (std::size_t i = 0; i < picture_url_vec.size(); ++i)
	{
		std::cout << "Start download " << i + 1 << " picture, url: " << picture_url_vec[i] << std::endl;
		std::string picture;
		// 获取图片
		if (!get_result(picture_url_vec[i], picture))
		{
			std::cout << "Download " << i + 1 << " picture failed" << std::endl;
		}

		std::string file_path = "./download/" + std::to_string(i) + ".jpg";
		// 保存图片
		if (!write_to_file(file_path, picture))
		{
			std::cout << "Write to file failed: " << i + 1 << std::endl;
		}
	}

	return 0;
}

创作不易,小小的支持一下吧!

相关推荐
IT技术分享社区1 分钟前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
极客代码4 分钟前
【Python TensorFlow】入门到精通
开发语言·人工智能·python·深度学习·tensorflow
疯一样的码农10 分钟前
Python 正则表达式(RegEx)
开发语言·python·正则表达式
&岁月不待人&32 分钟前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
StayInLove36 分钟前
G1垃圾回收器日志详解
java·开发语言
无尽的大道44 分钟前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化
爱吃生蚝的于勒1 小时前
深入学习指针(5)!!!!!!!!!!!!!!!
c语言·开发语言·数据结构·学习·计算机网络·算法
羊小猪~~1 小时前
数据结构C语言描述2(图文结合)--有头单链表,无头单链表(两种方法),链表反转、有序链表构建、排序等操作,考研可看
c语言·数据结构·c++·考研·算法·链表·visual studio
binishuaio1 小时前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE1 小时前
【Java SE】StringBuffer
java·开发语言