使用 Boost [C++] 将 http 文件内容读取到字符串

Posted

技术标签:

【中文标题】使用 Boost [C++] 将 http 文件内容读取到字符串【英文标题】:Read http file content to string with Boost [C++] 【发布时间】:2014-12-15 19:07:33 【问题描述】:

我必须读取文件的内容(在网络服务器中)以在 Visual Studio C++ 中进行字符串处理。我有一个在一个网络服务器上运行良好的代码。但是如果我使用另一个网络服务器,我的程序只会读取前 200 个字符。除了网络服务器,一切都一样。

这是工作网址:http://www.fxcoder.hu/fxc_esopus/clients.dat

这是最大值。 200 个字符的网址:http://www.forexhelpmate.com/app/webroot/forex/clients.dat

clients.dat 文件在每台服务器上也是相同的。

我的代码:

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::ostringstream ostringstream_content;
int GetPageContent(char* argv[])
    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(argv[0], "http");
        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 error = boost::asio::error::host_not_found;
        while (error && endpoint_iterator != end)
            socket.close();
            socket.connect(*endpoint_iterator++, error);
        
        if (error) throw boost::system::system_error(error);
        // 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 " << argv[1] << " HTTP/1.0\r\n";
        request_stream << "Host: " << argv[0] << "\r\n";
        request_stream << "Accept: */*\r\n";
        request_stream << "Connection: close\r\n\r\n";
        // Send the request.
        boost::asio::write(socket, request);
        // Read the response status line.
        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";
            return 1;
        
        if (status_code != 200)
            std::cout << "Response returned with status code " << status_code << "\n";
            return 1;
        
        // 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)
            ostringstream_content << &response;
        
        // Read until EOF, writing data to output as we go.
        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";
    
    return 0;

两个 Web 服务器之间的哪些差异可能导致此问题?

为什么第二台服务器只能读取前 200 个字符?

提前谢谢你。

亲戚


匈牙利、欧盟

【问题讨论】:

文件相似,不一样。 【参考方案1】:

您将第一部分流式传输到ostringstream_content,将其余部分流式传输到std::cout

以下为我正确下载了这两个文件

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;

int GetPageContent(char const *argv[]) 
    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(argv[0], "http");
        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 error = boost::asio::error::host_not_found;
        while (error && endpoint_iterator != end) 
            socket.close();
            socket.connect(*endpoint_iterator++, error);
        
        if (error)
            throw boost::system::system_error(error);
        // 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 " << argv[1] << " HTTP/1.0\r\n";
        request_stream << "Host: " << argv[0] << "\r\n";
        request_stream << "Accept: */*\r\n";
        request_stream << "Connection: close\r\n\r\n";
        // Send the request.
        boost::asio::write(socket, request);
        // Read the response status line.
        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::cerr << "Invalid response\n";
            return 1;
        
        if (status_code != 200) 
            std::cerr << "Response returned with status code " << status_code << "\n";
            return 1;
        

        // 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::cerr << header << "\n";

        std::cerr << "\n";
        std::cerr << "Writing content data\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.
        while (true)  
            size_t n = boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error);

            if (!error)
            
                if (n)
                    std::cout << &response;
            

            if (error == boost::asio::error::eof)
                break;

            if (error)
                throw boost::system::system_error(error);
        
    
    catch (std::exception &e) 
        std::cerr << "Exception: " << e.what() << "\n";
    

    std::cerr << "Done\n";
    return 0;


int main()
    char const* argv[] =  "www.fxcoder.hu", "/fxc_esopus/clients.dat" ;
    //char const* argv[] =  "www.forexhelpmate.com", "/app/webroot/forex/clients.dat" ;

    return GetPageContent(argv);

【讨论】:

太棒了!非常感谢!

以上是关于使用 Boost [C++] 将 http 文件内容读取到字符串的主要内容,如果未能解决你的问题,请参考以下文章

在 c++ 中将值转换为范围内的值,使用 boost 或 std 进行优化

在 C++ 中使用 boost 与可执行文件交互

Boost入门

C++ 使用boost库实现http client get操作

C++ 使用boost库实现http client get操作

如何在Visual Studio 2010中使用C++“准”标准库 Boost 1.44.0