如何再c語(yǔ)言代碼中使用HTTP代理IP。
以下代碼主要圍繞第一次接觸HTTP代理IP的c新手來(lái)寫(xiě)(步驟注釋清晰)。
直接把下面示例代碼中的HTTP代理API,替換成你后臺(tái)生成的代理API鏈接,就可以跑起來(lái)了。
以下是一個(gè)示例代碼,只是一個(gè)基礎(chǔ)的演示,具體的代碼還是要根據(jù)你業(yè)務(wù)的實(shí)際情況去寫(xiě)的。
示例代碼中的HTTP代理IP,我使用的是華益云的HTTP代理API,注冊(cè)就白嫖1萬(wàn)個(gè)高匿爬蟲(chóng)IP,有效期是一年,對(duì)于調(diào)試代碼來(lái)說(shuō)這個(gè)時(shí)間是非常的友好。
示例代碼demo中同款HTTP代理API-點(diǎn)我免費(fèi)領(lǐng)取10000個(gè)高匿IP
打開(kāi)代理API,獲取里面的IP,使用IP訪問(wèn)目標(biāo)網(wǎng)站,其實(shí)代碼中就是執(zhí)行這個(gè)過(guò)程而已,然后加了幾個(gè)錯(cuò)誤判斷有助于代碼的穩(wěn)定運(yùn)行。(步驟注釋清晰)
// demo.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#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;
}
/*
使用http代理
*/
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);//獲得訪問(wèn)結(jié)果
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("錯(cuò)誤代碼:%d\n", res);
MessageBox(NULL, TEXT("獲取IP錯(cuò)誤"), 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);/*訪問(wèn)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("錯(cuò)誤代碼:%d\n", res);
MessageBox(NULL, TEXT("獲取IP錯(cuò)誤"), TEXT("助手"), MB_ICONINFORMATION | MB_YESNO);
}
}
return res;
}
//將utf-8轉(zhuǎn)為gbk格式
void utf8ToGbk(char *utf8String, char *gbkString)
{
wchar_t *unicodeStr = NULL;
int nRetLen = 0;
nRetLen = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, NULL, 0);//求需求的寬字符數(shù)大小
unicodeStr = (wchar_t *)malloc(nRetLen * sizeof(wchar_t));
nRetLen = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, unicodeStr, nRetLen);//將utf-8編碼轉(zhuǎn)換成unicode編碼
nRetLen = WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, NULL, 0, NULL, 0);//求轉(zhuǎn)換所需字節(jié)數(shù)
nRetLen = WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, gbkString, nRetLen, NULL, 0);//unicode編碼轉(zhuǎn)換成gbk編碼
free(unicodeStr);
}
int main()
{
char *buff = (char*)malloc(1024 * 1024);
memset(buff, 0, 1024 * 1024);
//代理api(這里我推薦使用www.9vps.com華益云的HTTP代理API,注冊(cè)就白嫖1萬(wàn)IP)
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訪問(wèn)網(wǎng)站
memset(buff, 0, 1024 * 1024);
GetUrlHTTP("http://myip.top", buff, (char*)ip);
utf8ToGbk(buff, buff);//將獲取到的網(wǎng)頁(yè)內(nèi)容轉(zhuǎn)為gbk格式
printf("\nhttp結(jié)果:%s\n", buff);//輸出訪問(wèn)結(jié)果
//不使用代理訪問(wèn)
GetUrl("http://myip.top", buff);
utf8ToGbk(buff, buff); // 將獲取到的網(wǎng)頁(yè)內(nèi)容轉(zhuǎn)為gbk格式
printf("不使用代理:%s \n", buff);//輸出訪問(wèn)結(jié)果
Sleep(1000 * 1000);
free(buff);
return 0;
}
-
C語(yǔ)言
+關(guān)注
關(guān)注
180文章
7604瀏覽量
136842 -
HTTP
+關(guān)注
關(guān)注
0文章
505瀏覽量
31232 -
代碼
+關(guān)注
關(guān)注
30文章
4788瀏覽量
68617
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論