Tuck Design
WE MAKE YOUR IDEAS
COME TO LIFE
Bookmark and Share
Screen Tool Download for free demo version of Screen Tool:
screen capturing software for Windows, Linux (static) or MacOSX.
It will let you easily capture screen,
make annotations
and send it through Skype or save it.
For more details and shop click here.


Questions/Answers and sources for Qt framework


Can any one tell me how to use "formpostinterface.lib".Pls give me the solution
"FormPost" code published here is Qt plugin library.
About using Qt plugins see detailed Qt documentation on: http://doc.trolltech.com/4.5/plugins-howto.html#the-lower-level-api-extending-qt-applications and http://doc.trolltech.com/4.5/deployment-mac.html#qt-plugins.

About this specific plugin here is description of its functions:
Lets first presume you have FormPost in variable called "formPost" like
FormPost * formPost=new FormPost();

Now you can set some basics(optional):
formPost->setUserAgent("myBrowser"); //sets user agent form on web will recieve - default ""
formPost->setEncoding("UTF-8"); //sets transfer encoding (default is "utf-8")

Then you add form fields with eg:
formPost->addField("name", "Tuckdesign"); // adds fields in name/value pairs
You can add as many fields as you want.

And you can add files for upload:
formPost->addFile("upload", "mypic.jpg", "image/jpeg"); //adds file - first arg is form field name (php on server will get it in $_FILES['upload']); second arg is file name on your computer, or it can be QByteArray with file contents, but then you need to specify name as 3rd arg; and 3rd(4th) arg is mime type of file

At the end you need to post data to server:
QNetworkReply * postReply=formPost->postData("http://www.mydomain.com/form.php"); //will post data to specified url that then needs to process it on server
connect(postReply,SIGNAL(finished()),this,SLOT(postFinished()));

Conecting to slot is not mandatory, but this way you can get back results from server:
void yourclass::postFinished() {
   QByteArray data=formPost->response();
   QString result(data);
   qDebug(qPrintable(result));
}
You need to login to be able to post your answer.
How to make HTTP post to website form using POST method including file uploads?
Because of lot of interest of Open Source community we publish our Qt Form post plugin sources here.

formpostinterface.h
#ifndef __FORMPOSTINTERFACE_H__
#define __FORMPOSTINTERFACE_H__
#include <QByteArray>
#include <QString>
#include <QNetworkReply>

class FormPost
{
  public:
   virtual ~FormPost() {}
    virtual QString userAgent() = 0;
    virtual void setUserAgent(QString agent) = 0;
    virtual QString referer() = 0;
    virtual void setReferer(QString ref) = 0;
    virtual QString encoding() = 0;
    virtual void setEncoding(QString enc) = 0;
    virtual void addField(QString name, QString value) = 0;
    virtual void addFile(QString fieldName, QByteArray file, QString name, QString mime) = 0;
    virtual void addFile(QString fieldName, QString fileName, QString mime) = 0;
    virtual QNetworkReply * postData(QString url) = 0;
    virtual QByteArray response() = 0;
};

QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(FormPost,"com.tuckdesign.Plugin.FormPost/1.0");
QT_END_NAMESPACE

#endif


formpost.h
#ifndef __FORMPOST_H__
#define __FORMPOST_H__
#include <QtPlugin>
#include <QString>
#include <QByteArray>
#include <QFile>
#include <QProgressBar>
#include <QDateTime>
#include <QWidget>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

#include "formpostinterface.h";

class FormPostPlugin: public QWidget, FormPost
{
Q_OBJECT
Q_INTERFACES(FormPost)
  public:
    FormPostPlugin();
    QString userAgent();
    void setUserAgent(QString agent);
    QString referer();
    void setReferer(QString ref);
    QString encoding();
    void setEncoding(QString enc);
    void addField(QString name, QString value);
    void addFile(QString fieldName, QByteArray file, QString name, QString mime);
    void addFile(QString fieldName, QString fileName, QString mime);
    QNetworkReply * postData(QString url);
    QByteArray response();
  private:
    QByteArray data;
    QByteArray strToEnc(QString s);
    QString encodingS;
    QString userAgentS;
    QString refererS;
    QStringList fieldNames;
    QStringList fieldValues;
    QStringList fileFieldNames;
    QStringList fileNames;
    QStringList fileMimes;
    QList<QByteArray> files;
  private slots:
    void readData(QNetworkReply * r);
};

#endif


formpost.cpp
#include "formpost.h"

FormPostPlugin::FormPostPlugin()
:QWidget(0)
{
  userAgentS="";
  encodingS="utf-8";
  refererS="";
}

QString FormPostPlugin::userAgent() {
  return userAgentS;
}

void FormPostPlugin::setUserAgent(QString agent) {
  userAgentS=agent;
}

QString FormPostPlugin::referer() {
  return refererS;
}

