关于使用curl下载网页源码中文乱码问题!
来源:互联网 发布:上古卷轴5战斗优化mod 编辑:程序博客网 时间:2024/06/05 17:21
关于使用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 代码如下:
/*************************************************************************** * _ _ ____ _ * 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路径目录,库目录。
0 0
- 关于使用curl下载网页源码中文乱码问题!
- 关于使用libcurl下载网页源码中文乱码问题!
- php使用curl发送url中文乱码问题
- C++使用curl库发送https请求中文乱码问题
- 网页中文乱码问题
- 关于response中设置下载文件名中文乱码问题
- 关于中文乱码问题
- 关于中文乱码问题
- 关于中文乱码问题
- 网页输出中文乱码问题
- 使用WebBrowser获取网页源代码,解决中文乱码问题
- 使用WebBrowser获取网页源代码,解决中文乱码问题
- 使用WebStorm开发web前端---- 网页中文乱码问题
- 关于curl 采集时候乱码的问题
- 文件下载中文乱码问题
- 下载中文文件乱码问题
- 下载中文文件名乱码问题
- 中文文件名下载乱码问题
- 电力系统安全区划分
- QProgressBar水平、垂直样式表两种设置方式
- C++11特性
- CentOS搭建dubbo-admin管理平台
- [LeetCode]169 Majority Element
- 关于使用curl下载网页源码中文乱码问题!
- Android studio适配6.0的一些小问题
- Spark学习笔记00
- 基于WiFi调试Android应用
- ==与equals
- 使用Maven自动部署Java Web应用到Tomcat服务器
- 二阶段提交,三阶段提交,Paxos
- Kylin Cube构建过程优化
- sql 事务