【问题标题】:boost::asio return http 301 error code while no error with fiddlerboost::asio 返回 http 301 错误代码,而提琴手没有错误
【发布时间】:2013-12-01 15:01:22
【问题描述】:

我想阅读带有 boost::asio 的 thml 页面的源代码。

这是我的示例代码:

#include "stdafx.h"
#include <boost/algorithm/string/replace.hpp>
#include <boost/asio.hpp>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <ostream>


int _tmain(int argc, _TCHAR* argv[])
{
    std::string strHost = "www.fdj.fr";
    std::string strPort = "80";
    std::string strUrlPath = "https://www.fdj.fr/jeux/jeux-de-tirage/keno/resultats";
    std::string strUserAgent = "Fiddler";
    //std::vector<std::string> header;
    unsigned int TimeOut = 5000;
    //do_get(strHost, strPort, strUrlPath, header, TimeOut);
    try
    {
        using boost::asio::ip::tcp;
        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(strHost, "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 " << strUrlPath << " HTTP/1.0\r\n";
        request_stream << "User-Agent: " << strUserAgent << "\r\n";
        request_stream << "Host: " << strHost << "\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)
            std::cout << &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);
        char a;
        std::cin>>a ;
    }
    catch (std::exception& e)
    {
        std::cout << "Exception: " << e.what() << "\n";
    }

    return 0;
}

发送的请求是:

GET https://www.fdj.fr/jeux/jeux-de-tirage/keno/resultats HTTP/1.0
User-Agent: Fiddler
Host: www.fdj.fr
Accept: */*
Connection: close

如果我运行我的程序,我有:

如果我在提琴手中运行此请求(通过复制/粘贴),我有:

return code=200

(已接受)并且页面已加载!

有人知道我为什么会出现这个 301 错误吗? 我认为这是由于 boost::asio 参数,但是哪个?

非常感谢,

最好的问候,

尼克斯

【问题讨论】:

  • 请删除此帖并与您之前的帖合并!
  • 问题不一样,我的 Fiddler/我的应用程序有不同的答案!标题不一样。

标签: c++ sockets boost websocket boost-asio


【解决方案1】:

这里发生了两件事:

  1. 您正在尝试通过普通 HTTP 加载 HTTPS URI。这会导致 301 重定向,通过 HTTPS 将您指向原始页面。这表明托管服务器不通过不安全的 HTTP 提供此资源。

  2. 您的 HTTP 实现不支持重定向。您正在发出请求并提供第一个响应。完整的 HTTP 客户端将检查 301 响应并使用 301 的位置标头中的 URI 重试。

要解决此问题,您需要实现对 TLS/HTTPS 以及理想情况下的 HTTP 重定向的支持。

【讨论】:

  • 非常感谢!我使用 Wininet Api 解决了这个问题!是的,它不是跨平台的,但是,它解决了我的问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
相关资源
最近更新 更多