| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #include <iostream>
- #include <fstream>
- // 3rd_party 消除min max函数冲突
- #undef min
- #undef max
- #define NOMINMAX
- #include "json.hpp"
- using json = nlohmann::json;
- #define CPPHTTPLIB_OPENSSL_SUPPORT
- #include "httplib.h"
- int main()
- {
- json httpJson;
- httplib::Client cli("https://www.okx.com");
- // cli.set_proxy_basic_auth("user", "pass"); //匿名代理不需要设置认证信息
- cli.set_proxy("127.0.0.1", 1024);
-
- // 设置Chrome浏览器User-Agent
- httplib::Headers headers = {
- { "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36" }
- };
- httplib::Params params;
- params.emplace("instId", "BTC-USDT");
- params.emplace("bar", "1m");
- params.emplace("limit", "100");
- for (int i = 0; i < 50; i++) {
- auto res = cli.Get("/api/v5/market/candles", params, headers);
- if (res->status == 200) {
- json kLine;
- try {
- kLine = json::parse(res->body);
- std::string code = kLine["code"];
- std::cout << "num: " << i << "dataSize:" << kLine["data"].size() << "\n" << std::endl;
- // std::cout << "dataSize:\n" << kLine["data"].size() << std::endl;
- //std::cout << "Parsed JSON:\n" << kLine.dump(4) << std::endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- catch (const std::exception& e) {
- throw std::runtime_error("Json解析错误: " + std::string(e.what()) + "!");
- break;
- }
- }
- }
-
-
- }
|