Mryqu's Notes


  • 首页

  • 搜索
close

cURL错误处理

时间: 2016-01-07   |   分类: DataBuilder   C++     |   阅读: 58 字 ~1分钟
cURL执行错误分为两种: 通过curl_easy_perform函数执行请求结果,返回值不是CURLE_OK。错误信息除了可以对照CURLcode定义查看,也可以通过设置CURLOPT_ERRORBUFFER设置错误缓存区获得人类易读的错误文字信息。范例见https://curl.haxx.se/libcurl/c/CURLOPT_ERRORBUFFER.html curl = curl_easy_init(); if(curl) { CURLcode res; char errbuf[CURL_ERROR_SIZE]; curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); errbuf[0] = 0; res = curl_easy_perform(curl); if(res != CURLE_OK) { size_t len = strlen(errbuf); fprintf(stderr, "\nlibcurl: (%d) ", res); if(len) fprintf(stderr, "%s%s", errbuf, ((errbuf[len - 1] != '\n') ? "\n" : "")); else fprintf(stderr, "%s\n", curl_easy_strerror(res)); } } 另一种是curl_easy_perform返回CURLE_OK,但是HTTP响应代码为400及以上的整数。HTTP响应代码可以通过curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,&httpCode)获得错误消息需要通过curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,curlCallback)获得消息体后解析而得。

为cURL库设置HTTP代理的代码片段

时间: 2016-01-06   |   分类: DataBuilder   C++     |   阅读: 61 字 ~1分钟
在twitcurl看到cURL库设置http代理的方法,记录一下。 void twitCurl::prepareCurlProxy() { if( m_curlProxyParamsSet ) { return; } curl_easy_setopt( m_curlHandle, CURLOPT_PROXY, NULL ); curl_easy_setopt( m_curlHandle, CURLOPT_PROXYUSERPWD, NULL ); curl_easy_setopt( m_curlHandle, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY ); std::string proxyIpPort(""); if( getProxyServerIp().size() ) { utilMakeCurlParams( proxyIpPort, getProxyServerIp(), getProxyServerPort() ); } curl_easy_setopt( m_curlHandle, CURLOPT_PROXY, proxyIpPort.c_str() ); if( m_proxyUserName.length() && m_proxyPassword.length() ) { std::string proxyUserPass; utilMakeCurlParams( proxyUserPass,getProxyUserName(),getProxyPassword() ); curl_easy_setopt( m_curlHandle,CURLOPT_PROXYUSERPWD,proxyUserPass.c_str() ); } m_curlProxyParamsSet = true; }

Facebook Graph API合集

时间: 2016-01-04   |   分类: DataBuilder     |   阅读: 411 字 ~2分钟
常用URL笔记 获取Facebook主页Id https://graph.facebook.com/v2.5/SasSoftware?access_token={accessToken}&format=json 上面示例是通过主页名SasSoftware获取其主页Id。 获取Facebook主页帖子 https://graph.facebook.com/v2.5/{pageId}/feed?limit=100&format=json&include_hidden=true&access_token={accessToken}&since=2015-01-01&util=2015-12-31 https://graph.facebook.com/{pageId}/feed?limit=100&format=json&include_hidden=true&access_token={accessToken}&since=1420041660&util=1422634320 https://graph.facebook.com/v2.0/{pageId}/feed?limit=100&format=json&include_hidden=true&access_token={accessToken}&since=1420041660&util=1422634320 通过Facebook Graph API 2.5或不带版本的API仅能获取帖子的Id、创建时间和帖子内容,而FacebookGraph API 2.0则可以获得更多内容。 获取Facebook帖子的评论信息 https://graph.facebook.com/{postId}/comments?limit=100&format=json&include_hidden=true&access_token={accessToken} 获取Facebook帖子的点赞信息 https://graph.facebook.com/{postId}/likes?limit=100&format=json&include_hidden=true&summary=true&access_token={accessToken} 获取帖子订阅信息之limit参数 不同版本Facebook Graph API对获取帖子订阅信息 中limit参数要求不同: v2.0及以下版本没有说明 v2.1、v2.2和v2.3版本上限为250 v2.4和v2.5版本上限为100 处理Facebook API访问速率超限错误 对于下列Facebook通用错误,我个人觉的#2、#4、#9、#17、#18和#32错误都可以向客户端报告FacebookAPI访问速率超限,至于#5不确定。 Error number PHP Constant name Error description Generated by methods 2 API_EC_SERVICE Service temporarily unavailable (all) 4 API_EC_TOO_MANY_CALLS Application request limit reached (all) 5 API_EC_BAD_IP Unauthorized source IP address (all) 9 API_EC_RATE User is performing too many actions 17 API_EC_USER_TOO_MANY_CALLS User request limit reached 18 API_EC_REQUEST_RESOURCES_EXCEEDED This API call could not be completed due to resourcelimits 32 Page request limit reached 参考
阅读全文 »

