Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:44:27+00:00 2026-05-26T20:44:27+00:00

I am trying to connect to a website through a proxy. I implemented a

  • 0

I am trying to connect to a website through a proxy. I implemented a QTcpServer and a QTcpSocket.
The server passes the connection to the socket.
It works well, but for some sites, expecially for those which have dynamically created javascript, the socket stucks at some point and nothing is shown in the navigator.
Attach the code, hope clear.

            #include "webproxy.h"
            #include <QtNetwork>
            #include <QMessageBox>
            #include <QtGui>
            #include <QHash>


            WebProxy::WebProxy(QObject *parent,int port): QObject(parent)

                {
                    qDebug()<<" Listen...";
                    authMethod = "";
                    QTcpServer *proxyServer = new QTcpServer(this);
                    if (!proxyServer->listen(QHostAddress::Any, port)) {
                       emit error(1);

                        return;
                    }

                    connect(proxyServer, SIGNAL(newConnection()), this, SLOT(manageQuery()));
                    qDebug() << "Proxy server running at port" << proxyServer->serverPort();






            void WebProxy::manageQuery() {
                    QTcpServer *proxyServer = qobject_cast<QTcpServer*>(sender());
                    QTcpSocket *socket = proxyServer->nextPendingConnection();
                    connect(socket, SIGNAL(readyRead()), this, SLOT(processQuery()));
                    connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));

                    qDebug()<<"New connection started..."<<socket->peerAddress();
                }
            QUrl WebProxy::getUrl(QList<QByteArray > &entries)
               {

                   QByteArray method = entries.value(0);
                   QByteArray address = entries.value(1);
                   QByteArray version = entries.value(2);

                   qDebug()<<method;
                   qDebug()<<address;
                   qDebug()<<version;
                   QUrl url = QUrl::fromEncoded(address);
                   if (!url.isValid()) {

                       qWarning() << "Invalid URL:" << url;

                       return QString();
                   }


                   return url;
               }

              void WebProxy::processQuery() {

                    int wSize = 0;
                    QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
                    QByteArray requestData = socket->readAll();


             qDebug()<<"Request "<<requestData;

                    int pos = requestData.indexOf("\r\n");


                    QByteArray requestLine = requestData.left(pos);
                    requestData.remove(0, pos + 2);

                    QList<QByteArray> entries = requestLine.split(' ');
                    QByteArray method         = entries.value(0);
                    QByteArray address        = entries.value(1);
                    QByteArray version        = entries.value(2);

                    QByteArray auth;
                    QByteArray authMethod;

                    QUrl url = QUrl::fromEncoded(address);
                    if (!url.isValid()) {
                        qWarning() << "Invalid URL:" << url;
                        socket->disconnectFromHost();
                        return;
                    }

                    QString host              = url.host();


                    int port = (url.port() <= 0) ? 80 : url.port();
                    QByteArray req = url.encodedPath();
                    if (url.hasQuery())
                        req.append('?').append(url.encodedQuery());


                    requestLine = method + " " + req + " " + version + "\r\n";
                    if (!authMethod.isEmpty())
                    {
                         requestLine.append(requestLine);
                         requestLine.append(authMethod);
                         requestLine.append("\r\n");
                     }

                    QString key = host + ':' + QString::number(port);
                    QTcpSocket *proxySocket = socket->findChild<QTcpSocket*>(key);

                    if (proxySocket) {
                        proxySocket->setObjectName(key);
                        proxySocket->setProperty("url", url);
                        proxySocket->setProperty("requestData", requestData);
                        wSize = proxySocket->write(requestData);


                    } else {
                        proxySocket = new QTcpSocket(socket);
                        proxySocket->setObjectName(key);
                        proxySocket->setProperty("url", url);
                        proxySocket->setProperty("requestData", requestData);
                        connect(proxySocket, SIGNAL(connected()), this, SLOT(sendRequest()));
                        connect(proxySocket, SIGNAL(readyRead()), this, SLOT(transferData()));
                        connect(proxySocket, SIGNAL(disconnected()), this, SLOT(closeConnection()));
                        connect(proxySocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(closeConnection()));
                        proxySocket->connectToHost(host, port);
                    }
                }

            void WebProxy::sendRequest() {
                    QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());
                    QByteArray requestData = proxySocket->property("requestData").toByteArray();
                    int wSize = 0;
                    wSize     = proxySocket->write(requestData);

                }

            void WebProxy::transferData() {


                    QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());

                    QByteArray data = proxySocket->readAll();

            qDebug()<<"READ TRANSFER SIZE..."<<data.size();

                    QString host = proxySocket->peerAddress().toString();
                    QByteArray    filtered(data);

                    QTcpSocket *socket  = qobject_cast<QTcpSocket*>(proxySocket->parent());
                    int wSize           = 0;
                    if (!data.trimmed().isEmpty())
                    {
                        wSize = socket->write(filtered);
                        if (wSize==-1)
                             qDebug()<<"WP error";
                        else
                             qDebug()<<"TRANSFER WRITE SIZE = "<<wSize<<" READ SIZE"<<filtered.size();

                    }

                }

            void WebProxy::closeConnection() {

                    QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());
                    if (proxySocket) {
                        QTcpSocket *socket = qobject_cast<QTcpSocket*>(proxySocket->parent());
                        if (socket)
                            socket->disconnectFromHost();
                        if (proxySocket->error() != QTcpSocket::RemoteHostClosedError)
                            qWarning() << "Error for:" << proxySocket->property("url").toUrl()
                                    << proxySocket->errorString();
                        proxySocket->deleteLater();;
                    }
                }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:44:28+00:00Added an answer on May 26, 2026 at 8:44 pm

    You may want to use QTcpServer in a multi-threaded way.

    Subclass QTcpServer, overload QTcpServer::incomingConnection(int), create your QThread derived handler (described next) and start it with QThread::start

    Subclass QThread, make the constructor accept an int (the socket descriptor), overload QThread::run(). In the run function, create QTcpSocket, call QAbstractSocket::setSocketDescriptor to initialise the socket, connect up the socket slots and call QThread::exec() to start the thread event loop.

    Make sure you create the socket in the run of QThread, not the constructor so the socket is associated with that thread.

    For more details, look at the Threaded Fortune Server Example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to connect to server using a Java socket. I am trying
I am trying to connect from an iphone app to a java server but
So I am trying to connect to my company's website through an HTTP authentication
While trying to connect to a database that's set to read only, the connection
I am trying to connect to a Microsoft SQL 2005 server which is not
I'm having problems trying to connect my website to Facebook. What I would like
I'm trying to connect to a website I made with auth that uses MD5.hex(password)
I'm trying to implement Facebook Connect on a website with .NET MVC using C#.
I'm trying to get an ASP.NET website running on Vista (IIS7), using SQL Server
I am trying to deploy a winforms app through ClickOnce on a website. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.