J
J
Junior0072016-11-13 16:54:19
C++ / C#
Junior007, 2016-11-13 16:54:19

Why doesn't the web client connect to Https?

Mastering Boost::Asio.

I took a standard example from Boost, displays the contents of the site in the console, everything works, but if the site has Https, it throws a 302 error :(
I would be glad for any hints!

int main()
{
  setlocale(LC_ALL, "Russian");
  try
  {
    char* argv[3];

    argv[0] = "sync_client";
    argv[1] = "yandex.ru";
    argv[2] = "";

    boost::asio::io_service io_service;

    //Формирование конечной точки
    tcp::resolver resolver(io_service);
    tcp::resolver::query query(argv[1], "80");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

    //Создание сокета и подключение к адресу
    tcp::socket socket(io_service);
    boost::asio::connect(socket, endpoint_iterator);

    //Формирование заголовков запроса
    boost::asio::streambuf request;
    std::ostream request_stream(&request);
    request_stream << "GET " << argv[2] << " HTTP/1.1\r\n";
    request_stream << "Host: " << argv[1] << "\r\n";
    request_stream << "User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:49.0) Gecko/20100101 Firefox/49.0";
    request_stream << "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    request_stream << "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3";
    request_stream << "Accept-Encoding: gzip, deflate, br";
    request_stream << "Connection: keep-alive\r\n\r\n";

    //Сонхронная запись в поток
    boost::asio::write(socket, request);

    //Прочесть поток пока не будет встречен разделитель "\r\n"
    boost::asio::streambuf response;
    boost::asio::read_until(socket, response, "\r\n");

    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;
    }

    //Прочесть поток пока не будет встречен разделитель "\r\n\r\n"
    boost::asio::read_until(socket, response, "\r\n\r\n");

    //Вывести заголовки ответа сервера
    std::string header;
    while (std::getline(response_stream, header) && header != "\r")
      std::cout << header << "\n";

    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";
  }

  std::system("pause");

  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2016-11-13
@Junior007

This is not an error, it's just that the site is redirecting you to the HTTPS version. Some sites do not allow working over HTTP, forcibly enabling HTTPS in this way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question