example_tc_http_async.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include <iostream>
  17. #include "util/tc_file.h"
  18. #include "util/tc_http_async.h"
  19. using namespace std;
  20. using namespace tars;
  21. class AsyncHttpCallback : public TC_HttpAsync::RequestCallback
  22. {
  23. public:
  24. AsyncHttpCallback(const string &sUrl) : _sUrl(sUrl)
  25. {
  26. }
  27. virtual void onException(const string &ex)
  28. {
  29. cout << "onException:" << _sUrl << ":" << ex << endl;
  30. }
  31. virtual void onResponse(bool bClose, TC_HttpResponse &stHttpResponse)
  32. {
  33. cout << "onResponse:" << _sUrl << endl;//<< ":" << TC_Common::tostr(stHttpResponse.getHeaders()) << endl;
  34. TC_File::save2file("tmp.html", stHttpResponse.getContent());
  35. }
  36. virtual void onTimeout()
  37. {
  38. cout << "onTimeout:" << _sUrl << endl;
  39. }
  40. virtual void onClose()
  41. {
  42. cout << "onClose:" << _sUrl << endl;
  43. }
  44. protected:
  45. string _sUrl;
  46. };
  47. int addAsyncRequest(TC_HttpAsync &ast, const string &sUrl)
  48. {
  49. TC_HttpRequest stHttpReq;
  50. stHttpReq.setGetRequest(sUrl);
  51. TC_HttpAsync::RequestCallbackPtr p = new AsyncHttpCallback(sUrl);
  52. return ast.doAsyncRequest(stHttpReq, p);
  53. }
  54. int main(int argc, char *argv[])
  55. {
  56. try
  57. {
  58. uint64_t i = TC_Common::now2ms();
  59. TC_HttpAsync ast;
  60. ast.setTimeout(10000);
  61. ast.start(1);
  62. // int ret = addAsyncRequest(ast, "www.baidu.com");
  63. int ret = addAsyncRequest(ast, "http://www.qq.com");
  64. cout << ret << endl;
  65. /*
  66. addAsyncRequest(ast, "www.google.com");
  67. addAsyncRequest(ast, "http://news.qq.com/a/20100108/002269.htm");
  68. addAsyncRequest(ast, "http://news.qq.com/zt/2010/mtjunshou/");
  69. addAsyncRequest(ast, "http://news.qq.com/a/20100108/000884.htm");
  70. addAsyncRequest(ast, "http://tech.qq.com/zt/2009/renovate/index.htm");
  71. */
  72. ast.waitForAllDone();
  73. cout << (TC_Common::now2ms() - i) << endl;
  74. }
  75. catch(exception &ex)
  76. {
  77. cout << ex.what() << endl;
  78. }
  79. return 0;
  80. }