Boost ASIO:someURL上的连接被拒绝';s P>我尝试使用Boost ASIO库从C++中发出HTTP请求,基于Boost网站上的例子:

Boost ASIO:someURL上的连接被拒绝';s P>我尝试使用Boost ASIO库从C++中发出HTTP请求,基于Boost网站上的例子:,c++,sockets,http,boost,stl,C++,Sockets,Http,Boost,Stl,该实现在一些URL示例上运行良好:www.amazon.com、www.cnn.com等。 另一方面,在某些URL上,请求被服务器拒绝 例如:www.stackoverflow.com、www.flipkart.com、www.soundcloud.com等 下面的代码创建一个新线程:httpRequest并调用一个函数任务,其参数为主机、路径、端口号 #include <iostream> #include <thread> #include <boost/asi

该实现在一些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();
}
另一方面,在某些URL上,请求被服务器拒绝,例如:

返回状态代码为301的响应。。。地点:

这不是拒绝,而是重定向,即您应该在给定的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请求,因此响应可以是分块传输编码的。这种编码需要特殊的处理,而您不需要这样做。请注意,您引用的示例代码也使用HTTP/1.0,因此不受此问题的影响

我真的建议您在尝试自己实现HTTP之前,先熟悉HTTP的基本工作原理

    // 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.