您的位置:首页 > 产品设计 > UI/UE

Sending of multipart/form-data of request in Qt

2014-09-09 14:45 603 查看


from: http://sysmagazine.com/posts/143168/


Sometimes by development of a network application there are a task of loading on the server of a file, and are not simple so, and as parts of the filled http of the form. It are an example of so-called
multipart/form-data of request. Standard methods of library of Qt of it done not allow to make, therefore it are necessary to get out by own strength.


Shared information

So, first of all it are necessary to understand, what such interesting our multipart/form-data contained request?

If to look at an example from here, the typical request represented
the following:

POST http://www.site.ru/news.html HTTP/1.0\r\n
Host: www.site.ru\r\n
Referer: http://www.site.ru/index.html\r\n Cookie: income=1\r\n
Content-Type: multipart/form-data; boundary=1BEF0A57BE110FD467A\r\n
Content-Length: 209\r\n
\r\n
--1BEF0A57BE110FD467A\r\n
Content-Disposition: form-data; name="login"\r\n
\r\n
Petya Vasechkin\r\n
--1BEF0A57BE110FD467A\r\n
Content-Disposition: form-data; name="password"\r\n
\r\n
qq\r\n
--1BEF0A57BE110FD467A--\r\n


That us especially interested in titles — are boundary=1BEF0A57BE110FD467A and Content-Length: 209 then the body of request began. The request consisted of several parts, thus that are wr as boundary as the length
of a body of request — this field of Content-Length should be mandatory specif will be considered as a separator. A body of request — all since first line - 1BEF0A57BE110FD467A. In each section of name — the name of an appropriate field
of the form, after two transfers of lines \r\n\r\n went field value

For sending of a file it are necessary to create section of the following format:

--1BEF0A57BE110FD467A\r\n
Content-Disposition: form-data; name="news_file"; filename="news.txt"\r\n
Content-Type: application/octet-stream\r\n
Content-Transfer-Encoding: binary\r\n
\r\n
А вот такая новость, которая лежит в файле news.txt\r\n


Here the file name — news.txt, and as the coding of the data in the field of Content-Transfer-Encoding are in addition set. There was some different codings, including present binary — not coded data. Taking into account possibilities of Qt, it are very convenient
to use the coding of base64. If a file (application/octet-stream) not simply any there, and known type it are possible to specify in the field of Content-Type this type, for example Content-Type: image/png.


Simple example

Let's pass to a practical example of formation of request. At us are:

//язык С++ и библиотека Qt
QNetworkAccessManager *manager;
//параметр 1 - какое-то поле, параметр 2 - файл
QByteArray param1Name="param1" ,param1Value="value1";
QByteArray param2Name="param2", param2FileName="news.txt",
param2ContentType="text/plain",param2Data="А вот такая новость, которая лежит в файле news.txt";


Let's generate to begin with a body of request:

//задаем разделитель
QByteArray postData,boundary="1BEF0A57BE110FD467A";
//первый параметр
postData.append("--"+boundary+"\r\n");//разделитель
//имя параметра
postData.append("Content-Disposition: form-data; name=\"");
postData.append(param1Name);
postData.append("\"\r\n\r\n");
//значение параметра
postData.append(param1Value);
postData.append("\r\n");

//параметр 2 - файл
postData.append("--"+boundary+"\r\n");//разделитель
//имя параметра
postData.append("Content-Disposition: form-data; name=\"");
postData.append(param2Name);
//имя файла
postData.append("\"; filename=\"");
postData.append(param2FileName);
postData.append("\"\r\n");
//тип содержимого файла
postData.append("Content-Type: "+param2ContentType+"\r\n");
//передаем в base64
postData.append("Content-Transfer-Encoding: base64\r\n\r\n");
//данные
postData.append(param2Data.toBase64());
postData.append("\r\n");
//"хвост" запроса
postData.append("--"+boundary+"--\r\n");


In variable postData it are receiv a ready body of request — it were necessary to send and not to forget only to install additional titles of request:

QNetworkRequest request(QUrl("http://example.com/submit.php"));
request.setHeader(QNetworkRequest::ContentTypeHeader,
"multipart/form-data; boundary="+boundary);
request.setHeader(QNetworkRequest::ContentLengthHeader,
QByteArray::number(postData.length()));
QNetworkReply *reply=manager->post(request,postData);


Well and further — on the nakatanny track, as for any other requests.


As a result...

G an example, of course, too simple for daily application. But on it the basis can implement easily function, a class or class library under each specific target, vzavisimost from what complexity the requests to you should be generat.

The helpful information:
http://www.codenet.ru/webmast/php/HTTP-POST.php — the description
of a stuffing of http of requests.
http://www.ietf.org/rfc/rfc2388.txt — the Standard «Returning Values from Forms:
multipart/form-data»
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