Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向ssl服务器发送GET命令以获取结果 我尝试向AccutsGooLeGo发送一个GET请求,以便能够为C++ OAuthe实现一个库来学习它。_C++_Ssl_Https_Http Headers_Boost Asio - Fatal编程技术网

向ssl服务器发送GET命令以获取结果 我尝试向AccutsGooLeGo发送一个GET请求,以便能够为C++ OAuthe实现一个库来学习它。

向ssl服务器发送GET命令以获取结果 我尝试向AccutsGooLeGo发送一个GET请求,以便能够为C++ OAuthe实现一个库来学习它。,c++,ssl,https,http-headers,boost-asio,C++,Ssl,Https,Http Headers,Boost Asio,我从这篇文章中获得以下代码:并将其修改为: int main() { try { std::string request = "/o/oauth2/v2/auth"; boost::system::error_code ec; using namespace boost::asio; // what we need io_service svc; ssl::con

我从这篇文章中获得以下代码:并将其修改为:

int main()
{
    try
    {
        std::string request = "/o/oauth2/v2/auth";
        boost::system::error_code ec;
        using namespace boost::asio;

        // what we need
        io_service svc;
        ssl::context ctx(svc, ssl::context::method::sslv23_client);
        ssl::stream<ip::tcp::socket> ssock(svc, ctx);
        ip::tcp::resolver resolver(svc);
        auto it = resolver.resolve({ "accounts.google.com", "443" }); // https://accouts.google.com:443
        boost::asio::connect(ssock.lowest_layer(), it);

        ssock.handshake(ssl::stream_base::handshake_type::client);

        // send request
        std::string fullResuest = "GET " + request + " HTTP/1.1\r\n\r\n";
        boost::asio::write(ssock, buffer(fullResuest));

        // read response
        std::string response;

        do
        {
            char buf[1024];
            size_t bytes_transferred = ssock.read_some(buffer(buf), ec);
            if (!ec) response.append(buf, buf + bytes_transferred);
            std::cout << "Response received: '" << response << "'\n"; // I add this to see what I am getting from the server, so it should not be here.
                        
        } while (!ec);

        // print and exit
        std::cout << "Response received: '" << response << "'\n";
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
        if (std::string const * extra = boost::get_error_info<my_tag_error_info>(e))
        {
            std::cout << *extra << std::endl;
        }
    }
    
}
我应该如何设置“获取推荐”,以便获得与浏览器相同的结果

2-应用程序挂起从服务器获取数据,显然以下循环不正确:

 do
 {
      char buf[1024];
      size_t bytes_transferred = ssock.read_some(buffer(buf), ec);
      if (!ec) response.append(buf, buf + bytes_transferred);
 } while (!ec);
从快速读取所有数据的web服务器读取响应的正确方法是什么

编辑1 基于已接受的答案,为了便于参考,我使用正确的GET标题修复了该问题,如下所示:

// send request
    std::string fullResuest = "GET " + request + " HTTP/1.1\r\n";
    fullResuest+= "Host: " + server + "\r\n";
    fullResuest += "Accept: */*\r\n";
    fullResuest += "Connection: close\r\n\r\n";
    boost::asio::write(ssock, buffer(fullResuest));

HTTP/1.1请求必须具有
Host
头。OpenSSL的一个简单实验将显示问题,即缺少标头:

$ openssl s_client -connect accounts.google.com:443
...
GET /o/oauth2/v2/auth HTTP/1.1

...   The requested URL <code>/o/oauth2/v2/auth</code> was not found on this server.  <ins>That’s all we know.</ins>
当添加
主机
标题时,我们会得到不同的响应:

$ openssl s_client -connect accounts.google.com:443
...
GET /o/oauth2/v2/auth HTTP/1.1
Host: accounts.google.com

... >Required parameter is missing: response_type<
$openssl s_客户端-连接帐户。google.com:443
...
GET/o/oauth2/v2/auth HTTP/1.1
主持人:accounts.google.com
... >缺少必需的参数:响应类型<
除此之外,HTTP/1.1隐式使用HTTP保持活动状态,即服务器和客户端可能在响应完成后保持连接打开。这意味着您在连接结束之前不应该阅读,而是应该正确解析HTTP头,提取
内容长度
头和/或
传输编码
头,并根据它们的值进行操作。或者,如果您希望更简单,可以使用HTTP/1.0


有关详细信息,请参阅。

HTTP/1.1请求必须具有请求明显缺失的
Host
头。@SteffenUllrich谢谢,我如何修复它?
$ openssl s_client -connect accounts.google.com:443
...
GET /o/oauth2/v2/auth HTTP/1.1
Host: accounts.google.com

... >Required parameter is missing: response_type<