最近帮做个小程序,获取指定的网页内容,其实这很好办。
第一种windows平台可以使用MFC自带的库(别人要求要用windows平台),使用libcurl配置起来麻烦,第二种linux平台直接使用强大的libcurl,linux很容易使用libcurl。
先说第一种windows平台情况那个:网上找了代码,使用了MFC的库,在控制台下使用需要修改多字节集:
#include <stdio.h>
#include <afxinet.h>
int main()
{
CInternetSession session("HttpClient");
char * url = "http://www.baidu.com";
CHttpFile *pfile = (CHttpFile *)session.OpenURL(url);
DWORD dwStatusCode;
pfile->QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
CString content;
CString data;
while (pfile->ReadString(data))
{
content += data + "\r\n";
}
content.TrimRight();
printf(" %s\n ", content);
}
pfile->Close();
delete pfile;
session.Close();
return 0 ;
}
第二种情况linux下直接使用libcurl:
1.首先在linux下下载libcurl,并且解压:
# wget https://curl.haxx.se/download/curl-7.54.0.tar.gz
# tar -zxf curl-7.54.0.tar.gz
2.进入解压后的目录内,安装:
# cd cd curl-7.54.0/
# ./configure
# make
# make install
3.使用如下命令查看libcurl版本:
# curl --version
这样就安装好了。
如下就是如何简单的使用获取网页内容:
test.cpp
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
using namespace std;
static size_t downloadCallback(void *buffer, size_t sz, size_t nmemb, void *writer)
{
string* psResponse = (string*) writer;
size_t size = sz * nmemb;
psResponse->append((char*) buffer, size);
return sz * nmemb;
}
int main()
{
string strUrl = "http://www.baidu.com";
string strTmpStr;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strTmpStr);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
string strRsp;
if (res != CURLE_OK)
{
strRsp = "error";
}
else
{
strRsp = strTmpStr;
}
printf("strRsp is |%s|\n", strRsp.c_str());
return 0;
}
使用如下命令编译,并运行:
# g++ -o http test.cpp -lcurl
# ./http
libcurl非常强大,这里只是获取了网页信息,以前刚开始工作的时候用过libcurl下载,上传,断点续传等功能。