简体   繁体   English

如何发出将发布查询和文件都上传到C ++ Qt中的服务器的http发布请求

[英]How to make an http post request that uploads both post queries & a file to a server in C++ Qt

I have sample code for doing this in C#, Windows Phone 7, Silverlight, and I'm trying to find how to do the equivalent in C++ Qt (I am working in the Blackberry 10 Beta 3 SDK). 我有在C#,Windows Phone 7,Silverlight中执行此操作的示例代码,并且我试图找到如何在C ++ Qt中执行等效操作(我正在使用Blackberry 10 Beta 3 SDK)。 Can I use QNetworkAccessManager ? 我可以使用QNetworkAccessManager吗? Or is there something better for this? 还是对此有更好的选择? I would appreciated some help on how to do this and some sample code if you can. 如果可以的话,我将不胜感激,并提供一些示例代码。 Here is the sample C# code: 这是示例C#代码:

var request = new RestRequest(url, Method.POST);

request.AddParamater("example", "example");
request.AddParamater("example2", "example2");

request.AddFile("file", fileData, filename, "image/pjpeg");

RestSharp.RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
   if (response.StatusCode == HttpStatusCode.OK)
   {
       //upload successful
   }
   else
   {
       //error occured durring upload
   }
});

Here is something I have tried: 这是我尝试过的:

// postVars is a QByteArray that contains a query string of the arguments 
// being passed in then I pass it in to my prepareFileUploadRequest file, 
// which appends the data for   uploading a file.

void Utils::prepareFileUploadRequest(QMap<QString, QString> *d,
        QNetworkRequest &request, QByteArray &postVars, QString path,
        QString scriptName, QString filename,
        QString stockNum)
    {

        request.setUrl(QUrl(Utils::ApiUrl));

        postVars.append("Content-type: multipart/form-data, ");
            postVars.append("boundary='---';\r\n\r\n");
        postVars.append("---\r\n");
        postVars.append("Content-Disposition: form-data; name='file' );
            postVars.append("filename='examplePic.jpg'\r\n");
        postVars.append("Content-Type: image/jpg\r\n");
        postVars.append("Content-Transfer-Encoding: binary\r\n\r\n");

        QFile file(path);
        if (!file.open(QIODevice::ReadOnly)) {
            return;
        }

        postVars.append(file.readAll() + "\r\n");
        postVars.append('---');

        request.
                setRawHeader(QString("Content-Type").toAscii(),                   
                QString("multipart/form-data; boundary='---'").toAscii());

            request.
                setRawHeader(QString("Content-Length").toAscii(),            
                QString::number(postVars.length()).toAscii());

            QNetworkAccessManager *nam = new QNetworkAccessManager(this);
            connect(nam, SIGNAL(finished(QNetworkReply *)), 
                    this, SLOT(handleUpload(QNetworkReply *)));
            nam->post(request, postVars);
 }

I call that function above, and then I get an xml reply - which says I don't have permission - and it doesn't upload any file - so that's probably an issue with the php webservice that is used, but it makes it hard for me to tell if I have taken the right approach, or if I am doing anything wrong. 我在上面调用了该函数,然后得到一个xml答复-这表示我没有权限-并且它没有上载任何文件-因此使用的php webservice可能是一个问题,但这使它变得困难让我告诉我是否采取了正确的方法,或者我做错了什么。 } }

QString body = "" ;

const QUrl url("http://someurl.to/make/the/post" );
 QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
 QHttpPart textPartData;
 body = "" ;
 QTextStream(&body) << " this is your text to post with the file";
 textPartData.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=utf-8");
 textPartData.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\""));
 textPartData.setBody(body.toAscii());

     for(int i=0; i< number_of_files_to_send ; i++){
             QHttpPart imagePart;
             QString header_body = "";
             QTextStream(&header_body) << "form-data; name=\"" << name_of_the_file << "\"; filename=\"" << name_of_the_file << ".jpg\"" ;
             imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(header_body));
             imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));

             //prepare file for upload
             QString filepath;
             QTextStream(&filepath) << FunkyPigeonBlackberry::getInstance()->dataModelProductDetails()->custom_image(i).value("cut_image").toString();

             // pack the new file
             QFile *file = new QFile(filepath);
             file->open(QIODevice::ReadOnly);
             imagePart.setBodyDevice(file);
             file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
             multiPart->append(imagePart);
     }

 multiPart->append(textPartData);
 multiPart->append(textPartProductDetails);
 multiPart->append(textPartDataDelivery);


 QNetworkRequest request(url);

 QNetworkReply* reply = m_networkAccessManager->post(request, multiPart);
 connect(reply, SIGNAL(finished()), this, SLOT(onNetworkResponse()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:如何在SharpGs客户端库中发出POST请求以在Google Cloud Storage中启动可恢复的上传? - C# : How to make a POST request in SharpGs client library to initiate resumable uploads in Google Cloud Storage? C#中XML文件的HTTP发布请求 - HTTP Post request of XML file in C# 如何在 xamarin 上发出 HTTP POST Web 请求? - How to make HTTP POST web request on xamarin? 如何在C#中发出POST请求 - how to make POST request in C# C# 应用程序在对 python http 服务器的 POST 请求期间挂起 - C# application hangs during POST request to python http server C# HTTP 服务器 POST 请求将正文解析为 null - C# HTTP Server POST request is parsing the body as null 在 C# 中,如何使用 POST 方法将数据从 SQL Server 发送到 HTTP 请求? - In C# how can I send data from SQL Server to a HTTP request using the POST method? 如何从C#服务器中的POST(或其他HTTP)请求访问参数? - How can I access parameters from a POST (or other HTTP) request in a C# server? 如何在给定以下HTTP + JSON的情况下使用窗口服务发出POST请求 - How to make a POST request with window service given the following HTTP + JSON C# HTTP 服务器 - 响应发布请求而不处理发布数据 - C# HTTP Server - Respond post request without processing post data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM