PDA

View Full Version : QHttp::post() help



Funklord
7th February 2006, 16:25
I am having trouble with QHttp and related classes when trying to post a file to a server.
No matter what I try, the header response is: "Internal server error"

My code below (error checks, signals and checks for return values removed for clarity)

QHttp *http;
QFile *file;
QUrl url(lineEdit_url->text());
file = new QFile(lineEdit_filename->text());
file->open(QIODevice::ReadOnly)
http->setHost(url.host(),url.port() != -1 ? url.port() : 80);
if(!url.userName().isEmpty())
http->setUser(url.userName(),url.password());
http->post(url.path(),file);


Here is a working html form for posting to this server:

<form method=post enctype="multipart/form-data" action=/uploader/Default.asp>
Username:<br/><input type="text" name="username"><br/><br/>
Password:<br/><input type="password" name="password"><br/><br/>
Your File:<BR><input type=file name=YourFile><BR><BR>
<input type=submit name=submit value="Upload">
</form>


There are no examples in qtassistant apart from the brief description of QHttp:: post() :mad:

Any help is deeply appreciated!

jacek
7th February 2006, 16:41
You can use Ethereal or Proxomitron to see how browsers post files and how your program does this. You will probably have to use QHttpRequestHeader to set proper content type.

crocus
8th February 2006, 16:01
void HttpWindow::uploadFile()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("OpenFile"),
openFilesPath,
tr("All Files (*);;Text Files (*.txt)"));
if (!fileName.isEmpty())
openFilesPath = fileName;
QFileInfo path(openFilesPath);
QString fileName1 = path.fileName();
userfile = new QFile(openFilesPath);
if ( !userfile->open(QIODevice::ReadOnly) )
{
QMessageBox::information(this, tr("HTTP"),
tr("Unable to open the file %1: %2.")
.arg(openFilesPath).arg(userfile->errorString()));

}

QHttpRequestHeader header("POST", "/upload.php", 1, 1);
header.setValue("Host", "foliant");
header.setValue("Content-type", "multipart/form-data, boundary=AaB03x");
header.setValue("Cache-Control", "no-cache");
header.setValue("Accept","*/*");

QByteArray byt(openFilesPath.toUtf8());
QByteArray bytes;
bytes.append("--AaB03x\r\n");
bytes.append("content-disposition: ");
bytes.append("form-data; name=\"agency\"\r\n");
bytes.append("\r\n");
bytes.append("0\r\n");
bytes.append("--AaB03x\r\n");
bytes.append("content-disposition: ");
bytes.append("form-data; name=\"userfile\"; filename=\"" + byt+ "\"\r\n");
bytes.append("Content-Transfer-Encoding: binary\r\n");
bytes.append("\r\n");
bytes.append(userfile->readAll());
userfile->close(); // the file is opened earlier in the code
bytes.append("\r\n");
bytes.append("--AaB03x--");
int contentLength = bytes.length();
header.setContentLength(contentLength);

http->setHost("foliant");
httpRequestAborted = false;
httpGetId = http->request(header, bytes);
}

Funklord
16th February 2006, 15:30
Thanks a lot guys, got it working!
Seems there was a retarded VB script at the other end that's case-sensitive in places it shouldn't be...

awalesminfo
22nd March 2006, 08:10
hi guys am newbie , am also working on webbrowser i need ur help i want to know the location of these codes , or please mail me the http related code to me, my email is awalesminfo@gmail.com