关于使用curl下载网页源码中文乱码问题!
2017-02-08 10:22
615 查看
关于使用libcurl下载网页源码中文乱码问题!
参考了这位兄弟的:http://blog.csdn.net/malihong1/article/details/50480420,可能他没继续找到方法。
借用了http://www.cnblogs.com/iRoad/p/4105172.html的函数
直接改官网的demo https://curl.haxx.se/libcurl/c/example.html,https.c 代码如下:
只需要经.c后缀改为cpp后缀就可以了。
我下载的curl:
官网下载curl,编译后(curl\curl-7.52.1\projects\Windows\下有win项目的解决方案),在这个目录下curl\curl-7.52.1\builds\libcurl-vc10-x86-release-dll-ipv6-sspi-winssl\ 存在三个文件夹
--bin
--curl.exe
--libcurl.dll(需要拷贝到运行目录)
include
--curl(头文件目录)
lib
--libcurl.lib
其他没怎么配置,项目需要配置引用curl路径目录,库目录。
参考了这位兄弟的:http://blog.csdn.net/malihong1/article/details/50480420,可能他没继续找到方法。
借用了http://www.cnblogs.com/iRoad/p/4105172.html的函数
直接改官网的demo https://curl.haxx.se/libcurl/c/example.html,https.c 代码如下:
/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at https://curl.haxx.se/docs/copyright.html. * * You may opt to use, copy, modify, merge, publish, distribute and/or sell * copies of the Software, and permit persons to whom the Software is * furnished to do so, under the terms of the COPYING file. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ***************************************************************************/ /* <DESC> * Simple HTTPS GET * </DESC> */ // C 运行时头文件 #include <atlstr.h> #include <WinINet.h> #include <string> #include <stdio.h> #include <curl/curl.h> using namespace std; string UTF8ToGBK(const std::string& strUTF8) { int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0); WCHAR* wszGBK = new WCHAR[len+1]; memset(wszGBK, 0, len * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(LPCTSTR)strUTF8.c_str(), -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); char *szGBK = new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL); std::string strTemp(szGBK); delete[]szGBK; delete[]wszGBK; return strTemp; } /** @brief char*数据接收回调函数,仅适用stream实际类型是std::string */ static size_t DataReceiveCallback(void *ptr, size_t size, size_t nmemb, void *stream) { size_t len = size * nmemb; std::string* buffer = reinterpret_cast<std::string*>(stream); if (buffer) buffer->append((char*)ptr, len); return len; } int main(void) { CURL *curl; CURLcode res; std::string data; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://v.qq.com/x/cover/jwplwx9ootoigud.html"); #ifdef SKIP_PEER_VERIFICATION /* * If you want to connect to a site who isn't using a certificate that is * signed by one of the certs in the CA bundle you have, you can skip the * verification of the server's certificate. This makes the connection * A LOT LESS SECURE. * * If you have a CA cert for the server stored someplace else than in the * default bundle, then the CURLOPT_CAPATH option might come handy for * you. */ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); #endif #ifdef SKIP_HOSTNAME_VERIFICATION /* * If the site you're connecting to uses a different host name that what * they have mentioned in their server certificate's commonName (or * subjectAltName) fields, libcurl will refuse to connect. You can skip * this check, but this will make the connection less secure. */ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); #endif // 响应数据回调 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, DataReceiveCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); string strGBK = UTF8ToGBK(data); /* always cleanup */ curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; }
另外因为使用了atl,会报错错误
1 error C1189: #error : ATL requires C++ compilation (use a .cpp suffix)c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\atlstr.h16
只需要经.c后缀改为cpp后缀就可以了。我下载的curl:
官网下载curl,编译后(curl\curl-7.52.1\projects\Windows\下有win项目的解决方案),在这个目录下curl\curl-7.52.1\builds\libcurl-vc10-x86-release-dll-ipv6-sspi-winssl\ 存在三个文件夹
--bin
--curl.exe
--libcurl.dll(需要拷贝到运行目录)
include
--curl(头文件目录)
lib
--libcurl.lib
其他没怎么配置,项目需要配置引用curl路径目录,库目录。
相关文章推荐
- 关于使用curl下载网页源码中文乱码问题!
- 关于使用libcurl下载网页源码中文乱码问题!
- 关于使用ZXing扫描二维码出现中文乱码的问题
- 关于response中设置下载文件名中文乱码问题
- 关于Android使用HttpURLConnection传输含有中文JSON数据的报错、乱码问题
- 关于使用 ASIHTTPRequest URL中文参数乱码问题
- 关于使用 ASIHTTPRequest URL中文参数乱码问题
- 关于使用java自带ZipOutputStream压缩文件名为中文的乱码问题
- php使用curl发送url中文乱码问题
- 关于Struts2使用redirectAction方式在两个Action之间传值时中文乱码问题的解决方法
- 关于使用IDEA读取txt文件出现中文乱码的问题
- 关于使用response.addHeader下载中文名乱码问题
- 使用ZipEntry进行图片压缩下载,文件夹中文乱码问题
- 关于在MySQL 、 VC、 JSP 中使用UTF-8解决中文生僻字乱码的问题
- 关于使用HttpWebRequest和HttpWebResponse时中文乱码问题
- 关于使用Ajax传中文数据到后台接受乱码问题 data:{"dictype":"客户信誉度"}
- 关于客户端与服务器ajax使用json数据交互,出现中文乱码或因包含html标答而报错的问题(base64加密)
- 关于使用中文分词工具ICTCLAS2013 Java版本乱码的问题
- 关于使用response.addHeader下载中文名乱码问题
- 关于使用httpclient传输json信息并解决中文乱码问题