【发布时间】:2015-08-14 11:15:24
【问题描述】:
我想使用 Poco (1.6.0) 同时向同一个端点发送一些 https 请求,但我所做的代码经常出现异常。 (但是,使用 HTTP 请求的相同代码可以完美运行......)
这是我使用的代码:
Poco::Net::initializeSSL();
try {
Poco::URI ep("https://stackoverflow.com/");
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrHandler
= new Poco::Net::AcceptCertificateHandler(false);
Poco::Net::Context::Ptr context = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE,
"", "", "", Poco::Net::Context::VERIFY_STRICT,
9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Poco::Net::SSLManager::instance().initializeClient(0, ptrHandler, context);
for (int i = 0; i < 20; ++i)
{
// Start an asynchronous HTTPS request
std::future<bool> f = std::async(std::launch::async, []() -> bool {
try {
Poco::URI ep("https://github.com/");
std::unique_ptr<Poco::Net::HTTPSClientSession> session(
new Poco::Net::HTTPSClientSession (ep.getHost(), ep.getPort()) );
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET,
ep.getPathAndQuery() ,
Poco::Net::HTTPMessage::HTTP_1_1);
request.write(std::cerr);
try {
session->sendRequest(request);
}
catch (const Poco::Exception& e) {
std::cerr << "Error A" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
Poco::Net::HTTPResponse response;
try {
session->receiveResponse(response);
}
catch (const Poco::Exception& e) {
std::cerr << "Error B" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
response.write(std::cerr);
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cerr << "Async request ended" << std::endl;
}
catch (const Poco::Exception& e) {
std::cerr << "Error :-/" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
std::cerr << "Async request ended 2" << std::endl;
return true;
});
// Generate several HTTPS requests until the async request is completed
std::chrono::microseconds span (10);
while (f.wait_for(span) != std::future_status::ready)
{
std::unique_ptr<Poco::Net::HTTPSClientSession> session(
new Poco::Net::HTTPSClientSession (ep.getHost(), ep.getPort()) );
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET,
ep.getPathAndQuery() ,
Poco::Net::HTTPMessage::HTTP_1_1);
request.write(std::cerr);
try {
session->sendRequest(request);
}
catch (const Poco::Exception& e) {
std::cerr << "Error 1" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
Poco::Net::HTTPResponse response;
try {
session->receiveResponse(response);
}
catch (const Poco::Exception& e) {
std::cerr << "Error 2" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
response.write(std::cerr);
}
f.get();
}
}
catch (const Poco::Exception& e) {
std::cerr << "ERROR :-(" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
}
Poco::Net::uninitializeSSL();
这是我遇到的问题:
当我在 QtCreator 调试器中运行代码时,我得到如下:一旦异步请求结束,主线程中的当前请求抛出 Poco 异常。
Error 1
4 - I/O error: Interrupted
但是,这似乎没有出现在 QtCreator 调试器之外。这有点奇怪。
这可能是并发问题,但我找不到我做错了什么,我在互联网上也没有看到任何类似的问题。 你能帮我找出我的错误吗?
谢谢。
编辑:简化了最少的代码并在问题描述中添加了一些细节。
EDIT2:我似乎忘了提及最重要的信息,即我遇到问题的环境。如下: Ubuntu 14.04 LTS 64bit with gcc 4.8.4, gdb 7.7.1, openssl 1.0.1f
【问题讨论】:
-
你有没有得到这个工作?我正在努力解决类似的问题,但对我来说,无论我是否正在调试它都会崩溃
-
好吧,对我来说唯一的问题是 GDB。升级它解决了这个问题。如果您使用它,也许检查您的 gdb 版本,并在必要时升级它。但在你的情况下,它可能无法解决所有问题,如果不是,我不知道发生了什么。
标签: c++ multithreading c++11 poco poco-libraries