肖锐(Cooki)个人原创,欢迎转载,转载请注明地址,肖锐(Cooki)的技术博客 http://blog.csdn.net/xiao0026
由于游戏用到了网络头像, 今天发现换成facebook登陆后头像下载失败, 2dx默认的没有支持https, 于是找到 network/
HttpClient.cpp
[cpp] view plaincopy
[cpp] view plaincopy
- int processGetTask(CCHttpRequest *request, write_callback callback, void *stream, int *responseCode)
- {
- CURLcode code = CURL_LAST;
- CURL *curl = curl_easy_init();
- do {
- if (!configureCURL(curl))
- {
- break;
- }
- /* handle custom header data */
- /* create curl linked list */
- struct curl_slist *cHeaders=NULL;
- /* get custom header data (if set) */
- std::vector<std::string> headers=request->getHeaders();
- if(!headers.empty())
- {
- for(std::vector<std::string>::iterator it=headers.begin();it!=headers.end();it++)
- {
- /* append custom headers one by one */
- cHeaders=curl_slist_append(cHeaders,it->c_str());
- }
- /* set custom headers for curl */
- code = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, cHeaders);
- if (code != CURLE_OK) {
- break;
- }
- }
- code = curl_easy_setopt(curl, CURLOPT_URL, request->getUrl());
- if (code != CURLE_OK)
- {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, request->getUrl());
- if (code != CURLE_OK)
- {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
- if (code != CURLE_OK)
- {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, stream);
- if (code != CURLE_OK)
- {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
- if (code != CURLE_OK) {
- break;
- }
- code = curl_easy_perform(curl);
- if (code != CURLE_OK)
- {
- break;
- }
- /* free the linked list for header data */
- curl_slist_free_all(cHeaders);
- code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, responseCode);
- if (code != CURLE_OK || *responseCode != 200)
- {
- code = CURLE_HTTP_RETURNED_ERROR;
- }
- } while (0);
- if (curl) {
- curl_easy_cleanup(curl);
- }
- return (code == CURLE_OK ? 0 : 1);
- }
- //Process POST Request
- int processPostTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *responseCode)
- {
- CURLcode code = CURL_LAST;
- CURL *curl = curl_easy_init();
- do {
- if (!configureCURL(curl)) {
- break;
- }
- /* handle custom header data */
- /* create curl linked list */
- struct curl_slist *cHeaders=NULL;
- /* get custom header data (if set) */
- std::vector<std::string> headers=request->getHeaders();
- if(!headers.empty())
- {
- for(std::vector<std::string>::iterator it=headers.begin();it!=headers.end();it++)
- {
- /* append custom headers one by one */
- cHeaders=curl_slist_append(cHeaders,it->c_str());
- }
- /* set custom headers for curl */
- code = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, cHeaders);
- if (code != CURLE_OK) {
- break;
- }
- }
- code = curl_easy_setopt(curl, CURLOPT_URL, request->getUrl());
- if (code != CURLE_OK) {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
- if (code != CURLE_OK) {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, stream);
- if (code != CURLE_OK) {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_POST, 1);
- if (code != CURLE_OK) {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
- if (code != CURLE_OK) {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request->getRequestData());
- if (code != CURLE_OK) {
- break;
- }
- code = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, request->getRequestDataSize());
- if (code != CURLE_OK) {
- break;
- }
- code = curl_easy_perform(curl);
- if (code != CURLE_OK) {
- break;
- }
- /* free the linked list for header data */
- curl_slist_free_all(cHeaders);
- code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, responseCode);
- if (code != CURLE_OK || *responseCode != 200) {
- code = CURLE_HTTP_RETURNED_ERROR;
- }
- } while (0);
- if (curl) {
- curl_easy_cleanup(curl);
- }
- return (code == CURLE_OK ? 0 : 1);
- }
[cpp] view plaincopy
- code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
- if (code != CURLE_OK) {
- break;
- }
[cpp] view plaincopy
- 加入这一段curl代码, 即可支持解析https解析,不管是get还是post都可
时间: 2024-10-13 21:57:42