• c++使用http协议上传文件到七牛云服务器


    使用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 += "
    ";
    	str += "--" + boundary;
    	str += "
    ";
    	str += "Content-Disposition:form-data; name="token"";
    	str += "
    
    ";
        str += token;
    	str += "
    ";
    
    	// key
    	str += "--" + boundary;
    	str += "
    ";
    	str += "Content-Disposition:form-data; name="key"";
    	str += "
    
    ";
    	str += key;
    	
    	std::string strdata = strBin;
    	str += "
    --" + boundary + "
    ";
    	str = str + "Content-Disposition:form-data; name="file"; filename="" + key + ""
    ";
    	str = str + "Content-Type:application/octet-stream
    ";
    	str = str + "Content-Transfer-Encoding: binary
    
    ";
    	str = str + strBin;
    	str = str + "
    --" + boundary + "--
    ";
    	
    	
    	req->setRequestData(str.data(), str.size());
    
    
    	CCLOG("req data:%s", req->getRequestData());
    	CCLOG("str data = %s 
     str .size = %lu 
    ", 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;
    //}
    
    
        
    

      

  • 相关阅读:
    CSS之各种居中
    三步教会你装系统
    65条最常用正则表达式
    MongoDB介绍
    MongoDB基本命令用
    log4j配置
    使用spring + ActiveMQ 总结
    log4j配置文件
    如何入侵局域网电脑
    目标检测的图像特征提取
  • 原文地址:https://www.cnblogs.com/JD85/p/8455205.html
Copyright © 2020-2023  润新知