QByteArray params; QNetworkAccessManager accessManager; QNetworkRequest request; QUrl url("http://jiffyplace.com/photo.php?a=1&b=2"); request.setUrl(url); request.setRawHeader("Host", "jiffyplace.com"); request.setRawHeader("Cache-Control", "no-cache"); request.setRawHeader("Accept","*/*"); params.append("a=ok"); params.append("b=ok2"); reply = accessManager.post(request, "a=asd");
request.setHeader(QNetworkRequest::ContentLengthHeader, parmas.length());
Request::Request(bool syn, QObject *parent) : QObject(parent), synhro(syn){ manager=new QNetworkAccessManager(this); QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(sent(QNetworkReply*)));}void Request::setParametersForRequetsSlot(const QByteArray &method, const QByteArray &addr, QList<QByteArray> &headers, const QByteArray &body){ p_method=method; p_addr=addr; p_headers=headers; p_body=body;}void Request::sendRequest(){ sendRequest(p_method, p_addr, p_headers, p_body);}void Request::sendRequest(const QByteArray &method, const QByteArray &addr, QList<QByteArray> &headers, const QByteArray &body){ int type=0; QByteArray ba=method.simplified().toLower(); if(ba.contains("put"))type=1; else if(ba.contains("get"))type=2; //post - default QNetworkRequest request; for(int i=0;i<headers.count();i++){ ba=headers.at(i); int x=ba.indexOf(": "); if(x<0)continue;//игнорирование неверной строчки if(x==ba.length()-2)continue; request.setRawHeader(ba.left(x),ba.right(ba.length()-x-2)); } if(type!=2) request.setHeader(QNetworkRequest::ContentLengthHeader, body.length()); //не гет request.setUrl(QUrl(addr)); replyBody.clear(); replyHeaders.clear(); ended=false; switch(type){ case 0: manager->post(request,body); break; case 1: manager->put(request, body); break; case 2: manager->get(request); } //подождать if(synhro) while(!ended)QCoreApplication::processEvents();}void Request::sent(QNetworkReply *reply){ replyBody=reply->readAll(); replyHeaders=reply->rawHeaderList(); for(int i=0;i<replyHeaders.count();i++){ replyHeaders[i]=replyHeaders.at(i)+": "+reply->rawHeader(replyHeaders.at(i)); } replyError=reply->error(); replyErrorString=reply->errorString(); replyURL=reply->url().toString(); ended=true; emit finished();}
#ifndef REQUEST_H#define REQUEST_H#include <QObject>#include <QList>#include <QNetworkAccessManager>#include <QNetworkReply>class Request : public QObject{ Q_OBJECTpublic: explicit Request(bool syn=true, QObject *parent = 0); void sendRequest(const QByteArray &method, const QByteArray &addr, QList <QByteArray> &headers, const QByteArray &body); void setParametersForRequetsSlot(const QByteArray &method, const QByteArray &addr, QList <QByteArray> &headers, const QByteArray &body); const QByteArray& getReply(){ return replyBody; } QList<QByteArray> getReplyHeaders(){ return replyHeaders; } int getError(){ return replyError; } QString getErrorString(){ return replyErrorString; } QString getReplyURL(){ return replyURL; } void synchronous(bool s=true){ synhro=s; }private: QNetworkAccessManager *manager; QByteArray replyBody; QList<QByteArray> replyHeaders; bool ended; int replyError; QString replyErrorString; QString replyURL; bool synhro; QByteArray p_method; QByteArray p_addr; QList <QByteArray> p_headers; QByteArray p_body;public slots: void sendRequest();private slots: void sent(QNetworkReply *reply);signals: void finished();};#endif // REQUEST_H
params.append("--AyV04a\r\n"); params.append("content-disposition: "); params.append("form-data; name=\"p1\"\r\n"); params.append("\r\n"); params.append("Konstantin"); params.append("\r\n"); params.append("--AyV04a\r\n"); params.append("content-disposition: "); params.append("file; name=\"file\"; filename=\""+file.fileName()+"\"\r\n"); params.append("Content-Transfer-Encoding: binary\r\n"); params.append("\r\n"); params.append(file.readAll()); params.append("\r\n"); params.append("--AyV04a--"); request.setHeader(QNetworkRequest::ContentLengthHeader, params.length());
params.append("--AyV04a\r\n"); params.append("content-disposition: "); params.append("form-data; name=\"p1\"\r\n"); params.append("\r\n"); params.appe...
params = "a=1&b=2";
params.append("--AyV04a\r\n");