📄 qhttp.cpp
字号:
\sa get() post() request() readyRead() bytesAvailable() readAll()*/qint64 QHttp::read(char *data, qint64 maxlen){ Q_D(QHttp); if (data == 0 && maxlen != 0) { qWarning("QHttp::read: Null pointer error"); return -1; } if (maxlen >= d->rba.size()) maxlen = d->rba.size(); int readSoFar = 0; while (!d->rba.isEmpty() && readSoFar < maxlen) { int nextBlockSize = d->rba.nextDataBlockSize(); int bytesToRead = qMin<qint64>(maxlen - readSoFar, nextBlockSize); memcpy(data + readSoFar, d->rba.readPointer(), bytesToRead); d->rba.free(bytesToRead); readSoFar += bytesToRead; } d->bytesDone += maxlen;#if defined(QHTTP_DEBUG) qDebug("QHttp::read(): read %lld bytes (%lld bytes done)", maxlen, d->bytesDone);#endif return maxlen;}/*! Reads all the bytes from the response content and returns them. \sa get() post() request() readyRead() bytesAvailable() read()*/QByteArray QHttp::readAll(){ qint64 avail = bytesAvailable(); QByteArray tmp; tmp.resize(int(avail)); qint64 got = read(tmp.data(), int(avail)); tmp.resize(got); return tmp;}/*! Returns the identifier of the HTTP request being executed or 0 if there is no request being executed (i.e. they've all finished). \sa currentRequest()*/int QHttp::currentId() const{ Q_D(const QHttp); if (d->pending.isEmpty()) return 0; return d->pending.first()->id;}/*! Returns the request header of the HTTP request being executed. If the request is one issued by setHost() or close(), it returns an invalid request header, i.e. QHttpRequestHeader::isValid() returns false. \sa currentId()*/QHttpRequestHeader QHttp::currentRequest() const{ Q_D(const QHttp); if (!d->pending.isEmpty()) { QHttpRequest *r = d->pending.first(); if (r->hasRequestHeader()) return r->requestHeader(); } return QHttpRequestHeader();}/*! Returns the received response header of the most recently finished HTTP request. If no response has yet been received QHttpResponseHeader::isValid() will return false. \sa currentRequest()*/QHttpResponseHeader QHttp::lastResponse() const{ Q_D(const QHttp); return d->response;}/*! Returns the QIODevice pointer that is used as the data source of the HTTP request being executed. If there is no current request or if the request does not use an IO device as the data source, this function returns 0. This function can be used to delete the QIODevice in the slot connected to the requestFinished() signal. \sa currentDestinationDevice() post() request()*/QIODevice *QHttp::currentSourceDevice() const{ Q_D(const QHttp); if (d->pending.isEmpty()) return 0; return d->pending.first()->sourceDevice();}/*! Returns the QIODevice pointer that is used as to store the data of the HTTP request being executed. If there is no current request or if the request does not store the data to an IO device, this function returns 0. This function can be used to delete the QIODevice in the slot connected to the requestFinished() signal. \sa currentSourceDevice() get() post() request()*/QIODevice *QHttp::currentDestinationDevice() const{ Q_D(const QHttp); if (d->pending.isEmpty()) return 0; return d->pending.first()->destinationDevice();}/*! Returns true if there are any requests scheduled that have not yet been executed; otherwise returns false. The request that is being executed is \e not considered as a scheduled request. \sa clearPendingRequests() currentId() currentRequest()*/bool QHttp::hasPendingRequests() const{ Q_D(const QHttp); return d->pending.count() > 1;}/*! Deletes all pending requests from the list of scheduled requests. This does not affect the request that is being executed. If you want to stop this this as well, use abort(). \sa hasPendingRequests() abort()*/void QHttp::clearPendingRequests(){ Q_D(QHttp); // delete all entires except the first one while (d->pending.count() > 1) delete d->pending.takeLast();}/*! Sets the HTTP server that is used for requests to \a hostName on port \a port. The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished(). When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted. \sa get() post() head() request() requestStarted() requestFinished() done()*/int QHttp::setHost(const QString &hostName, quint16 port){ Q_D(QHttp); return d->addRequest(new QHttpSetHostRequest(hostName, port, ConnectionModeHttp));}/*! Sets the HTTP server that is used for requests to \a hostName on port \a port using the connection mode \a mode. If port is 0, it will use the default port for the \a mode used (80 for Http and 443 fopr Https). The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished(). When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted. \sa get() post() head() request() requestStarted() requestFinished() done()*/int QHttp::setHost(const QString &hostName, ConnectionMode mode, quint16 port){ Q_D(QHttp); if (port == 0) port = (mode == ConnectionModeHttp) ? 80 : 443; return d->addRequest(new QHttpSetHostRequest(hostName, port, mode));}/*! Replaces the internal QTcpSocket that QHttp uses with \a socket. This is useful if you want to use your own custom QTcpSocket subclass instead of the plain QTcpSocket that QHttp uses by default. QHttp does not take ownership of the socket, and will not delete \a socket when destroyed. The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished(). When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted. Note: If QHttp is used in a non-GUI thread that runs its own event loop, you must move \a socket to that thread before calling setSocket(). \sa QObject::moveToThread(), {Thread Support in Qt}*/int QHttp::setSocket(QTcpSocket *socket){ Q_D(QHttp); return d->addRequest(new QHttpSetSocketRequest(socket));}/*! This function sets the user name \a userName and password \a password for web pages that require authentication. The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished(). When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.*/int QHttp::setUser(const QString &userName, const QString &password){ Q_D(QHttp); return d->addRequest(new QHttpSetUserRequest(userName, password));}#ifndef QT_NO_NETWORKPROXY/*! Enables HTTP proxy support, using the proxy server \a host on port \a port. \a username and \a password can be provided if the proxy server requires authentication. Example: \code void Ticker::getTicks() { http = new QHttp(this); connect(http, SIGNAL(done(bool)), this, SLOT(showPage())); http->setProxy("proxy.example.com", 3128); http->setHost("ticker.example.com"); http->get("/ticks.asp"); } void Ticker::showPage() { display(http->readAll()); } \endcode QHttp supports non-transparent web proxy servers only, such as the Squid Web proxy cache server (from \l http://www.squid.org/). For transparent proxying, such as SOCKS5, use QNetworkProxy instead. \sa QFtp::setProxy()*/int QHttp::setProxy(const QString &host, int port, const QString &username, const QString &password){ Q_D(QHttp); QNetworkProxy proxy(QNetworkProxy::HttpProxy, host, port, username, password); return d->addRequest(new QHttpSetProxyRequest(proxy));}/*! \overload Enables HTTP proxy support using the proxy settings from \a proxy.*/int QHttp::setProxy(const QNetworkProxy &proxy){ Q_D(QHttp); return d->addRequest(new QHttpSetProxyRequest(proxy));}#endif/*! Sends a get request for \a path to the server set by setHost() or as specified in the constructor. \a path must be an absolute path like \c /index.html or an absolute URI like \c http://www.trolltech.com/index.html. If the IO device \a to is 0 the readyRead() signal is emitted every time new content data is available to read. If the IO device \a to is not 0, the content data of the response is written directly to the device. Make sure that the \a to pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted). The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished(). When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted. \sa setHost() post() head() request() requestStarted() requestFinished() done()*/int QHttp::get(const QString &path, QIODevice *to){ Q_D(QHttp); QHttpRequestHeader header(QLatin1String("GET"), path); header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); return d->addRequest(new QHttpPGHRequest(header, (QIODevice *) 0, to));}/*! Sends a post request for \a path to the server set by setHost() or as specified in the constructor. \a path must be an absolute path like \c /index.html or an absolute URI like \c http://www.trolltech.com/index.html. The incoming data comes via the \a data IO device. If the IO device \a to is 0 the readyRead() signal is emitted every time new content data is available to read. If the IO device \a to is not 0, the content data of the response is written directly to the device. Make sure that the \a to pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted). The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished(). When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted. \sa setHost() get() head() request() requestStarted() requestFinished() done()*/int QHttp::post(const QString &path, QIODevice *data, QIODevice *to ){ Q_D(QHttp); QHttpRequestHeader header(QLatin1String("POST"), path); header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); return d->addRequest(new QHttpPGHRequest(header, data, to));}/*! \overload \a data is used as the content data of the HTTP request.*/int QHttp::post(const QString &path, const QByteArray &data, QIODevice *to){ Q_D(QHttp); QHttpRequestHeader header(QLatin1String("POST"), path); header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); return d->addRequest(new QHttpPGHRequest(header, new QByteArray(data), to));}/*! Sends a header request for \a path to the server set by setHost() or as specified in the constructor. \a path must be an absolute path like \c /index.html or an absolute URI like \c http://www.trolltech.com/index.html. The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished(). When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted. \sa setHost() get() post() request() requestStarted() requestFinished() done()*/int QHttp::head(const QString &path){ Q_D(QHttp); QHttpRequestHeader header(QLatin1String("HEAD"), path); header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); return d->addRequest(new QHttpPGHRequest(header, (QIODevice*)0, 0));}/*! Sends a request to the server set by setHost() or as specified in the constructor. Uses the \a header as the HTTP request header. You are responsible for setting up a header that is appropriate for your request. The incoming data comes via the \a data IO device. If the IO device \a to is 0 the readyRead() signal is emitted every time new content data is available to read. If the IO device \a to is not 0, the content data of the response is written directly to the device. Make sure that the \a to pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted). The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished(). When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted. \sa setHost() get() post() head() requestStarted() requestFinished() done()*/int QHttp::request(const QHttpRequestHeader &header, QIODevice *data, QIODevice *to){ Q_D(QHttp); return d->addRequest(new QHttpNormalRequest(header, data, to));}/*! \overload \a data is used as the content data of the HTTP request.*/int QHttp::request(const QHttpRequestHeader &header, const QByteArray &data, QIODevice *to ){ Q_D(QHttp); return d->addRequest(new QHttpNormalRequest(header, new QByteArray(data), to));}/*! Closes the connection; this is useful if you have a keep-alive connection and want to close it. For the requests issued with get(), post() and head(), QHttp sets the connection to be keep-alive. You can also do this using the header you pass to the request() function. QHttp only closes the connection to the HTTP server if the response header requires it to do so. The function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -