C++获取网页内容

最近帮做个小程序,获取指定的网页内容,其实这很好办。

第一种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 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下载,上传,断点续传等功能。

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值