PDA

View Full Version : QHttp::post() - cannot upload file



arunredi
16th May 2008, 06:54
I'm trying to upload the file using Qhttp::post(). But the file on the other end goes in as null. I double checked to match exactly how the web browser posts it using http header plugin for Mozilla and both are sending the same data but the file is not being posted by my Qt app.

here is my code:

void Snipitron::connectS()
{
http = new QHttp(this);
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
connect(http, SIGNAL(readyRead(const QHttpResponseHeader&)),
this, SLOT(readyRead(const QHttpResponseHeader&)));


http->setHost("www.example.com");

QString initialPath = QDir::currentPath() + tr("/unt.") + "jpeg";
userfile = new QFile(initialPath);

QByteArray data,payLoad;
QString boundary="-----------------------------AaB03x";
QString endline="\r\n";
QString start_delim="--"+boundary+endline;
QString cont_disp_str="Content-Disposition: form-data; ";

QString user_str = start_delim + cont_disp_str + "name=" + "\"user\""+endline+endline+"arun"+endline;
data.append(QString(user_str).toUtf8());

QString pass_str = start_delim + cont_disp_str + "name=" + "\"pass\""+endline+endline+"1a27452283b0b46720913760f056377eb0b6388c"+endline;
data.append(QString(pass_str).toUtf8());

QString proj_str = start_delim + cont_disp_str + "name=" + "\"project_name\""+endline+endline+"Misc"+endline;
data.append(QString(proj_str).toUtf8());

QString projId_str = start_delim + cont_disp_str + "name=" + "\"project_id\""+endline+endline+"1951"+endline;
data.append(QString(projId_str).toUtf8());

QString notes_str = start_delim + cont_disp_str + "name=" + "\"title\""+endline+endline+"Test project add - Qt way"+endline;
data.append(QString(notes_str).toUtf8());

if (!userfile->open(QIODevice::ReadOnly))
QMessageBox::critical(this, "Arun", "File not found", QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);


QString title_str = start_delim + cont_disp_str + "name=" + "\"uploadfile1\""+ ";filename=unt.jpeg" +endline + "Content-Type: image/jpeg" +endline+endline;
title_str.append(userfile->readAll());
title_str = title_str + endline;
data.append(QString(title_str).toUtf8());
userfile->close(); // the file is opened earlier in the code

QString v_str = start_delim + cont_disp_str + "name=" + "\"v\""+endline+endline+"1.0"+endline;
//data.writeRawBytes(v_str.data(),v_str.length());
data.append(QString(v_str).toUtf8());

QString cmd_str = start_delim + cont_disp_str + "name=" + "\"command\""+endline+endline+"add"+endline;
//data.writeRawBytes(cmd_str.data(),cmd_str.length() );
data.append(QString(cmd_str).toUtf8());

QString stop_delim="--"+boundary+"--"+endline;
data.append(QString(stop_delim).toUtf8());

QHttpRequestHeader header("POST", "/app/submit.jsp");
header.setValue("Host", "www.example.com");
header.setContentType("multipart/form-data, boundary=-----------------------------AaB03x");
header.setContentLength(data.length());
QMessageBox::critical(this, "Arun", data, QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
http->request(header, data);
}

what am i doing wrong here???

Thanks,

patrik08
16th May 2008, 08:14
I suppose you dont set cookie or session ...
submit.jsp java server having any time session ..
You must first call the page upload to get cokie or session .. and after upload...

I can only suppose if i not see the upload script from server ...

If you having access on server a easy way to upload is method PUT and use a serialkey as password inside a cookie ... as base64 encoded..

sample upload:
http://www.qtforum.de/forum/viewtopic.php?t=3085
http://www.qtforum.de/forum/viewtopic.php?t=2255

arunredi
16th May 2008, 08:17
No. I'm am not using any session or cookie on the server side. Its pretty straight forward html form. All the other information data that I'm posting is being recieved on the server. Just not the files.

patrik08
16th May 2008, 08:52
Ok test your normal html form on
Firefox + LiveHTTPHeaders. plug-in to grab all variables from header....

http://livehttpheaders.mozdev.org/

arunredi
16th May 2008, 09:01
I did that. Except for header variables everything else looks the same when my app does the post action.

I have also attached the http header result of the form along with the post.

THANKS,

patrik08
16th May 2008, 12:13
I did that. Except for header variables everything else looks the same when my app does the post action.

I have also attached the http header result of the form along with the post.

THANKS,

you dont have cookie???

Cookie: lang=en; country=US; JSESSIONID=cIThHwHP0QcjyJzWNr
modern http server having 90% session or cokkie session!!
immer cokkie essen...