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

使用c++ http协议上传文件到七牛服务器时,比较搞的一点就是header的设置:

"Content-Type:multipart/form-data;boundary=xxx"

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
////////////// 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;
//}
 
 
    

  

posted @   居家懒人  阅读(4119)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示