void requestFinished ( int id, bool error )
QByteArray QHttp::readAll ()
void getdata(){buffer = new QBuffer();http=new QHttp("blablabla.ru");http->get("/some.html",buffer);connect(http,SIGNAL(done(bool)),this, SLOT(requestFinishedSlot(bool)));}void requestFinishedSlot(bool error){if(!error) { QByteArray data; data=buffer->data(); QString str(&data); textedit->setHtml(str); }}
#include "httpsio.h"#include <Qt/QtNetwork>httpsio::httpsio(){ //httpsconnect(); QTextStream stream(stdout); http = new QHttp(this); connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));// connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),// this, SLOT(readResponseHeader(const QHttpResponseHeader &))); //downloadFile urlLine="http://www.opennet.ru/opennews/art.shtml?num=15492"; QUrl url(urlLine); QFileInfo fileInfo(url.path()); QString fileName = fileInfo.fileName(); if (fileName.isEmpty()) fileName = "index.html"; stream<<"File name: "<<fileName<<endl; if (QFile::exists(fileName)) { stream<<"There already exists this file in the current directory \n"<<fileName<<endl; if(QFile::remove(fileName)) stream<<"It is deleted"<<endl; } file = new QFile(fileName); if (!file->open(QIODevice::WriteOnly)) { stream<<"Unable to save the file "<<fileName<<" : "<<file->errorString()<<endl; delete file; file = 0; return; } QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp; stream<<url.host()<<" "<<url.port()<<endl; http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port()); if (!url.userName().isEmpty()) http->setUser(url.userName(), url.password()); httpRequestAborted = false; stream<<url.path()<<" "<<file<<endl; httpGetId = http->get(url.path(), file); }httpsio::~httpsio(){}void httpsio::httpsconnect(){}void httpsio::httpRequestFinished(int requestId, bool error){ //RequestFinished QTextStream stream(stdout); stream<<"httpRequestFinished"<<endl; stream<<http->readAll()<<endl; if (requestId != httpGetId) return; if (httpRequestAborted) { if (file) { file->close(); file->remove(); delete file; file = 0; } return; } if (requestId != httpGetId) return; file->close(); if (error) { file->remove(); stream<<"Download failed: "<<http->errorString()<<endl; } else { QString fileName = QFileInfo(QUrl(urlLine).path()).fileName(); stream<<"Downloaded "<<fileName<<" to current directory."<<endl; } delete file; file = 0;}/*void httpsio::readResponseHeader(const QHttpResponseHeader &responseHeader){ QTextStream stream(stdout); stream<<"readResponseHeader"<<endl; if (responseHeader.statusCode() != 200) { stream<<"Download failed: "<<responseHeader.reasonPhrase()<<endl; httpRequestAborted = true; http->abort(); return; }}*/
#ifndef HTTPSIO_H_#define HTTPSIO_H_#include <QTextStream>class QFile;class QHttp;class QHttpResponseHeader;class httpsio : public QObject{ Q_OBJECTpublic: httpsio(); virtual ~httpsio(); void httpsconnect();private: QHttp *http; QFile *file; int httpGetId; bool httpRequestAborted; QString urlLine; private slots: void httpRequestFinished(int requestId, bool error);// void readResponseHeader(const QHttpResponseHeader &responseHeader);};#endif /*HTTPSIO_H_*/
urlLine = tr( "http://www.opennet.ru/opennews/art.shtml?num=15492" );//-- QUrl url( urlLine ).host() - opennet.ru (или http://www.opennet.ru/ - не помню)//-- QUrl url( urlLine ).path() - /opennews/art.shtml,//-- поэтому когда есть параметры GET - передавать надо "оригинальный" path:httpGetId = http->get( urlLine, file );
QHttpRequestHeader header( "GET", urlLine, 1, 1 );header.setValue( "Host", QUrl( urlLine ).host() );//header.setValue( "User-Agent", ... );//header.setValue( "Accept", ... );//header.setValue( "Accept-Language", ... );//header.setValue( "Accept-Charset", ... );//header.setValue( "Keep-Alive", ... );//header.setValue( "Connection", ... );//header.setValue( "Cookie", ... );...httpGetId = http->request( header, 0, file );
// /usr/lib/qt4/bin/qmake && make && ./httpsinfo#include <QtCore>#include <QCoreApplication>#include "httpsio.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); httpsio *httpsi=new httpsio(); delete httpsi; return a.exec();}
TEMPLATE = appTARGET = httpsinfoQT += core \ networkHEADERS += httpsio.hSOURCES += httpsio.cpp \ main.cppFORMS += RESOURCES +=
#include "httpsio.h"#include <Qt/QtNetwork>httpsio::httpsio(){ QTextStream stream(stdout); http = new QHttp(this); connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool))); connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &))); //downloadFile urlLine="http://www.opennet.ru/opennews/art.shtml?num=15492"; QUrl url(urlLine); QFileInfo fileInfo(url.path()); QString fileName = fileInfo.fileName(); if (fileName.isEmpty()) fileName = "index.html"; stream<<"File name: "<<fileName<<endl; if (QFile::exists(fileName)) { stream<<"There already exists this file in the current directory \n"<<fileName<<endl; if(QFile::remove(fileName)) stream<<"It is deleted"<<endl; } file = new QFile(fileName); if (!file->open(QIODevice::WriteOnly)) { stream<<"Unable to save the file "<<fileName<<" : "<<file->errorString()<<endl; delete file; file = 0; return; } QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp; stream<<url.host()<<" "<<url.port()<<endl; http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port()); if (!url.userName().isEmpty()) http->setUser(url.userName(), url.password()); httpRequestAborted = false; stream<<url.path()<<" "<<file<<endl; httpGetId = http->get(url.path(), file); }httpsio::~httpsio(){}void httpsio::httpsconnect(){}void httpsio::httpRequestFinished(int requestId, bool error){ //RequestFinished QTextStream stream(stdout); stream<<"httpRequestFinished"<<endl; stream<<http->readAll()<<endl; if (requestId != httpGetId) return; if (httpRequestAborted) { if (file) { file->close(); file->remove(); delete file; file = 0; } return; } if (requestId != httpGetId) return; file->close(); if (error) { file->remove(); stream<<"Download failed: "<<http->errorString()<<endl; } else { QString fileName = QFileInfo(QUrl(urlLine).path()).fileName(); stream<<"Downloaded "<<fileName<<" to current directory."<<endl; } delete file; file = 0;}void httpsio::readResponseHeader(const QHttpResponseHeader &responseHeader){ QTextStream stream(stdout); stream<<"readResponseHeader"<<endl; if (responseHeader.statusCode() != 200) { stream<<"Download failed: "<<responseHeader.reasonPhrase()<<endl; httpRequestAborted = true; http->abort(); return; }}
#ifndef HTTPSIO_H_#define HTTPSIO_H_#include <QTextStream>class QFile;class QHttp;class QHttpResponseHeader;class httpsio : public QObject{ Q_OBJECTpublic: httpsio(); virtual ~httpsio(); void httpsconnect();private: QHttp *http; QFile *file; int httpGetId; bool httpRequestAborted; QString urlLine; private slots: void httpRequestFinished(int requestId, bool error); void readResponseHeader(const QHttpResponseHeader &responseHeader);};#endif /*HTTPSIO_H_*/