改用Poco请求。
This commit is contained in:
@@ -1,43 +1,94 @@
|
||||
#include "bf.request.h"
|
||||
|
||||
// Poco 头文件
|
||||
#include <Poco/Net/HTTPClientSession.h>
|
||||
#include <Poco/Net/HTTPRequest.h>
|
||||
#include <Poco/Net/HTTPResponse.h>
|
||||
#include <Poco/Net/HTTPSClientSession.h>
|
||||
#include <Poco/StreamCopier.h>
|
||||
#include <Poco/URI.h>
|
||||
|
||||
// SSL 相关头文件
|
||||
#include <Poco/Net/AcceptCertificateHandler.h>
|
||||
#include <Poco/Net/Context.h>
|
||||
#include <Poco/Net/InvalidCertificateHandler.h>
|
||||
#include <Poco/Net/SSLManager.h>
|
||||
#include <Poco/SharedPtr.h>
|
||||
#include <fmt/format.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <sstream>
|
||||
|
||||
#include "bf.util.h"
|
||||
|
||||
BF_Request::BF_Request()
|
||||
{
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
curl_ = curl_easy_init();
|
||||
}
|
||||
|
||||
BF_Request::~BF_Request()
|
||||
{
|
||||
curl_easy_cleanup(curl_);
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
std::string BF_Request::getResult(const std::string& url)
|
||||
{
|
||||
std::string reponse;
|
||||
if (!curl_) {
|
||||
gLogger->error("curl init failed.");
|
||||
return reponse;
|
||||
std::string response;
|
||||
|
||||
try {
|
||||
Poco::URI uri(url);
|
||||
|
||||
// 禁用SSL验证(模拟curl的SSL_VERIFYHOST=0, SSL_VERIFYPEER=0)
|
||||
Poco::Net::initializeSSL();
|
||||
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> pCertHandler = new Poco::Net::AcceptCertificateHandler(false);
|
||||
Poco::Net::Context::Ptr pContext =
|
||||
new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_NONE, 9, true);
|
||||
Poco::Net::SSLManager::instance().initializeClient(0, pCertHandler, pContext);
|
||||
|
||||
std::unique_ptr<Poco::Net::HTTPClientSession> pSession;
|
||||
|
||||
if (uri.getScheme() == "https") {
|
||||
// HTTPS
|
||||
pSession = std::make_unique<Poco::Net::HTTPSClientSession>(uri.getHost(), uri.getPort(), pContext);
|
||||
} else {
|
||||
// HTTP
|
||||
pSession = std::make_unique<Poco::Net::HTTPClientSession>(uri.getHost(), uri.getPort());
|
||||
}
|
||||
|
||||
std::string path(uri.getPathAndQuery());
|
||||
if (path.empty())
|
||||
path = "/";
|
||||
|
||||
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
|
||||
|
||||
// 添加必要的请求头
|
||||
request.set("User-Agent", "Mozilla/5.0 (compatible; baidu_fanyi/1.0)");
|
||||
request.set("Accept", "*/*");
|
||||
request.set("Connection", "close");
|
||||
|
||||
pSession->setTimeout(Poco::Timespan(10, 0)); // 10秒超时
|
||||
|
||||
pSession->sendRequest(request);
|
||||
|
||||
Poco::Net::HTTPResponse httpResponse;
|
||||
std::istream& rs = pSession->receiveResponse(httpResponse);
|
||||
|
||||
if (httpResponse.getStatus() != Poco::Net::HTTPResponse::HTTP_OK) {
|
||||
gLogger->error("HTTP request failed. Status: {} - {}", static_cast<int>(httpResponse.getStatus()),
|
||||
httpResponse.getReason());
|
||||
return response;
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
Poco::StreamCopier::copyStream(rs, ss);
|
||||
response = ss.str();
|
||||
|
||||
} catch (const Poco::Exception& e) {
|
||||
gLogger->error("Poco request failed. Error: {}", e.displayText());
|
||||
} catch (const std::exception& e) {
|
||||
gLogger->error("Request failed. Error: {}", e.what());
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl_, CURLOPT_POST, 0L);
|
||||
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, writeData);
|
||||
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &reponse);
|
||||
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
Poco::Net::uninitializeSSL(); // 清理SSL
|
||||
|
||||
CURLcode res = curl_easy_perform(curl_);
|
||||
if (res != CURLE_OK) {
|
||||
gLogger->error("curl request failed. error code: {}", static_cast<int>(res));
|
||||
return reponse;
|
||||
}
|
||||
return reponse;
|
||||
return response;
|
||||
}
|
||||
|
||||
std::string BF_Request::doReady(const std::string& words, const std::string& from, const std::string& to)
|
||||
|
||||
Reference in New Issue
Block a user