Overview
Features
Download
Documentation
Community
Add-Ons & Services

HTTP : send a HTML form

A general discussion forum.

HTTP : send a HTML form

Postby cdelmas » 10 Jan 2007, 14:01

Hi all,
I try to send a HTML form using POST method but my request fails (the response I receive is "Internal Server Error"). I wrote a HTML page to compare with the same request, and it works.

This is the code of the HTML page :
^
form method="POST"
action="http://path/to/my/page.cfm"
enctype="multipart/form-data"
input type="text" name="PurgeXML" /
input type="submit" value="Send It" /
/form
^

This is now the C++ code I wrote, and doesn't work :

^
const string field("PurgeXML");
const string mypage("http://path/to/my/page.cfm");
const string data("" "");
try {
URI uri(mypage);
string path(uri.getPathAndQuery());
if (path.empty()) {
path = "/";
}

HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_0);

HTMLForm form(HTMLForm::ENCODING_MULTIPART);
form.set(field,data);
form.prepareSubmit(req);
session.sendRequest(req);
HTTPResponse res;
istream& rs = session.receiveResponse(res);

if(res.getStatus() != HTTPResponse::HTTP_OK) {
//failure
}

stringstream responseS;
responseS << rs.rdbuf();
string response(responseS.str());

cout << response;
}catch (Poco::Exception& e) {
//failure
}
^

Can anybody help me to understand what is wrong ? (I'm a newbie in HTTP requests, and this is an important work).

Thanks in advance,
cd.
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: HTTP : send a HTML form

Postby guenter » 10 Jan 2007, 14:55

Since you're using POST to send the form data, you must send the message body containing the form data. You can do this by replacing the line

session.sendRequest(req);

with

form.send(session.sendRequest(req));

guenter
 
Posts: 1132
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Re: HTTP : send a HTML form

Postby cdelmas » 10 Jan 2007, 15:08

Thanks for your quick answer, but HTMLForm does not have a "send" member function. I see want you mean, but don't know how to do.
I tried
form.write(session.sendRequest(req))
but it fails again (same error). Something else ?
cd.
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: Re: Re: HTTP : send a HTML form

Postby guenter » 10 Jan 2007, 16:27

Sorry, it's actually form.write(). No idea why it does not work, though. Maybe try to use a tool like Ethereal to find out what your application sends vs. what the browser sends.
guenter
 
Posts: 1132
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Re: Re: Re: HTTP : send a HTML form

Postby cdelmas » 10 Jan 2007, 18:11

> Sorry, it's actually form.write(). No idea why it does not work, though. Maybe try to use a tool like Ethereal to find out what your application sends vs. what the browser sends.

This is the server response :

^
HTTP/1.1 500 Internal Server Error
Server: Microsoft-IIS/5.0
Date: Wed, 10 Jan 2007 15:27:47 GMT
X-Powered-By: ASP.NET
Content-Language: fr-FR

JRun Servlet Error500 Corrupt form data: premature ending
Corrupt form data: premature ending
^

This is what the application sends :
^
POST /path/to/mypage.cfm HTTP/1.0
Connection: Close
Content-Type: multipart/form-data; boundary="MIME_boundary_6C246EF87B050027"
Host: foobar.com

--MIME_boundary_6C246EF87B050027
Content-Disposition: form-data; name="PurgeXML"

--MIME_boundary_6C246EF87B050027--
^

And this is the conversation between the browser and the site :

^
__browser talking__
POST /path/to/mypage.cfm HTTP/1.1
Host: foobar.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1
Accept:text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------23281168279961
Content-Length: 633

__server talking__
HTTP/1.1 100 Continue
Server: Microsoft-IIS/5.0
Date: Wed, 10 Jan 2007 15:25:09 GMT
X-Powered-By: ASP.NET


__browser talking__
-----------------------------23281168279961
Content-Disposition: form-data; name="PurgeXML"


-----------------------------23281168279961--


__server talking__
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 10 Jan 2007 15:25:09 GMT
X-Powered-By: ASP.NET
Connection: close

Content-Type: text/html; charset=iso-8859-1


^

According to my colleague, my request seems OK, but maybe it misses something. My question is : what's missing, and how can I add it to complete the request ?

Thanks.
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: Re: Re: Re: Re: HTTP : send a HTML form

Postby cdelmas » 11 Jan 2007, 12:11

I've found why my request fails : after the header there are too many crlf.
As I debug my code, form.write(cout) prints a leading crlf. If I erase it, my request succeeds.
My guess is that HTMLForm is maybe buggy (it gives an additionnal leading crlf in the output stream...).
Can anybody confirm that ?

Thanks.
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: Re: Re: Re: Re: Re: HTTP : send a HTML form

Postby guenter » 11 Jan 2007, 14:16

With regard to RFC 1341, HTMLForm is correct:
^The requirement that the encapsulation boundary begins witha CRLF implies that the body of a multipart entity must itself begin with a CRLF before the first encapsulation line -- that is, if the "preamble" area is not used, the entity headers must be followed by TWO CRLFs. This is indeed how such entities should be composed. A tolerant mail reading
program, however, may interpret a body of type multipart that begins with an encapsulation line NOT initiated by a CRLF as also being an encapsulation boundary, but a compliant mail sending program must not generate such entities.
^

Interestingly, in the W3C HTML specification of multipart/form-data, there is no additional CR-LF at the beginning of the multipart form data.

Ah, just found out that later revisions of 1341 (beginning with RFC 1521) removed the leading CR-LF. Damn.

Okay, I wil fix that in MultipartWriter...

guenter
 
Posts: 1132
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Re: Re: Re: Re: Re: Re: HTTP : send a HTML form

Postby guenter » 11 Jan 2007, 14:42

The fix for MultipartWriter is now in the trunk in the Subversion repository on Sourceforge.
guenter
 
Posts: 1132
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests