栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

如何在c语言代码中使用HTTP代理IP,示例代码demo直接可用(步骤注释清晰)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在c语言代码中使用HTTP代理IP,示例代码demo直接可用(步骤注释清晰)

如何在c语言代码中使用HTTP代理IP。

以下代码主要围绕第一次接触HTTP代理IP的c新手来写(步骤注释清晰)。

直接把下面示例代码中的HTTP代理API,替换成你后台生成的代理API链接,就可以跑起来了。

以下是一个示例代码,只是一个基础的演示,具体的代码还是要根据你业务的实际情况去写的。

示例代码中的HTTP代理IP,我使用的是华益云的HTTP代理API,注册就白嫖1万个高匿爬虫IP,有效期是一年,对于调试代码来说这个时间是非常的友好。

打开代理API,获取里面的IP,使用IP访问目标网站,其实代码中就是执行这个过程而已,然后加了几个错误判断有助于代码的稳定运行。(步骤注释清晰)

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

#include "stdafx.h"
#include "curl/curl.h"
#include 
#include 
#pragma comment(lib, "libcurl.lib")
static size_t write_buff_data(char *buffer, size_t size, size_t nitems, void *outstream)
{
	memcpy(outstream, buffer, nitems*size);
	return nitems*size;
}



int GetUrlHTTP(char *url, char *buff, char* ip)
{
	CURL *curl;
	CURLcode res;
	curl = curl_easy_init();
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_PROXY, ip); //代理方式 http://ip:port
		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("错误代码:%dn", 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("错误代码:%dn", res);
			MessageBox(NULL, TEXT("获取IP错误"), TEXT("助手"), MB_ICONINFORMATION | MB_YESNO);
		}
	}

	return res;
}

//将utf-8转为gbk格式
void utf8ToGbk(char *utf8String, char *gbkString)
{
	wchar_t *unicodeStr = NULL;
	int nRetLen = 0;

	nRetLen = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, NULL, 0);//求需求的宽字符数大小
	unicodeStr = (wchar_t *)malloc(nRetLen * sizeof(wchar_t));
	nRetLen = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, unicodeStr, nRetLen);//将utf-8编码转换成unicode编码
	nRetLen = WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, NULL, 0, NULL, 0);//求转换所需字节数
	nRetLen = WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, gbkString, nRetLen, NULL, 0);//unicode编码转换成gbk编码
	free(unicodeStr);
}


int main()
{

	char *buff = (char*)malloc(1024 * 1024);
	memset(buff, 0, 1024 * 1024);
	
	//代理api(直接百度搜索:华益云HTTP代理IP联系客服获取代理API)
	GetUrl("http://http.9vps.com/getip.asp?username=166xxxx6597&pwd=xxxxbaa59ce237dff65134984b9cxxxx&geshi=1&fenge=1&fengefu=&Contenttype=1&getnum=20&setcity=&operate=all&", buff); 
	printf("代理IP列表:n%s", buff); //输出

	//输入代理IP
	char ip[100];
	printf("nEnter your IP:");
	scanf_s("%s", &ip, 100);

	//使用代理IP访问网站
	memset(buff, 0, 1024 * 1024);
	GetUrlHTTP("http://myip.top", buff, (char*)ip);
	utf8ToGbk(buff, buff);//将获取到的网页内容转为gbk格式
	printf("nhttp结果:%sn", buff);//输出访问结果


	//不使用代理访问
	GetUrl("http://myip.top", buff);
	utf8ToGbk(buff, buff); // 将获取到的网页内容转为gbk格式
	printf("不使用代理:%s n", buff);//输出访问结果

	Sleep(1000 * 1000);
	free(buff);
	return 0;
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号