使用c++ http协议上传文件到七牛服务器时,比较搞的一点就是header的设置:
"Content-Type:multipart/form-data;boundary=xxx"
////////////// HttpUpload.h //////////// #include "cocos2d.h" #include "network/HttpClient.h" using namespace cocos2d; using namespace std; class uploadFile { public: static uploadFile *m_inst; static uploadFile *GetInst(); void UpLoadFile(string photoPath,string key,string token); void onHttpRequestCompleted(network::HttpClient* client, network::HttpResponse* response); // static size_t write_data(uint8_t *dataBack, size_t size, size_t nmemb, void *userp); }; ///////////////////////// HttpUpload.cpp //////////////////// #include "HttpUpload.h" uploadFile* uploadFile::m_inst = NULL; uploadFile* uploadFile::GetInst() { if (!m_inst) { m_inst = new uploadFile(); return m_inst; } return NULL; } void uploadFile::onHttpRequestCompleted(network::HttpClient* client, network::HttpResponse* response) { int result = 0; if (!response->isSucceed()) { CCLOG("error"); CCLOG("error buffer: %s", response->getErrorBuffer()); CCLOG("error code: %d", (int)response->getResponseCode()); //CCLOG("resp: %s", response->getResponseData()); std::vector<char> *buffer = response->getResponseData(); std::string errMsg = ""; for (vector<char>::iterator iter = buffer->begin(); iter != buffer->end(); ++iter) { errMsg += *iter; } CCLOG("errMsg: %s",errMsg.c_str()); } else { result = 1; CCLOG("upload success"); // std::vector<char>* buffer = response->getResponseData(); // std::vector<char>* header = response->getResponseHeader(); // auto data = std::string(buffer->begin(), buffer->end()); // auto headerData = std::string(header->begin(), header->end()); // CCLOG("responseData %s", data.c_str()); // CCLOG("responseHeader %s", headerData.c_str()); } EventCustom event("upload_result"); event.setUserData(&result); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); }; void uploadFile::UpLoadFile(string photoPath,string key,string token) { network::HttpClient* http = network::HttpClient::getInstance(); network::HttpRequest* req = new network::HttpRequest; req->setRequestType(network::HttpRequest::Type::POST); req->setUrl("http://up.qiniu.com"); req->setResponseCallback(CC_CALLBACK_2(uploadFile::onHttpRequestCompleted, this)); std::string pathKey = photoPath; Data imgdata = FileUtils::getInstance()->getDataFromFile(pathKey); // long buff = 0; // unsigned char * pBuffer = FileUtils::sharedFileUtils()->getFileData(pathKey.c_str(), "rb", &buff); // const char* fileBinary = (const char*)pBuffer; // std::string strBin = std::string(fileBinary, buff); cocos2d:Data fileData = FileUtils::getInstance()->getDataFromFile(pathKey); std::string strBin = std::string((const char*)fileData.getBytes(), fileData.getSize()); std::string boundary = "----------------WebKitFormBou3123ndaryEm5WNw6hGiQUBpng"; std::vector<std::string> headers; headers.push_back("Content-Type:multipart/form-data;boundary=" + boundary); req->setHeaders(headers); std::string str = ""; // token str += "\r\n"; str += "--" + boundary; str += "\r\n"; str += "Content-Disposition:form-data; name=\"token\""; str += "\r\n\r\n"; str += token; str += "\r\n"; // key str += "--" + boundary; str += "\r\n"; str += "Content-Disposition:form-data; name=\"key\""; str += "\r\n\r\n"; str += key; std::string strdata = strBin; str += "\r\n--" + boundary + "\r\n"; str = str + "Content-Disposition:form-data; name=\"file\"; filename=\"" + key + "\"\r\n"; str = str + "Content-Type:application/octet-stream\r\n"; str = str + "Content-Transfer-Encoding: binary\r\n\r\n"; str = str + strBin; str = str + "\r\n--" + boundary + "--\r\n"; req->setRequestData(str.data(), str.size()); CCLOG("req data:%s", req->getRequestData()); CCLOG("str data = %s \n str .size = %lu \n", str.data(), str.size()); http->send(req); req->release(); } //size_t uploadFile::write_data(uint8_t *dataBack, size_t size, size_t nmemb, void *user_p) //{ // string szData = string((char*)dataBack); // // return 0; //}