Sending of multipart/form-data of request in Qt

本文深入解析了multipart/form-data在HTTP POST请求中的应用方式,包括边界值、内容长度、文件上传格式等关键要素,并通过C++与Qt库的示例代码展示如何实现复杂的文件上传功能。
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»
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值