Sending of multipart/form-data of request in Qt

来源:互联网 发布:spss类似的软件 编辑:程序博客网 时间:2024/06/05 11:45

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\nHost: www.site.ru\r\nReferer: http://www.site.ru/index.html\r\nCookie: income=1\r\nContent-Type: multipart/form-data; boundary=1BEF0A57BE110FD467A\r\nContent-Length: 209\r\n\r\n--1BEF0A57BE110FD467A\r\nContent-Disposition: form-data; name="login"\r\n\r\nPetya Vasechkin\r\n--1BEF0A57BE110FD467A\r\nContent-Disposition: form-data; name="password"\r\n\r\nqq\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\nContent-Disposition: form-data; name="news_file"; filename="news.txt"\r\nContent-Type: application/octet-stream\r\nContent-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:

//язык С++ и библиотека QtQNetworkAccessManager *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");//передаем в base64postData.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»
0 0