C++ VK API通过C&x2B+;促进

C++ VK API通过C&x2B+;促进,c++,boost,vk,C++,Boost,Vk,如何打开连接到VKAPI的套接字,此代码与其他资源配合使用效果良好,但提供了APPCRASH和API.VK.com。在浏览器中,它与http一起工作,因此它应该在这里工作,问题不在“http”中,还是我错了?救命啊 另外,我是Boost和VKAPI的新手,所以如果可以的话,请详细解释一下,谢谢 int main() { boost::asio::io_service io_service; // Get a list of endpoints corresponding to

如何打开连接到VKAPI的套接字,此代码与其他资源配合使用效果良好,但提供了
APPCRASH
API.VK.com
。在浏览器中,它与
http
一起工作,因此它应该在这里工作,问题不在“http”中,还是我错了?救命啊

另外,我是Boost和VKAPI的新手,所以如果可以的话,请详细解释一下,谢谢

int main()
{
    boost::asio::io_service io_service;

    // Get a list of endpoints corresponding to the server name.
    tcp::resolver resolver(io_service);
    tcp::resolver::query query("api.vk.com", "http");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

    // Try each endpoint until we successfully establish a connection.
    tcp::socket socket(io_service);
    boost::system::error_code error = boost::asio::error::host_not_found;
    socket.connect(*endpoint_iterator, error);
    return 0;
}

您可能会在Windows事件日志中看到
APPCRASH

因此,我假设您可能正在windows服务上下文中运行此代码

默认情况下,Windows服务没有网络访问权限

这意味着DNS查找可能会失败。您会得到一个异常,例如
解析:找不到主机(权威)
。当我故意将域名更改为不存在的TLD时,Linux控制台中会发生这种情况:

$ ./test
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
  what():  resolve: Host not found (authoritative)
Aborted (core dumped)

我只是简单地更改了我的DNS服务器,它就工作了:
成功连接到87.240.131.119:80

我想
APPCRASH
是一种解释,请提供事实。另外,您知道并不是所有迭代器都是可取消引用的,对吧?在我的系统上提供了带有错误处理和报告的固定示例:
跳过[2a00:bdc0:3:103:1:0:403:902]:80-未连接
跳过[2a00:bdc0:3:103:1:0:403:901]:80-未连接
跳过[2a00:bdc0:3:103:0:900]:80-未连接
跳过87.240.131.119:80-未连接
跳过87.240.131.120:80-未连接
跳过87.240.131.118:80-未连接我要检查防火墙。如果浏览器正常工作,它很可能通过代理。考虑使用LIbCURL或SO来获得代理支持。
#include <boost/asio.hpp>
#include <iostream>

using boost::asio::ip::tcp;

int main()
{
    boost::asio::io_service io_service;
    boost::system::error_code error = boost::asio::error::host_not_found;

    // Get a list of endpoints corresponding to the server name.
    tcp::resolver resolver(io_service);
    tcp::resolver::query query("api.vk.com", "http");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error), last;

    if (!error) {

        // Try each endpoint until we successfully establish a connection.
        tcp::socket socket(io_service);

        for (;endpoint_iterator != last; ++endpoint_iterator) {
            socket.connect(*endpoint_iterator, error);
            if (!error) {
                std::cout << "Successfully connected to " << endpoint_iterator->endpoint() << "\n";
                break; // found working endpoint
            } else {
                std::cout << "Skipped " << endpoint_iterator->endpoint() << " - not connecting\n";
            }
        }
        return 0;
    } else {
        std::cout << error.message() << "\n";
        return 255;
    }
}
Successfully connected to 87.240.131.97:80