zoukankan      html  css  js  c++  java
  • 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;
    //}
    
    
        
    

      

  • 相关阅读:
    leetcode 33. Search in Rotated Sorted Array
    leetcode 32. Longest Valid Parentheses
    leetcode 28. Implement strStr()
    leetcode 27. Remove Element
    leetcode 26. Remove Duplicates from Sorted Array
    leetcode 24. Swap Nodes in Pairs
    leetcode 22. Generate Parentheses
    树莓派的频率管理和热控制
    sql执行insert插入一条记录同时获取刚插入的id
    全程直播个人博客重构过程,采用springboot+dubbo+jpa技术栈。
  • 原文地址:https://www.cnblogs.com/JD85/p/8455205.html
Copyright © 2011-2022 走看看