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»
相关文章推荐
- Processing of multipart/form-data request failed. Stream ended unexpectedly
- FileUpload异常:Processing of multipart/form-data request failed. Stream ended unexpectedly
- 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
- 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
- Processing of multipart/form-data request failed. Connection reset 上传文件报错
- Struts FileUpload - Processing of multipart/form-data request failed
- org.apache.commons.fileupload.FileUploadException: Processing of multipart/form-data request failed解决
- 解决 Processing of multipart/form-data request failed. /upload/A.tmp (No such file or directory) 问题
- servlet: org.apache.tomcat.util.http.fileupload.FileUploadException: Processing of multipart/form-data request failed
- Unable to parse request org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
- 上传文件报错:Processing of multipart/form-data request failed. Stream ended unexpectedly
- Struts FileUpload 异常处理之Processing of multipart/form-data request failed.
- Struts FileUpload 异常处理之Processing of multipart/form-data request failed.
- JSP使用ENCTYPE="multipart/form-data"后request.getParameter无效问题Common-FileUpload组件解决方法
- multipart/form-data表单提交后,使用request.getParameter来获得text的参数值
- qt 错误:request for member 'show' in 'showlist_ui', which is of non-class type 'ShowList*'
- C#中HttpWebRequest用Post提交Content-Type: multipart/form-data; boundary=
- 解决当FORM的ENCTYPE="multipart/form-data" 时request.getParameter()获取不到值的方法
- JSP使用ENCTYPE="multipart/form-data"后request.getParameter无效问题Common-FileUpload组件解决方法
- FORM的ENCTYPE="multipart/form-data" 时request.getParameter()值为null问题的解决