搜狗workflow--03.异步http请求

洪浩波
2023-12-01
//g++ -std=c++11 -o testhttp_get testhttp_get.cc -I/usr/local/include/workflow -lworkflow

#include <netdb.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>

#include <assert.h>
#include <sys/time.h>
#include <vector>
#include <iostream>


#include "workflow/HttpMessage.h"
#include "workflow/HttpUtil.h"
#include "workflow/WFTaskFactory.h"
#include "workflow/WFFacilities.h"


#define REDIRECT_MAX    5
#define RETRY_MAX       2

static int task_num = 10;
static WFFacilities::WaitGroup wait_group(task_num);//


std::string get_filename(std::string resource){
    std::string::size_type startpos = 0;
    while (startpos!= std::string::npos)
    {
        startpos = resource.find('/');   //找到'.'的位置
        if( startpos != std::string::npos ) //std::string::npos表示没有找到该字符
        {
            resource.replace(startpos,1,"_"); //实施替换,注意后面一定要用""引起来,表示字符串
        }
    }
    return resource;
}

void wget_callback(WFHttpTask *task)
{
	protocol::HttpRequest *req = task->get_req();
	protocol::HttpResponse *resp = task->get_resp();
	int state = task->get_state();
	int error = task->get_error();

	//get_request_uri
	//printf("url %s\n", req->get_request_uri());

	switch (state)
	{
	case WFT_STATE_SYS_ERROR:
		fprintf(stderr, "system error: %s\n", strerror(error));
		break;
	case WFT_STATE_DNS_ERROR:
		fprintf(stderr, "DNS error: %s\n", gai_strerror(error));
		break;
	case WFT_STATE_SSL_ERROR:
		fprintf(stderr, "SSL error: %d\n", error);
		break;
	case WFT_STATE_TASK_ERROR:
		fprintf(stderr, "Task error: %d\n", error);
		break;
	case WFT_STATE_SUCCESS:
		break;
	}

	if (state != WFT_STATE_SUCCESS)
	{
		fprintf(stderr, "Failed. Press Ctrl-C to exit.\n");
		return;
	}


	std::string code;
	resp->get_status_code(code);
	fprintf(stderr, "%s %s\r\n", req->get_request_uri(), code.c_str());

	if(strcmp(code.c_str(), "200") == 0)
	{
        /* Print response body. */
	    const void *body;
	    size_t body_len;

	    resp->get_parsed_body(&body, &body_len);

        std::string filename;
        //bool get_request_uri(std::string& uri) const
        req->get_request_uri(filename);
        filename = "./img/" + get_filename(filename);

		FILE *fp = fopen(filename.c_str(), "wb");
		assert(fp != NULL);

		fwrite(body, 1, body_len, fp);
		fclose(fp);
		fprintf(stderr, "%s save file ok\n", req->get_request_uri());
	}
	else
	{
		fprintf(stderr, "%s status code is not 200\n", req->get_request_uri());
	}	

	wait_group.done();//
}

int main(int argc, char *argv[])
{
    char url[64] = {0};

    for (int i=0; i<task_num; i++)
    {
        sprintf(url, "https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_27%02d.jpg", i + 1);

        WFHttpTask *task;
		task = WFTaskFactory::create_http_task(url, REDIRECT_MAX, RETRY_MAX,
										   wget_callback);
		protocol::HttpRequest *req = task->get_req();
		req->add_header_pair("Accept", "*/*");
		req->add_header_pair("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0");
		req->add_header_pair("Connection", "close");
		task->start();

		std::cout<<"start: "<<url<<std::endl;
    }

	wait_group.wait();
	
	return 0;
}
[gdut17@localhost test]$ g++ -std=c++11 -o testhttp_get testhttp_get.cc -I/usr/local/include/workflow -lworkflow
[gdut17@localhost test]$ 
[gdut17@localhost test]$ 
[gdut17@localhost test]$ ll
总用量 10600
drwxrwxr-x. 2 gdut17 gdut17     111 11月  5 11:35 img
-rw-rw-r--. 1 gdut17 gdut17    1220 11月  5 11:22 server.crt
-rw-rw-r--. 1 gdut17 gdut17    1675 11月  5 11:21 server.key
-rwxrwxr-x. 1 gdut17 gdut17 3613936 11月  5 11:18 testhttp
-rw-rw-r--. 1 gdut17 gdut17     672 11月  5 11:18 testhttp.cc
-rwxrwxr-x. 1 gdut17 gdut17 3598248 11月  5 11:39 testhttp_get
-rw-rw-r--. 1 gdut17 gdut17    3295 11月  5 11:39 testhttp_get.cc
-rwxrwxr-x. 1 gdut17 gdut17 3613944 11月  5 11:23 testhttps
-rw-rw-r--. 1 gdut17 gdut17     717 11月  5 11:23 testhttps.cc
-rw-rw-r--. 1 gdut17 gdut17       0 11月  4 23:18 testmysql.cc
-rw-rw-r--. 1 gdut17 gdut17       0 11月  4 23:17 testredis.cc
[gdut17@localhost test]$ 

[gdut17@localhost test]$ ./testhttp_get 
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2701.jpg
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2702.jpg
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2703.jpg
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2704.jpg
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2705.jpg
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2706.jpg
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2707.jpg
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2708.jpg
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2709.jpg
start: https://meituba.jmsla.cn/uploads/allimg/2019/04/02/92_2710.jpg
/uploads/allimg/2019/04/02/92_2705.jpg 404
/uploads/allimg/2019/04/02/92_2705.jpg status code is not 200
/uploads/allimg/2019/04/02/92_2706.jpg 404
/uploads/allimg/2019/04/02/92_2706.jpg status code is not 200
/uploads/allimg/2019/04/02/92_2704.jpg 200
/uploads/allimg/2019/04/02/92_2704.jpg save file ok
/uploads/allimg/2019/04/02/92_2703.jpg 200
/uploads/allimg/2019/04/02/92_2703.jpg save file ok
/uploads/allimg/2019/04/02/92_2708.jpg 200
/uploads/allimg/2019/04/02/92_2708.jpg save file ok
/uploads/allimg/2019/04/02/92_2702.jpg 404
/uploads/allimg/2019/04/02/92_2702.jpg status code is not 200
/uploads/allimg/2019/04/02/92_2710.jpg 200
/uploads/allimg/2019/04/02/92_2710.jpg save file ok
/uploads/allimg/2019/04/02/92_2701.jpg 200
/uploads/allimg/2019/04/02/92_2701.jpg save file ok
/uploads/allimg/2019/04/02/92_2707.jpg 200
/uploads/allimg/2019/04/02/92_2707.jpg save file ok
/uploads/allimg/2019/04/02/92_2709.jpg 200
/uploads/allimg/2019/04/02/92_2709.jpg save file ok

 类似资料: