微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决提升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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?