c++ - boost ASIO : connection rejected on someURL's

标签 c++ sockets http boost stl

我正在尝试根据 boost 网站上给出的示例使用 boost asio 库从 c++ 发出 http 请求:http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/example/http/client/sync_client.cpp

该实现在某些 URL 示例上运行良好:www.amazon.com、www.cnn.com 等。 另一方面,在某些 URL 上请求被服务器拒绝 例如:www.stackoverflow.com、www.flipkart.com、www.soundcloud.com 等。

下面的代码创建了一个新线程:httpRequest 并调用了一个参数为主机、路径、端口号的函数任务

#include <iostream>
#include <thread>
#include <boost/asio.hpp>

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

void createRequest(std::string host,std::string path,std::string port) {
    try { 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(host, port);
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;

        // Try each endpoint until we successfully establish a connection.
        tcp::socket socket(io_service);
        boost::system::error_code error1 = boost::asio::error::host_not_found;
        while (error1 && endpoint_iterator != end)
        {
            socket.close();
            socket.connect(*endpoint_iterator++, error1);
        }
        if (error1)
            throw boost::system::system_error(error1);


        // Form the request. We specify the "Connection: close" header so that the
        // server will close the socket after transmitting the response. This will
        // allow us to treat all data up until the EOF as the content.
        boost::asio::streambuf request;
        std::ostream request_stream(&request);
        request_stream << "GET " << path <<  " HTTP/1.1\r\n";
        request_stream << "Host: " << host << "\r\n";
        request_stream << "Accept: */*\r\n";
        request_stream << "Connection: close\r\n\r\n";

        // Send the request.
        boost::asio::write(socket, request);
        //std::cout << typeid(socket).name() << std::endl;

        // Read the response status line. The response streambuf will automatically
        // grow to accommodate the entire line. The growth may be limited by passing
        // a maximum size to the streambuf constructor.
        boost::asio::streambuf response;
        boost::asio::read_until(socket, response, "\r\n");

        // Check that response is OK.
        std::istream response_stream(&response);
        std::string http_version;
        response_stream >> http_version;
        unsigned int status_code;
        response_stream >> status_code;
        std::string status_message;
        std::getline(response_stream, status_message);
        if (!response_stream || http_version.substr(0, 5) != "HTTP/") {
            std::cout << "Invalid response\n";;
        }
        if (status_code != 200) {
            std::cout << "Response returned with status code " << status_code << "\n";
        }

        // Read the response headers, which are terminated by a blank line.
        boost::asio::read_until(socket, response, "\r\n\r\n");

        // Process the response headers.
        std::string header;
        while (std::getline(response_stream, header) && header != "\r")
            std::cout << header << "\n";
        std::cout << "\n";

        // Write whatever content we already have to output.
        if (response.size() > 0)
            std::cout << &response;

        // Read until EOF, writing data to output as we go.
        boost::system::error_code error;
        while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
            std::cout << &response;
        if (error != boost::asio::error::eof)
            throw boost::system::system_error(error);
    }
    catch (std::exception& e) {
        std::cout << "Exception: " << e.what() << "\n";
    }
}

int main() {
    std::thread httpThred(createRequest, "www.stackoverflow.com","/","80");
    std::cout << "async task launched\n";
    std::cout << "main done\n";
    httpThred.join();
}

输出:

Response returned with status code 301
Content-Type: text/html; charset=UTF-8
Location: http://stackoverflow.com/
Date: Sun, 05 Jul 2015 19:50:44 GMT
Connection: close
Content-Length: 148

最佳答案

On the other hand on some URL's the request is beingg rejected from the server eg: ...

Response returned with status code 301 ... Location: https://stackoverflow.com/

这不是拒绝而是重定向,即您应该在给定的 URI 请求资源。

    // Form the request. We specify the "Connection: close" header so that the
    // server will close the socket after transmitting the response. This will
    // allow us to treat all data up until the EOF as the content.

不是真的。您正在发出 HTTP/1.1 请求,因此响应可以使用 Transfer-Encoding 分块。这种编码需要您不需要的特殊处理。请注意,您引用的示例代码也使用 HTTP/1.0,因此它不受此问题的影响。

我强烈建议您先熟悉 HTTP 的工作原理,然后再尝试自己实现它。

关于c++ - boost ASIO : connection rejected on someURL's,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31234386/

相关文章:

java - java中的指针?

c++ - "Property"类似的方法而不是 C++ 中的 getter 和 setter?

sockets - TCP:是否可以绑定(bind)一个套接字然后/两者/从它连接并接受它(客户端和服务器规则)?

python - 如何使用 openCV 和 python 从流中逐帧获取视频

C++ 模板元编程 : Inheritance from template template parameter

c++ - 在未解析的外部 dll 中导出模板类

c# - 可扩展的套接字事件队列处理

python - Bokeh 服务 HTTPS 而不是 HTTP

java - 如何在 Camel HTTP 代理中获取和设置参数

http - Cookie 将安全标志设置为 true