gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:24:22 25 4
gpt4 key购买 nike

我正在尝试根据 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com