C/C++语言如何接入代码Demo

以下分别展示API模式、账密认证模式下的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示例

// demo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "curl/curl.h"
#pragma comment(lib, "libcurl.lib")
//curl 回调函数
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://代理服务器地址:端口");//设置http代理地址
        curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "认证账户名:认证账户密码");//认证账户名及密码,以":"分隔
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buff);//设置读写缓存
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_buff_data);//设置回调函数
        curl_easy_setopt(curl, CURLOPT_URL, url);//设置url地址
        curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10L);//设置一个长整形数,控制多少秒传送CURLOPT_LOW_SPEED_LIMIT规定的字节数
        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://代理服务器地址:端口");//设置socks5代理地址
        curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "认证账户名:认证账户密码");//认证账户名及密码,以":"分隔
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buff);//设置读写缓存
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_buff_data);//设置回调函数
        curl_easy_setopt(curl, CURLOPT_URL, url);//设置url地址
        curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10L);//设置一个长整形数,控制多少秒传送CURLOPT_LOW_SPEED_LIMIT规定的字节数
        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库
    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);//设置url地址
        curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10L);//设置一个长整形数,控制多少秒传送CURLOPT_LOW_SPEED_LIMIT规定的字节数
        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);
    //不使用http代理
    GetUrl("http://myip.top", buff);
    printf("不使用代理:%s\n", buff);
    //使用http代理
    memset(buff, 0, 1024 * 1024);
    GetUrlHTTP("http://ipinfo.io", buff);
    printf("http结果:%s\n", buff);
    //使用socks5代理
    memset(buff, 0,1024 * 1024);
    GetUrlSocks5("http://ipinfo.io", buff);
    printf("socks5结果:%s\n", buff);

    free(buff);
    Sleep(10 * 1000);//等待10秒退出
   
    return 0;
}