[C++]用正则表达式检查日期格式yyyy-MM-dd

时间: 2015-12-31   |   分类: C++     |   阅读: 144 字 ~1分钟
写了一个小程序使用C++的正则表达式检查日期是否符合yyyy-MM-dd格式: 结果总是抛出exception,错误代码不是error_brack就是error_escape。检查了一下代码,没觉得不符合ECMAScript语法法则。 查了一下我的环境,用的gcc 4.7.0。试了一下regex_match的例子,没问题,但是稍微改动一下用\d{4}检查4位数字就又抛exception了。 C:\>g++ -v Using built-in specs. COLLECT_GCC=C:\quTools\Anaconda\Scripts\g++.bat\..\..\MinGW\bin\g++.exe COLLECT_LTO_WRAPPER=c:/qutools/anaconda/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.7.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: ../../../build/gcc/src/configure --target=x86_64-w64-mingw32 --prefix=/c/bb/vista64-mingw32/mingw-x86-x86_64/build/build/root --with-sysroot=/c/bb/vista64-mingw32/mingw-x86-x86_64/build/build/root --enable-languages=all,obj-c++ --enable-fully-dynamic-string --disable-multilib Thread model: win32 gcc version 4.7.0 20111220 (experimental) (GCC) 搜了一下,发现C++2011标准中的regex功能直到gcc 4.9.0才正式发布。啥也不说了,在Mingw-w64 Toolchains上直接下载个gcc 5.3.0试试,一切正常了 C:\ctest>g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=C:/tools/mingw32/bin/../libexec/gcc/i686-w64-mingw32/5.3.0/lto-wrapper.exe Target: i686-w64-mingw32 Configured with: ../../../src/gcc-5.3.0/configure --host=i686-w64-mingw32 --build=i686-w64-mingw32 --target=i686-w64-mingw32 --prefix=/mingw32 --with-sysroot=/c/mingw530/i686-530-posix-dwarf-rt_v4-rev0/mingw32 --with-gxx-include-dir=/mingw32/i686-w64-mingw32/include/c++ --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-sjlj-exceptions -- with-dwarf2 --disable-isl-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=i686 --with-tune=generic --with-libiconv --with-system-zlib --with-gmp=/c/mingw530/prerequisites/i686-w64-mingw32-static --with-mpfr=/c/mingw530/prerequisites/i686-w64-mingw32-static --with-mpc=/c/mingw530/prerequisites/i686-w64-mingw32-static --with-isl=/c/mingw530/prerequisites/i686-w64-mingw32-static --with-pkgversion='i686-posix-dwarf-rev0, Built by MinGW -W64 project' --with-bugurl=http://sourceforge.
阅读全文 »

解决使用twitcurl.lib遇到的LNK1112和LNK2038链接错误

时间: 2015-12-30   |   分类: DataBuilder   C++     |   阅读: 31 字 ~1分钟
在使用twitcurl.lib时,遭遇下列链接错误: fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64' libtwitcurl.lib(twitcurl.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in xxxxx.obj 解决方法:

[C++]玩玩Designated Initializer

时间: 2015-12-28   |   分类: C++     |   阅读: 46 字 ~1分钟
玩一把gcc的Designated Initializers: 测试结果:结构体内的变量必须按照声明的顺序初始化,并且不能遗漏,否则会报“sorry, unimplemented:non-trivial designated initializers not supported”错误。 参考 C99标准 Bug 55606 - sorry, unimplemented: non-trivial designated initializers not supported C++ - g++: sorry, unimplemented: non-trivial designated initializers not supported - SysTutorials QA http://stackoverflow.com/questions/31215971/non-trivial-designated-initializers-not-supported Non-trivial designated initializers not supported · Issue #8 · couchbaselabs/cbforest · GitHub

twitcurl生成HTTP OAuth头的实现流程

