дак выкладывай
Да там все "абы хоть как-то работало".
networkmanager.h
C++ (Qt)
#ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H
#include <QtCore/QObject>
#include <auto_ptr.h>
class QNetworkRequest;
namespace Network
{
class NetworkManagerPrivate;
class NetworkManager : public QObject
{
Q_OBJECT
public:
explicit NetworkManager (QObject *parent = 0);
~NetworkManager ();
void post (const QNetworkRequest &request, const QByteArray &data);
Q_SIGNALS:
void uploadProgress (qint64 bytesSent, qint64 bytesTotal);
void finished (bool result);
private:
Q_DISABLE_COPY (NetworkManager);
private:
typedef std::auto_ptr <NetworkManagerPrivate> Impl;
Impl impl_;
};
}
#endif // NETWORKMANAGER_H
networkmanager_p.h
C++ (Qt)
#ifndef NETWORKMANAGER_P_H
#define NETWORKMANAGER_P_H
#include <QtCore/QObject>
#include "networkmanager.h"
class QTcpSocket;
class QTimer;
namespace Network
{
class NetworkManagerPrivate : public QObject
{
Q_OBJECT
public:
NetworkManagerPrivate ();
~NetworkManagerPrivate ();
void post (const QNetworkRequest &request, const QByteArray &data);
Q_SIGNALS:
void uploadProgress (qint64 bytesSent, qint64 bytesTotal);
void finished (bool result);
private Q_SLOTS:
void readyRead ();
void timeout ();
private:
Q_DISABLE_COPY (NetworkManagerPrivate);
private:
QTcpSocket *socket_;
QTimer *timer_;
};
}
#endif // NETWORKMANAGER_P_H
networkmanager.cpp
C++ (Qt)
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtCore/QTimer>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QTcpSocket>
#include "networkmanager_p.h"
const uint defaultTimeout = 10 * 1000;//10 seconds
namespace Network
{
NetworkManagerPrivate::NetworkManagerPrivate ()
: QObject (0)
{
socket_ = new QTcpSocket (this);
connect (socket_, SIGNAL (readyRead ()), SLOT (readyRead ()));
timer_ = new QTimer (this);
timer_->setSingleShot (true);
connect (timer_, SIGNAL (timeout ()), SLOT (timeout ()));
}
NetworkManagerPrivate::~NetworkManagerPrivate ()
{
socket_->close ();
}
void NetworkManagerPrivate::readyRead ()
{
static const QRegExp doneRegexp ("^HTTP\\/([0-9]\\.[0-9]) ([0-9]+) (OK|ERROR)\r\n$");
while (socket_->canReadLine ()) {
const QString line = socket_->readLine ();
if (doneRegexp.exactMatch (line)) {
timer_->stop ();
emit finished (doneRegexp.cap (3) == QLatin1String ("OK"));
}
}
}
void NetworkManagerPrivate::post (const QNetworkRequest &request, const QByteArray &data)
{
emit uploadProgress (0, 100);
socket_->disconnectFromHost ();
const QUrl url = request.url ();
socket_->connectToHost (url.host (), url.port ());
if (socket_->waitForConnected (defaultTimeout)) {
emit uploadProgress (30, 100);
QStringList rawRequest;
rawRequest.push_back ("POST / HTTP/1.0");
foreach (const QString & header, request.rawHeaderList ()) {
rawRequest.push_back (header + ": " + request.rawHeader (header.toUtf8 ()));
}
rawRequest.push_back (QString ("Content-Length: %1").arg (data.size ()));
rawRequest.push_back ("\r\n");
socket_->write (rawRequest.join ("\r\n").toUtf8 ());
socket_->write (data);
timer_->start (defaultTimeout);
emit uploadProgress (100, 100);
} else {
QTimer::singleShot (0, this, SLOT (timeout ()));
}
}
void NetworkManagerPrivate::timeout ()
{
emit finished (false);
}
NetworkManager::NetworkManager (QObject *parent)
: QObject (parent), impl_ (new NetworkManagerPrivate)
{
connect (impl_.get (), SIGNAL (uploadProgress (qint64, qint64)),
SIGNAL (uploadProgress (qint64, qint64)));
connect (impl_.get (), SIGNAL (finished (bool)), SIGNAL (finished (bool)));
}
NetworkManager::~NetworkManager ()
{
}
void NetworkManager::post (const QNetworkRequest &request, const QByteArray &data)
{
impl_->post (request, data);
}
}