You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将C# Azure翻译API POST请求转换为Boost C++实现?

基于Boost Beast的Azure翻译API C++实现

以下是对应你提供的C#代码的C++实现,使用Boost Beast处理HTTP请求,nlohmann/json序列化请求体,支持HTTPS连接:

#include <boost/beast.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <nlohmann/json.hpp>
#include <iostream>
#include <string>

namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
namespace ssl = net::ssl;
using json = nlohmann::json;

int main() {
    // 替换为你的Azure翻译资源信息
    const std::string key = "<your-translator-key>";
    const std::string host = "api.cognitive.microsofttranslator.com";
    const std::string location = "<YOUR-RESOURCE-LOCATION>";
    const std::string target = "/translate?api-version=3.0&from=en&to=fr&to=zu";
    const std::string text_to_translate = "I would really like to drive your car around the block a few times!";

    // 构造JSON请求体,和C#逻辑一致
    json request_body_json = json::array();
    request_body_json.push_back({{"Text", text_to_translate}});
    const std::string request_body = request_body_json.dump();

    try {
        // 初始化IO上下文与SSL上下文(支持TLS 1.2)
        net::io_context ioc;
        ssl::context ctx(ssl::context::tlsv12_client);
        ctx.set_default_verify_paths();

        // 解析服务器地址
        net::ip::tcp::resolver resolver(ioc);
        auto const results = resolver.resolve(host, "https");

        // 创建并连接SSL流
        ssl::stream<net::ip::tcp::socket> stream(ioc, ctx);
        if(!SSL_set_tlsext_host_name(stream.native_handle(), host.c_str())) {
            beast::error_code ec{static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()};
            throw beast::system_error{ec};
        }
        net::connect(stream.next_layer(), results.begin(), results.end());
        stream.handshake(ssl::stream_base::client);

        // 构建POST请求
        http::request<http::string_body> req(http::verb::post, target, 11);
        req.set(http::field::host, host);
        req.set(http::field::content_type, "application/json");
        req.set("Ocp-Apim-Subscription-Key", key);
        req.set("Ocp-Apim-Subscription-Region", location);
        req.content_length(request_body.size());
        req.body() = request_body;
        req.prepare_payload();

        // 发送请求
        http::write(stream, req);

        // 接收并输出响应
        beast::flat_buffer buffer;
        http::response<http::string_body> res;
        http::read(stream, buffer, res);
        std::cout << res.body() << std::endl;

        // 关闭连接
        beast::error_code ec;
        stream.shutdown(ec);
        if(ec && ec != beast::errc::not_connected) {
            throw beast::system_error{ec};
        }
    } catch(const std::exception& e) {
        std::cerr << "请求失败: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

编译说明

  1. 依赖:Boost库(需包含Beast、Asio模块,建议版本1.70+)、nlohmann/json头文件
  2. GCC编译示例:
g++ -std=c++17 translator.cpp -o translator -lboost_system -lboost_thread -lssl -lcrypto

内容的提问来源于stack exchange,提问作者Kasharl1353

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 00:27:33