C语言如何接品易海外HTTP

技术支持发布日期:2021-07-13

image.png

定义控制台应用程序的入口点:


#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)

{

    //设置http代理地址

curl_easy_setopt(curl, CURLOPT_PROXY,"http://测试ip:端口");

    //设置读写缓存

curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buff)

     //设置回调函数

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_buff_data);

    //设置url地址

curl_easy_setopt(curl, CURLOPT_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://测试ip:端口");//设置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);//设置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://baidu.com", buff);

printf("不使用代理:%s\n", buff);

                           

使用http代理

   

memset(buff, 0, 1024 * 1024);

GetUrlHTTP("http://baidu.com", buff);

printf("http结果:%s\n", buff);

//使用socks5代理

memset(buff, 0,1024 * 1024);

GetUrlSocks5("http://baidu.com", buff);

printf("socks5结果:%s\n", buff);



Sleep(1000 * 1000);

free(buff);

    return 0;

}

                 

 重点:


C语言客户,参考如下demo案例:


C语言下载教程


挂件 关闭
客服
二维码
客服二维码

加微信 领流量

大客户经理二维码

售前咨询,企业定制

专属客服竭诚为您服务