ZLMediaKit/tests/test_httpClient.cpp

158 lines
6.1 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2017-05-05 23:54:59 +08:00
#include <signal.h>
2017-09-30 13:00:12 +08:00
#include <string>
2017-05-05 23:54:59 +08:00
#include <iostream>
2017-09-30 13:00:12 +08:00
#include "Util/MD5.h"
#include "Util/File.h"
2017-05-05 23:54:59 +08:00
#include "Util/logger.h"
#include "Util/onceToken.h"
#include "Poller/EventPoller.h"
2017-09-30 13:00:12 +08:00
#include "Http/HttpRequester.h"
#include "Http/HttpDownloader.h"
2017-05-05 23:54:59 +08:00
using namespace std;
using namespace ZL::Util;
using namespace ZL::Http;
using namespace ZL::Poller;
using namespace ZL::Network;
2017-09-30 13:00:12 +08:00
int main(int argc,char *argv[]){
//设置退出信号处理函数
signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
2017-05-05 23:54:59 +08:00
///////////////////////////////http downloader///////////////////////
2017-09-30 13:00:12 +08:00
//下载器map
map<string,HttpDownloader::Ptr> downloaderMap;
//下载两个文件一个是http下载一个https下载
2017-05-05 23:54:59 +08:00
auto urlList = {"http://img3.imgtn.bdimg.com/it/u=158031390,1321729164&fm=214&gp=0.jpg",
2017-09-30 13:00:12 +08:00
"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=931786003,1029770543&fm=27&gp=0.jpg"};
for(auto &url : urlList){
//创建下载器
2017-05-05 23:54:59 +08:00
HttpDownloader::Ptr downloader(new HttpDownloader());
downloader->setOnResult([](ErrCode code,const char *errMsg,const char *filePath){
2017-09-30 13:00:12 +08:00
DebugL << "=====================HttpDownloader result=======================";
//下载结果回调
2017-05-05 23:54:59 +08:00
if(code == Err_success){
2017-09-30 13:00:12 +08:00
//文件下载成功
2017-05-05 23:54:59 +08:00
InfoL << "download file success:" << filePath;
}else{
2017-09-30 13:00:12 +08:00
//下载失败
2017-05-05 23:54:59 +08:00
WarnL << "code:" << code << " msg:" << errMsg;
}
});
//断点续传功能,开启后可能会遇到416的错误因为上次文件已经下载完全
2017-09-30 13:00:12 +08:00
downloader->startDownload(url,exeDir() + MD5(url).hexdigest() + ".jpg",true);
//下载器必须被强引用,否则作用域一失效就会导致对象销毁
downloaderMap.emplace(url,downloader);
2017-05-05 23:54:59 +08:00
}
///////////////////////////////http get///////////////////////
2017-09-30 13:00:12 +08:00
//创建一个Http请求器
2017-05-05 23:54:59 +08:00
HttpRequester::Ptr requesterGet(new HttpRequester());
2017-09-30 13:00:12 +08:00
//使用GET方式请求
2017-05-05 23:54:59 +08:00
requesterGet->setMethod("GET");
2017-09-30 13:00:12 +08:00
//设置http请求头我们假设设置cookie当然你也可以设置其他http头
2017-05-05 23:54:59 +08:00
requesterGet->addHeader("Cookie","SESSIONID=e1aa89b3-f79f-4ac6-8ae2-0cea9ae8e2d7");
2017-09-30 13:00:12 +08:00
//开启请求该api会返回当前主机外网ip等信息
requesterGet->startRequester("http://pv.sohu.com/cityjson?ie=utf-8",//url地址
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
const string &status, //http回复的状态码比如说200/404
const HttpClient::HttpHeader &header, //http回复头
const string &strRecvBody){ //http回复body
DebugL << "=====================HttpRequester GET===========================";
2017-05-05 23:54:59 +08:00
if(ex){
2017-09-30 13:00:12 +08:00
//网络相关的错误
2017-05-05 23:54:59 +08:00
WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
}else{
2017-09-30 13:00:12 +08:00
//打印http回复信息
2017-05-05 23:54:59 +08:00
_StrPrinter printer;
for(auto &pr: header){
printer << pr.first << ":" << pr.second << "\r\n";
}
2017-09-30 13:00:12 +08:00
InfoL << "status:" << status << "\r\n"
<< "header:\r\n" << (printer << endl)
<< "\r\nbody:" << strRecvBody;
2017-05-05 23:54:59 +08:00
}
});
///////////////////////////////http post///////////////////////
2017-09-30 13:00:12 +08:00
//创建一个Http请求器
2017-05-05 23:54:59 +08:00
HttpRequester::Ptr requesterPost(new HttpRequester());
2017-09-30 13:00:12 +08:00
//使用POST方式请求
2017-05-05 23:54:59 +08:00
requesterPost->setMethod("POST");
2017-09-30 13:00:12 +08:00
//设置http请求头
requesterPost->addHeader("X-Requested-With","XMLHttpRequest");
requesterPost->addHeader("Origin","http://fanyi.baidu.com");
//设置POST参数列表
2017-05-05 23:54:59 +08:00
HttpArgs args;
args["query"] = "test";
args["from"] = "en";
args["to"] = "zh";
args["transtype"] = "translang";
args["simple_means_flag"] = "3";
requesterPost->setBody(args.make());
2017-09-30 13:00:12 +08:00
//开启请求
requesterPost->startRequester("http://fanyi.baidu.com/langdetect",//url地址
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
const string &status, //http回复的状态码比如说200/404
const HttpClient::HttpHeader &header, //http回复头
const string &strRecvBody){ //http回复body
DebugL << "=====================HttpRequester POST==========================";
2017-05-05 23:54:59 +08:00
if(ex){
2017-09-30 13:00:12 +08:00
//网络相关的错误
2017-05-05 23:54:59 +08:00
WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
} else {
2017-09-30 13:00:12 +08:00
//打印http回复信息
2017-05-05 23:54:59 +08:00
_StrPrinter printer;
for(auto &pr: header){
printer << pr.first << ":" << pr.second << "\r\n";
}
2017-09-30 13:00:12 +08:00
InfoL << "status:" << status << "\r\n"
<< "header:\r\n" << (printer << endl)
<< "\r\nbody:" << strRecvBody;
2017-05-05 23:54:59 +08:00
}
});
2017-09-30 13:00:12 +08:00
//事件轮询
2017-05-05 23:54:59 +08:00
EventPoller::Instance().runLoop();
2017-09-30 13:00:12 +08:00
//清空下载器
downloaderMap.clear();
requesterGet.reset();
requesterPost.reset();
//程序开始退出
EventPoller::Destory();
2018-02-24 15:18:16 +08:00
AsyncTaskThread::Destory();
2017-09-30 13:00:12 +08:00
Logger::Destory();
2017-05-05 23:54:59 +08:00
return 0;
}