void FormPostPlugin::setReferer(QString ref) {
  refererS=ref;
}

QString FormPostPlugin::encoding() {
  return encodingS;
}

void FormPostPlugin::setEncoding(QString enc) {
  if (enc=="utf-8" || enc=="ascii") {
    encodingS=enc;    
  }
}

QByteArray FormPostPlugin::strToEnc(QString s) {
  if (encodingS=="utf-8") {
    return s.toUtf8();
  } else {
    return s.toAscii();
  }
}

void FormPostPlugin::addField(QString name, QString value) {
  fieldNames.append(name);
  fieldValues.append(value);
}

void FormPostPlugin::addFile(QString fieldName, QByteArray file, QString name, QString mime) {
  files.append(file);
  fileFieldNames.append(fieldName);
  fileNames.append(name);
  fileMimes.append(mime);
}

void FormPostPlugin::addFile(QString fieldName, QString fileName, QString mime) {
  QFile f(fileName);
  f.open(QIODevice::ReadOnly);
QByteArray file=f.readAll();
f.close();
QString name;
if (fileName.contains("/")) {
  int pos=fileName.lastIndexOf("/");
  name=fileName.right(fileName.length()-pos-1);
  } else if (fileName.contains("\\")) {
  int pos=fileName.lastIndexOf("\\");
  name=fileName.right(fileName.length()-pos-1);
  } else {
    name=fileName;  
  }
  addFile(fieldName,file,name,mime);
}

QNetworkReply * FormPostPlugin::postData(QString url) {
  QString host;
  host=url.right(url.length()-url.indexOf("://")-3);
  host=host.left(host.indexOf("/"));
  QString crlf="\r\n";
  qsrand(QDateTime::currentDateTime().toTime_t());
  QString b=QVariant(qrand()).toString()+QVariant(qrand()).toString()+QVariant(qrand()).toString();
  QString boundary="---------------------------"+b;
  QString endBoundary=crlf+"--"+boundary+"--"+crlf;
  QString contentType="multipart/form-data; boundary="+boundary;
  boundary="--"+boundary+crlf;
  QByteArray bond=boundary.toAscii();
  QByteArray send;
  bool first=true;
  
  for (int i=0; i<fieldNames.size(); i++) {
    send.append(bond);
    if (first) {
      boundary=crlf+boundary;
      bond=boundary.toAscii();
      first=false;
    }
    send.append(QString("Content-Disposition: form-data; name=\""+fieldNames.at(i)+"\""+crlf).toAscii());
    if (encodingS=="utf-8") send.append(QString("Content-Transfer-Encoding: 8bit"+crlf).toAscii());
    send.append(crlf.toAscii());
    send.append(strToEnc(fieldValues.at(i)));
  }
  for (int i=0; i<files.size(); i++) {
    send.append(bond);
    send.append(QString("Content-Disposition: form-data; name=\""+fileFieldNames.at(i)+"\"; filename=\""+fileNames.at(i)+"\""+crlf).toAscii());
    send.append(QString("Content-Type: "+fileMimes.at(i)+crlf+crlf).toAscii());
    send.append(files.at(i));  
  }

  send.append(endBoundary.toAscii());
  
  fieldNames.clear();
  fieldValues.clear();
  fileFieldNames.clear();
  fileNames.clear();
  fileMimes.clear();
  files.clear();

  QNetworkAccessManager * http=new QNetworkAccessManager(this);
  connect(http,SIGNAL(finished(QNetworkReply *)),this,SLOT(readData(QNetworkReply *)));
  QNetworkRequest request;
  request.setRawHeader("Host", host.toAscii());
  if (userAgentS!="") request.setRawHeader("User-Agent", userAgentS.toAscii());
  if (refererS!="") request.setRawHeader("Referer", refererS.toAscii());
  request.setHeader(QNetworkRequest::ContentTypeHeader, contentType.toAscii());
  request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(send.size()).toString());
  request.setUrl(QUrl(url));
  QNetworkReply * reply=http->post(request,send);
  return reply;
}

void FormPostPlugin::readData(QNetworkReply * r) {
  data=r->readAll();
}

QByteArray FormPostPlugin::response() {
  return data;
}

Q_EXPORT_PLUGIN2(formpost, FormPostPlugin);



formpost.pro
TEMPLATE = lib
QT += core \
network
CONFIG += release \
warn_off \
plugin \
shared
DESTDIR += bin
OBJECTS_DIR += build
MOC_DIR += build
UI_DIR += build
HEADERS += src/formpost.h \
src/formpostinterface.h
SOURCES += src/formpost.cpp
TARGET = $$qtLibraryTarget(formpost)


Have fun!
You need to login to be able to post your answer.
What is new?
    follow us on Twitter

    Check out our
    monthly discount:

    Dropdown Menus for just €20



    screen-capture-tools :: screen-tool

    screen-capture-tools :: screen-tool

    1stdownload