Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Too many "HTTP 400 – Bad Request"
Qt 6.11 is out! See what's new in the release blog

Too many "HTTP 400 – Bad Request"

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 5.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MonoSlon
    wrote on last edited by
    #1

    Hello,

    Our team is developing application using Qt 4.8.2. This application periodically makes many http and https requests to our servers and we found that there are a lots of "400" status codes in the log-files of our web servers (nginx). There are much more such bad requests than "good" requests.

    We've found that our app often opens "empty" connection, i mean that it establishes tcp connection with web server and do not send anything to this socket.

    Is it a bug or is it a normal behavior and how can it be handled?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MonoSlon
      wrote on last edited by
      #2

      here is a code of test app that reproduce this issue.

      file main.cpp:
      [code]
      #include <QCoreApplication>
      #include <QTimer>
      #include <Sample.h>
      #include <Windows.h>

      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);

      Sample obj;
      

      // QTimer::singleShot(10000, &obj, SLOT(onTimer()));

      for (int i = 0; i < 3; i++) {
          obj.makeRequest();
      }
      
      while(true) {
          Sleep(1000);
          obj.makeRequest();
      }
      
      int ret = a.exec&#40;&#41;;
      return ret;
      

      }
      [/code]

      file sample.h
      [code]
      #ifndef SAMPLE_H
      #define SAMPLE_H

      #include <QObject>
      #include <QList>
      #include <QtNetwork/QNetworkAccessManager>
      #include <QtNetwork/QNetworkReply>
      #include <QtNetwork/QSslError>

      class Sample : public QObject
      {
      Q_OBJECT
      public:
      explicit Sample(QObject *parent = 0);

      void startTest(&#41;;
      void makeRequest();
      

      signals:

      public slots:
      void sslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
      void onTimer();
      void onRequestFinish();
      void onError(QNetworkReply::NetworkError error);

      private:
      QNetworkAccessManager *_manager;

      };

      #endif // SAMPLE_H

      [/code]

      file sample.cpp
      [code]
      #include <Sample.h>

      #include <QCoreApplication>
      #include <QDebug>
      #include <QUrl>
      #include <QtNetwork/QNetworkRequest>

      Sample::Sample(QObject parent)
      : QObject(parent), _manager(new QNetworkAccessManager(this))
      {
      connect(this->_manager, SIGNAL(sslErrors(QNetworkReply
      , const QList<QSslError>&)),
      this, SLOT(sslErrors(QNetworkReply *, const QList<QSslError>&)));
      }

      void Sample::startTest()
      {
      makeRequest();
      }
      [code]
      void Sample::makeRequest()
      {
      QUrl request("https://localhost:8443/restapi?method=games.getMaintenance");

      QNetworkReply *reply = (1 == 1)
          ? this->_manager->get(QNetworkRequest(request))
          : this->_manager->post(QNetworkRequest(request), request.encodedQuery());
      
      connect(reply, SIGNAL(finished()), this, SLOT(onRequestFinish()));
      connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
      
      qDebug() << "makeRequest" << request;
      

      }

      void Sample::onRequestFinish()
      {
      qDebug() << "onRequestFinish";

      QNetworkReply *reply = qobject_cast<QNetworkReply*>(QObject::sender());
      reply->deleteLater();
      
      QString response = QString::fromUtf8(reply->readAll());
      QNetworkReply::NetworkError error = reply->error();
      
      qDebug() << response << error;
      

      }

      void Sample::onError(QNetworkReply::NetworkError error)
      {
      qDebug() << "error" << error;
      }

      void Sample::sslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
      {
      qDebug() << "sslErrors";
      Q_FOREACH(QSslError error, errors) {
      qDebug() << error;
      }

      reply->ignoreSslErrors();
      

      }

      void Sample::onTimer()
      {
      QCoreApplication::quit();
      }

      [/code]

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AcerExtensa
        wrote on last edited by
        #3

        Please use "code" tags around your code to make it readable...

        God is Real unless explicitly declared as Integer.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MonoSlon
          wrote on last edited by
          #4

          I'm sorry. Updated.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MonoSlon
            wrote on last edited by
            #5

            Found this:
            [url]https://bugreports.qt-project.org/browse/QTBUG-14783?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel[/url]

            Is there a way to force desktop application not to open additional parallel channels (tcp connections) if it is not really necessary? Especially for the SSL.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              misterion
              wrote on last edited by
              #6

              Up. We have same issue as MonoSlon :(

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tzander
                wrote on last edited by
                #7

                I noticed one big problem; you schedule all your requests in main. Before you start the event-loop (the exec line).

                Please try to restructure your code to
                a) avoid a sleep() call. Really, you can't have Sleep() in your code.
                b) make the requests being called be called in the event-loop. I suggest using QTimer::singleShot()

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MonoSlon
                  wrote on last edited by
                  #8

                  I tried to change the code this way:
                  [code]
                  ...
                  QTimer::singleShot(3000, &obj, SLOT(startTest()));
                  int ret = a.exec();
                  ...
                  [/code]
                  but it didn't make changes.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MonoSlon
                    wrote on last edited by
                    #9

                    Sample::startTest was changed respectively:
                    [code]
                    void Sample::startTest()
                    {
                    for (int i = 0; i < 3; i++) {
                    makeRequest();
                    }
                    }
                    [/code]

                    this opens 6 channels, only 3 are used.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      radzaw
                      wrote on last edited by
                      #10

                      We are having the same problem. Manyyy connections without any content to our API server :/

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved