curl 探测java网站_利用cURL来获取网页信息-Using cURL to get webpage content

#include

#include

#include

#include

#include

extern "C" {

#include

//#include

#include

}

using namespace std;

int writer(char* data, size_t size, size_t nmemb, string *buffer) {

fprintf(stderr, "Hello, I am a function pointer\n");

int result = 0;

if (buffer != NULL) {

buffer->append(data, size * nmemb);

result = size * nmemb;

}

return result;

}

int main(int argc, char** args) {

CURL* curl; // That is the connection, see : curl_easy_init

CURLcode res; // Not needed, see : curl_easy_cleanup

string buffer; // see: CURLOPT_WRITEDATA

curl = curl_easy_init(); // Initilize web query

if (curl) {

// set options for web query

curl_easy_setopt(curl, CURLOPT_URL, args[1]);

curl_easy_setopt(curl, CURLOPT_HEADER, 0); // No we don't need the header of the web content, Set to 0 and

// curl ignores the first line

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);//Don't follow anything else than the particular url requested

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); // Function Pointer 'writer' manages the required buffer size

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); // Data Pointer &buffer stores downloaded web content

} else {

fprintf(stderr, "Error 1\n");

return 0;

}

res = curl_easy_perform(curl);

//string s = (res);

cout << res << endl;

curl_easy_cleanup(curl);

std::istringstream iss(buffer);

string line, item;

int linenum = 0;

while (getline(iss, line)) {

linenum ++;

cout << "\nline #" << linenum << " : " << endl;

std::istringstream linestream(line);

int itemnum = 0;

while (getline(linestream, item, ',')) {

itemnum ++;

cout << "Item # " << itemnum << " : " << item << endl;

}

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值