25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

172 satır
8.7KB

  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <signal.h>
  11. #include <string>
  12. #include <iostream>
  13. #include "Util/MD5.h"
  14. #include "Util/logger.h"
  15. #include "Util/onceToken.h"
  16. #include "Poller/EventPoller.h"
  17. #include "Http/HttpRequester.h"
  18. #include "Http/HttpDownloader.h"
  19. using namespace std;
  20. using namespace toolkit;
  21. using namespace mediakit;
  22. int main(int argc, char *argv[]) {
  23. //设置退出信号处理函数
  24. static semaphore sem;
  25. signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
  26. //设置日志
  27. Logger::Instance().add(std::make_shared<ConsoleChannel>());
  28. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  29. //加载证书,证书包含公钥和私钥
  30. SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data());
  31. //信任某个自签名证书
  32. SSL_Initor::Instance().trustCertificate((exeDir() + "ssl.p12").data());
  33. //不忽略无效证书证书(例如自签名或过期证书)
  34. SSL_Initor::Instance().ignoreInvalidCertificate(false);
  35. ///////////////////////////////http downloader///////////////////////
  36. //下载器map
  37. map<string, HttpDownloader::Ptr> downloaderMap;
  38. //下载两个文件,一个是http下载,一个https下载
  39. auto urlList = {"http://www.baidu.com/img/baidu_resultlogo@2.png",
  40. "https://www.baidu.com/img/baidu_resultlogo@2.png"};
  41. for (auto &url : urlList) {
  42. //创建下载器
  43. HttpDownloader::Ptr downloader(new HttpDownloader());
  44. downloader->setOnResult([](const SockException &ex, const string &filePath) {
  45. DebugL << "=====================HttpDownloader result=======================";
  46. //下载结果回调
  47. if (!ex) {
  48. //文件下载成功
  49. InfoL << "download file success:" << filePath;
  50. } else {
  51. //下载失败
  52. WarnL << "code:" << ex.getErrCode() << " msg:" << ex.what();
  53. }
  54. });
  55. //断点续传功能,开启后可能会遇到416的错误(因为上次文件已经下载完全)
  56. downloader->startDownload(url, exeDir() + MD5(url).hexdigest() + ".jpg", true);
  57. //下载器必须被强引用,否则作用域一失效就会导致对象销毁
  58. downloaderMap.emplace(url, downloader);
  59. }
  60. ///////////////////////////////http get///////////////////////
  61. //创建一个Http请求器
  62. HttpRequester::Ptr requesterGet(new HttpRequester());
  63. //使用GET方式请求
  64. requesterGet->setMethod("GET");
  65. //设置http请求头,我们假设设置cookie,当然你也可以设置其他http头
  66. requesterGet->addHeader("Cookie", "SESSIONID=e1aa89b3-f79f-4ac6-8ae2-0cea9ae8e2d7");
  67. //开启请求,该api会返回当前主机外网ip等信息
  68. requesterGet->startRequester("http://pv.sohu.com/cityjson?ie=utf-8",//url地址
  69. [](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
  70. const Parser &parser) { //http回复body
  71. DebugL << "=====================HttpRequester GET===========================";
  72. if (ex) {
  73. //网络相关的错误
  74. WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
  75. } else {
  76. //打印http回复信息
  77. _StrPrinter printer;
  78. for (auto &pr: parser.getHeader()) {
  79. printer << pr.first << ":" << pr.second << "\r\n";
  80. }
  81. InfoL << "status:" << parser.Url() << "\r\n"
  82. << "header:\r\n" << (printer << endl)
  83. << "\r\nbody:" << parser.Content();
  84. }
  85. });
  86. ///////////////////////////////http post///////////////////////
  87. //创建一个Http请求器
  88. HttpRequester::Ptr requesterPost(new HttpRequester());
  89. //使用POST方式请求
  90. requesterPost->setMethod("POST");
  91. //设置http请求头
  92. requesterPost->addHeader("X-Requested-With", "XMLHttpRequest");
  93. requesterPost->addHeader("Origin", "http://fanyi.baidu.com");
  94. //设置POST参数列表
  95. HttpArgs args;
  96. args["query"] = "test";
  97. args["from"] = "en";
  98. args["to"] = "zh";
  99. args["transtype"] = "translang";
  100. args["simple_means_flag"] = "3";
  101. requesterPost->setBody(args.make());
  102. //开启请求
  103. requesterPost->startRequester("http://fanyi.baidu.com/langdetect",//url地址
  104. [](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
  105. const Parser &parser) { //http回复body
  106. DebugL << "=====================HttpRequester POST==========================";
  107. if (ex) {
  108. //网络相关的错误
  109. WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
  110. } else {
  111. //打印http回复信息
  112. _StrPrinter printer;
  113. for (auto &pr: parser.getHeader()) {
  114. printer << pr.first << ":" << pr.second << "\r\n";
  115. }
  116. InfoL << "status:" << parser.Url() << "\r\n"
  117. << "header:\r\n" << (printer << endl)
  118. << "\r\nbody:" << parser.Content();
  119. }
  120. });
  121. ///////////////////////////////http upload///////////////////////
  122. //创建一个Http请求器
  123. HttpRequester::Ptr requesterUploader(new HttpRequester());
  124. //使用POST方式请求
  125. requesterUploader->setMethod("POST");
  126. //设置http请求头
  127. HttpArgs argsUploader;
  128. argsUploader["query"] = "test";
  129. argsUploader["from"] = "en";
  130. argsUploader["to"] = "zh";
  131. argsUploader["transtype"] = "translang";
  132. argsUploader["simple_means_flag"] = "3";
  133. static string boundary = "0xKhTmLbOuNdArY";
  134. HttpMultiFormBody::Ptr body(new HttpMultiFormBody(argsUploader, exePath(), boundary));
  135. requesterUploader->setBody(body);
  136. requesterUploader->addHeader("Content-Type", HttpMultiFormBody::multiFormContentType(boundary));
  137. //开启请求
  138. requesterUploader->startRequester("http://fanyi.baidu.com/langdetect",//url地址
  139. [](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
  140. const Parser &parser) { //http回复body
  141. DebugL << "=====================HttpRequester Uploader==========================";
  142. if (ex) {
  143. //网络相关的错误
  144. WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
  145. } else {
  146. //打印http回复信息
  147. _StrPrinter printer;
  148. for (auto &pr: parser.getHeader()) {
  149. printer << pr.first << ":" << pr.second << "\r\n";
  150. }
  151. InfoL << "status:" << parser.Url() << "\r\n"
  152. << "header:\r\n" << (printer << endl)
  153. << "\r\nbody:" << parser.Content();
  154. }
  155. });
  156. sem.wait();
  157. return 0;
  158. }