提升http写未定义的错误问题

问题描述

我正在尝试使用boost http和beast Websocket进行加密货币交易。当前Websocket可以正常工作,但是当我休息发送命令时,它返回Undefined error:0。我对Websocket和http使用相同的io_service。我检查了在写帖子http时发生的错误。请分享您的智慧来解决这个问题。谢谢!

double SendFokBuyOrder(const std::string& currencyPair,double rate,dou    ble amount) {
    beast::error_code ec;
    try {
    std::cout << "send fok buy\n";
    double Traded_quantity = 0;
    std::stringstream ss;
    ss << std::setprecision(8);
    ss << std::fixed;
    ss << "command=buy&currencyPair=";
    ss << currencyPair;
    ss << "&rate=";
    ss << rate;
    ss << "&amount=";
    ss << amount;
    ss << "&fillOrKill=1";
    ss << "&nonce=";
    ss << get_current_ms_epoch();

    std::cout << ss.str() << std::endl;
    http::request<http::string_body> req(http::verb::post,"/TradingApi",11);
    req.set(http::field::host,host);
    req.set(http::field::user_agent,BOOST_BEAST_VERSION_STRING);
    req.set(http::field::content_length,ss.str().length());
    req.set(http::field::content_type,"application/x-www-form-urlencode    d");
    req.set("Key",api_key_);
    req.set("Sign",hmac_sha512(api_secret_,ss.str()));

    req.body() = ss.str();

    http::write(stream_,req,ec); // error occurs here !
    std::cout << "after write\n" << ec.message() << std::endl;
    http::read(stream_,buffer_,res_,ec);
    std::cout << "after read\n" << ec.message() << std::endl;
    std::cout << boost::beast::buffers_to_string(res_.body().data()) <<     std::endl;

    auto document = ParseJson(boost::beast::buffers_to_string(res_.body(    ).data()).c_str());
    Traded_quantity = ParseDouble(std::string(document["resultingTrades"    ][0]["amount"].GetString()).c_str());

    buffer_.consume(buffer_.size());
    res_.body().consume(res_.body().size());
    std::cout << "end fok buy\n";
    return Traded_quantity;
    } catch (std::exception const& e) {
        std::cout << "reason " << res_.reason() << std::endl;
        std::cout << "status code" << res_.result() << std::endl;
    std::cout << boost::beast::buffers_to_string(res_.body().data()) <<     std::endl;
        std::cerr << "Error: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
}

这是我在主要功能中运行boost asio的方式

rest_manager -> Connect();
num_threads += 1;
for (int i = 0; i < num_threads -1; i++ ) // one is for rest manager
    std::make_shared<WebsocketMessageHandler<OrderBook>> (ioc,ctx) -> S    ubscribe(OnorderBook,symbols[i]);

for (int i = 0; i < num_threads; i++)
    threads.emplace_back([&ioc] { ioc.run(); });

已连接的rest_manager(http)这样发送http。

     auto first_executed_quantity = rest_manager -> SendFokBuyOrder(c    rypto_ptr -> usdt_alt_crypto_ -> symbol_,first_ask_record -> price_,alt_quantity
             );

这是我收到的错误消息

after write
Undefined error: 0

after read
bad status

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)