我有一个 C# 中的 get 请求,我需要将其转换为 C++ (boost) 在 C# 中获取请求:
using System.Text;
using Newtonsoft.Json;
class Program
{
private static readonly string key = "<your-translator-key>";
private static readonly string endpoint = "https://api.cognitive.microsofttranslator.com";
// location, also known as region.
// required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
private static readonly string location = "<YOUR-RESOURCE-LOCATION>";
static async Task Main(string[] args)
{
// Input and output languages are defined as parameters.
string route = "/translate?api-version=3.0&from=en&to=fr&to=zu";
string textToTranslate = "I would really like to drive your car around the block a few times!";
object[] body = new object[] { new { Text = textToTranslate } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// Build the request.
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(endpoint + route);
request.Content = new StringContent(requestBody, Encoding.UTF8 "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
// location required if you're using a multi-service or regional (not global) resource.
request.Headers.Add("Ocp-Apim-Subscription-Region", location);
// Send the request and get response.
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
// Read response as a string.
string result = await response.Content.ReadAsStringAsync();
Console.WriteLine(result);
}
}
}
在boost中获取请求:https://www.boost.org/doc/libs/1_67_0/libs/beast/doc/html/beast/quick_start.html
由于额外输入数据量巨大,不清楚如何使用它们
这应该是一个好的开始:
#include <boost/asio/ssl.hpp>
#include <boost/beast.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/json.hpp>
#include <iostream>
namespace net = boost::asio;
namespace beast = boost::beast;
namespace http = beast::http;
namespace ssl = net::ssl;
namespace json = boost::json;
using tcp = net::ip::tcp;
int main() {
std::string const my_translator_key = "YOUR_TRANSLATOR_KEY";
std::string const my_resource_location = "westus";
net::io_context ioc;
tcp::resolver resolver{ioc};
auto eps = resolver.resolve("api.cognitive.microsofttranslator.com", "https");
// Input and output languages are defined as parameters.
std::string route = "/translate?api-version=3.0&from=en&to=fr&to=zu";
std::string textToTranslate = "I would really like to drive your car around the block a few times!";
json::array body = {{{"Text", textToTranslate}}};
std::string requestBody = json::serialize(body);
http::request<http::string_body> request;
request.method(http::verb::post);
request.target(route);
request.set(http::field::content_type, "application/json");
request.set(http::field::user_agent, "Beast");
request.set(http::field::accept, "application/json");
request.set(http::field::host, "api.cognitive.microsofttranslator.com");
request.set(http::field::authorization, "Ocp-Apim-Subscription-Key: " + my_translator_key);
request.set(http::field::authorization, "Ocp-Apim-Subscription-Region: " + my_resource_location);
request.body() = requestBody;
request.prepare_payload();
tcp::socket s{ioc};
net::connect(s, eps);
ssl::context ctx{ssl::context::tlsv12_client};
boost::beast::ssl_stream<tcp::socket> stream{std::move(s), ctx};
stream.handshake(ssl::stream_base::client);
http::write(stream, request);
beast::flat_buffer buffer;
http::response<http::string_body> response;
http::read(stream, buffer, response);
std::cout << response.body() << std::endl;
}
显然没有端点密钥我无法测试它: