1.需求
做一个批量数字签名的工具,从一个文件夹内寻找所有的exe文件,然后复制到另外一个文件夹中,并且保存源文件的路径,将目标文件夹内的所有exe文件上传到公司的数字签名平台去做数字签名,并且返回接收后的文件;
尝试了libcurl的post请求,发现总是返回参数错误;
int HttpClient::PostFile(std::string strFile) { std::string strurl = "http://10.1.13.228/sign"; int iret = -1; struct curl_httppost *formpost = NULL; struct curl_httppost *lastptr = NULL; QFileInfo fileinfo(strFile.c_str()); std::string strFilePath = fileinfo.absolutePath().toStdString(); std::string strFileName = fileinfo.fileName().toStdString(); QFile file(strFile.c_str()); int filesize = file.size(); QByteArray pBuffer = file.readAll(); int buffsize = pBuffer.size(); if (pBuffer.size() != fileinfo.size()) { printf("read file failed\n"); return -iret; } /* */ //curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "upfile", CURLFORM_FILE, strFile.c_str(), CURLFORM_CONTENTTYPE, "text/xml", CURLFORM_END); //curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_COPYCONTENTS, strFileName.c_str(), CURLFORM_END); //curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "action", CURLFORM_COPYCONTENTS, "upload", CURLFORM_END); CURL* curl = curl_easy_init(); //curl_formadd(&formpost, &lastptr, // CURLFORM_COPYNAME, "uploadfile", // CURLFORM_FILE, strFilePath.c_str(),//imagepath // CURLFORM_CONTENTTYPE, "multipart/form-data", // CURLFORM_END); //curl_formadd(&formpost, &lastptr, // CURLFORM_COPYNAME, "filename", // CURLFORM_COPYCONTENTS, strFileName.c_str(), // CURLFORM_END); if (curl) { /*curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_BUFFER, strFileName.c_str(), CURLFORM_BUFFERPTR, pReadBuffer, CURLFORM_BUFFERLENGTH, fileinfo.size(), CURLFORM_CONTENTTYPE, "application/octet-stream", CURLFORM_END);*/ /*curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "fileUpload", CURLFORM_FILE, strFile.c_str(), CURLFORM_END);*/ curl_formadd(&formpost, &lastptr, CURLFORM_PTRNAME, "reqformat", CURLFORM_PTRCONTENTS, "plain", CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_PTRNAME, "file", CURLFORM_PTRCONTENTS, pBuffer, CURLFORM_CONTENTSLENGTH, pBuffer.size(), CURLFORM_END); ///* Fill in the filename field */ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_COPYCONTENTS, strFileName.c_str(), CURLFORM_END); //curl_formadd(&formpost, &lastptr, CURLFORM_PTRNAME, "file", CURLFORM_FILE, strFile.c_str(), CURLFORM_END); /* Fill in the submit field too, even if this is rarely needed */ curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); //curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5); // 重定向次数 ///curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);//设置301、302跳转跟随location curl_easy_setopt(curl, CURLOPT_URL, strurl.c_str()); //curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);// 超时10秒 if (0 == strncmp(strurl.c_str(), "https:", 6)) { curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); } struct curl_slist *headers = NULL; std::string strfiletext = "file:@" + strFile; headers = curl_slist_append(headers, "Content-Type:multipart/form-data"); headers = curl_slist_append(headers, strfiletext.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); char* raw_parame = ""; //char* escap_param = curl_escape(raw_parame, strlen(raw_parame)); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, raw_parame); /* 长参数一定要设置CURLOPT_POSTFIELDSIZE,否则会截断 */ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(raw_parame)); FILE *pagefile; //写入到文件 QString strOutPath = fileinfo.absolutePath().toStdString().c_str(); strOutPath+="/output/"; QDir dir(strOutPath); if (!dir.exists()) { dir.mkpath((strOutPath)); } std::string strSigniturFile = fileinfo.absolutePath().toStdString() + "/output/" + strFileName; QFile Outputfileinfo(strSigniturFile.c_str()); if (Outputfileinfo.exists()) { Outputfileinfo.remove(); } pagefile = fopen(strSigniturFile.c_str(), "wb"); if (pagefile) { curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteFile); curl_easy_setopt(curl, CURLOPT_WRITEDATA, pagefile); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } fclose(pagefile); printf("************************************\n"); } else { printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); } if (curl) { curl_easy_cleanup(curl); } if (headers) { } } return iret; }
发现总是包参数错误;
后来发现可以curl加命令参数的方式上传程序文件去签名;前提是电脑必须安装了curl,可以打开window程序cmd.exe,然后查看curl --version 查看本地电脑的curl版本;
#include "UploadFileThread.h" #include <QProcess> UploadFileThread::UploadFileThread() { } UploadFileThread::~UploadFileThread() { } void UploadFileThread::SetFiles(QMap<QString, QString> taskInfo) { m_taskInfo = taskInfo; start(); } void UploadFileThread::run() { int s = 0; for (auto is = m_taskInfo.begin(); is != m_taskInfo.end(); is++) { QString strPath = is.key(); strPath = strPath.replace("/", "\\"); QStringList strParameter; //strParameter << "curl"; strParameter << "-F"; strParameter << "fd=sha256"; strParameter << "-F"; strParameter << ("file=@"+ strPath); strParameter << "-F"; strParameter << "cert_type=EV"; strParameter << "http://109.1.13.228"; strParameter << "-k"; strParameter << "-f"; strParameter << "-o"; strParameter << strPath; QProcess process(this); process.execute("curl", strParameter); process.waitForStarted(); process.waitForFinished(); s++; emit signalProgress(int(100.0*(s) / m_taskInfo.size())); } }
标签:文件,Qt,CURLFORM,QProcess,str,easy,curl,CURLOPT,setopt From: https://www.cnblogs.com/bclshuai/p/17985181