C/C++语言如何接入代码Demo
一、API教程demo示例
// demo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "curl/curl.h"
#pragma comment(lib, "libcurl.lib")
//在CURLOPT_WRITEFUNCTION设置属性下,使用回调write_buff_data进行处理
static size_t write_buff_data(char *buffer, size_t size, size_t nitems, void *outstream)
{
memcpy(outstream, buffer, nitems*size);
return nitems*size;
}
/*
使用http代理
*/
int GetUrlHTTP(char *url, char *buff)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_PROXY,"http://代理服务器地址:端口");//设置代理
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buff);//void* buff 将会传递给回调函数write_buff_data的第四个参数 void* outstream
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_buff_data);//在CURLOPT_WRITEFUNCTION设置属性下,使用回调write_buff_data进行处理
curl_easy_setopt(curl, CURLOPT_URL, url);//设置访问的域名
/* 如果在10秒内速度低于50个字节/秒,则中止 */
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10L);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 50L);
curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, 2000000L);/*下载最高速度*/
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK){
return res;
}else {
printf("错误代码:%d\n", res);
MessageBox(NULL, TEXT("获取IP错误"), TEXT("助手"), MB_ICONINFORMATION | MB_YESNO);
}
}
return res;
}
/*
使用socks5代理
*/
int GetUrlSocks5(char *url, char *buff)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_PROXY, "socks5://代理服务器地址:端口");//设置代理
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buff);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_buff_data);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10L);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 50L);
curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, 2000000L);/*下载最高速度*/
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
return res;
}
else {
printf("错误代码:%d\n", res);
MessageBox(NULL, TEXT("获取IP错误"), TEXT("助手"), MB_ICONINFORMATION | MB_YESNO);
}
}
return res;
}
/*
不使用代理
*/
int GetUrl(char *url, char *buff)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buff);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_buff_data);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10L);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 50L);
curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, 2000000L);/*下载最高速度*/
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK)
{
return res;
}
else {
printf("错误代码:%d\n", res);
MessageBox(NULL, TEXT("获取IP错误"), TEXT("助手"), MB_ICONINFORMATION | MB_YESNO);
}
}
return res;
}
int main()
{
char *buff=(char*)malloc(1024*1024);
memset(buff, 0, 1024 * 1024);
GetUrl("http://baidu.com", buff);
printf("不使用代理:%s\n", buff);
memset(buff, 0, 1024 * 1024);
GetUrlHTTP("http://baidu.com", buff);
printf("http结果:%s\n", buff);
memset(buff, 0,1024 * 1024);
GetUrlSocks5("http://baidu.com", buff);
printf("socks5结果:%s\n", buff);
free(buff);
Sleep(10 * 1000);//等待10秒退出
return 0;
}
二、账密认证教程demo示例
最后更新于