时间: 2015-12-27   |   分类: DataBuilder   C++     |   阅读: 106 字 ~1分钟
对twitcurl代码做了一些修改,结果遇到了认证失败的错误: {“errors”:[{“message”:”Could not authenticate you”,”code”:32}]} 通过继续修改twitcurl代码改正问题,学习了twitcurl的认证授权部分代码。其授权部分主要在oauthlib.h和oauthlib.cpp中的oAuth类实现中。下面主要分析一下oAuth::getOAuthHeader方法。 外部数据 Http URL: https://api.twitter.com/1.1/search/tweets.json Http头参数: |参数键|参数值 |—– |q|va |count|23 |result_type|recent Http授权参数: |参数键|参数值 |—– |oauth_consumer_key|xvz1evFS4wEEPTGEFPHBog |oauth_signature_method|HMAC-SHA1 |oauth_token|370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb |oauth_version|1.0 oAuth::getOAuthHeader方法 通过buildOAuthHttpParameterKeyValPairs(params, true,rawKeyValuePairs);对Http头参数中参数值进行百分号编码(URL编码),编码后结果放在哈希表rawKeyValuePairs中 rawKeyValuePairs: 键值qvacount23result_typerecent 假定HTTP内容是经过百分号编码的,通过buildOAuthRawDataKeyValPairs( rawData,false, rawKeyValuePairs );找到内容中的键值对,放入哈希表rawKeyValuePairs中 rawKeyValuePairs: 键值qvacount23result_typerecent 通过buildOAuthTokenKeyValuePairs( includeOAuthVerifierPin,std::string( "" ), rawKeyValuePairs, true );创建认授权证: rawKeyValuePairs: 键值说明qvacount23result_typerecentoauth_consumer_keyxvz1evFS4wEEPTGEFPHBogoauth_nonce131862295819ctwitcurl实现就是时戳项加一个随机数oauth_signature_methodHMAC-SHA1固定值oauth_timestamp1318622958oauth_token370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEboauth_version1.0固定值 通过getSignature( eType, pureUrl, rawKeyValuePairs,oauthSignature );获得签名 生成 sigBase: 使用consumer_secret和token_secret组成signing_key,使用HMAC_SHA1算法通过sigBase和signing_key生成摘要strDigest:B6 79 C0 AF 18 F4 E9 C5 87 AB 8E 20 0A CD 4E 48 A9 3F 8C B6(非真实计算而得数据) 通过base64_encode进行编码:tnnArxj06cWHq44gCs1OSKk/jLY= (非真实计算而得数据) 通过百分比编码获得最终签名: (非真实计算而得数据) 通过rawKeyValuePairs.
阅读全文 »

表情符号之Unicode和UTF-8编码

时间: 2015-12-26   |   分类: Tech     |   阅读: 31 字 ~1分钟
最近在玩表情符号,这个表情符号Unicode表格,还挺全: http://apps.timwhitlock.info/emoji/tables/unicode 此外也常用这个在线编码转换工具进行验证:http://tool.oschina.net/encode 表情符号的Unicode范围在Android - How to filter emoji (emoticons) from a string?有提到过: U+2190 to U+21FF U+2600 to U+26FF U+2700 to U+27BF U+3000 to U+303F U+1F300 to U+1F64F U+1F680 to U+1F6FF

[C++] 我服务器上的GCC版本不支持C++11特性

时间: 2015-12-25   |   分类: Tool   C++     |   阅读: 29 字 ~1分钟
用了点C++11特性,结果编译失败,编译参数加"-std=c++0x",结果识别不出来。 $ g++ -v Using built-in specs. Target: amd64-undermydesk-freebsd Configured with: FreeBSD/amd64 system compiler Thread model: posix gcc version 4.2.1 20070719 [FreeBSD] C++0x/C++11 Support in GCC提到GCC 4.3版本之后才支持C++11特性,白折腾一把! 好吧,我用gcc docker!

[C++]遭遇error C2039: 'min' : is not a member of 'std'

时间: 2015-12-22   |   分类: C++     |   阅读: 15 字 ~1分钟
使用Visual Studio2013编译twitcurl遭遇下列错误: error C2039: 'min' : is not a member of 'std' 解决方法: #include <algorithm>
13 14 15 16 17 18 19 20 21

Programmer & Architect

662 日志
27 分类
1472 标签
RSS 订阅
GitHub Twitter FB Page
© 2009 - 2023 Mryqu's Notes
Powered by - Hugo v0.120.4
Theme by - NexT
0%