Sending of multipart/form-data of request in Qt
来源:互联网 发布:spss类似的软件 编辑:程序博客网 时间:2024/06/05 11:45
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\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
- Sending of multipart/form-data of request in Qt
- Processing of multipart/form-data request failed. Stream ended unexpectedly
- Processing of multipart/form-data request failed. Read timed out
- Processing of multipart/form-data request failed. Stream ended unexpectedly
- 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.FileUploadException: Processing of multipart/form-data request failed解决
- Processing of multipart/form-data request failed. Connection reset 上传文件报错
- Struts FileUpload 异常处理之Processing of multipart/form-data request failed
- spring mvc上传文件Processing of multipart/form-data request failed
- 上传文件报错:Processing of multipart/form-data request failed. Stream ended unexpectedly
- 文件上传时异常Processing of multipart/form-data request failed. Stream ended unexpectedly at org.apache.com
- 报错Processing of multipart/form-data request failed. /upload/A.tmp (No such file or directory)
- enctype="multipart/form-data 无法通过request得到值
- 关于enctype="multipart/form-data" ,后台request.getParameterMap()
- request.getParameterValues()与enctype="multipart/form-data"不能共用
- 关于multipart/form-data
- multipart/form-data
- android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open
- QML类型说明-EnginioReply
- Linux 库文件的配置:pkg-config的使用
- 单精度浮点数转换成十六进制
- zoj3816,Generalized Palindromic Number,牡丹江网络赛,乱搞
- Sending of multipart/form-data of request in Qt
- 如何使用strace+pstack利器分析程序性能
- QML类型说明-ExclusiveGroup
- win7 ubuntu 双系统改为win7为默认启动
- Eclipse通过HibernateTools实现逆向生成Hibernate实体类
- [leetcode] Longest Valid Parentheses
- 关于c++的多态
- Struts2配置详解_配置Action
- 进程与线程学习笔记