400-692-3698

帮助文档

七大主流语言SDK及示例

可复制供用户参考
      当前位置: 首页 > SDK 示例

如何在C++中使用代理示例

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <curl/curl.h>
#include <QApplication>
#include <QWebView>
#include <QNetworkProxy>

// 回调函数用于接收 libcurl 的数据
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
    size_t totalSize = size * nmemb;
    userp->append((char*)contents, totalSize);
    return totalSize;
}

std::string getProxyIp(const std::string& apiUrl) {
    CURL* curl;
    CURLcode res;
    std::string readBuffer;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, apiUrl.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); // 超时设置为10秒

        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            std::cerr << "Failed to get IP address: " << curl_easy_strerror(res) << std::endl;
            curl_easy_cleanup(curl);
            curl_global_cleanup();
            return "";
        }

        long httpCode = 0;
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
        if (httpCode != 200) {
            std::cerr << "Failed to get IP address: HTTP error code : " << httpCode << std::endl;
            curl_easy_cleanup(curl);
            curl_global_cleanup();
            return "";
        }

        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

    std::string ip = readBuffer;
    std::istringstream iss(ip);
    std::string line;
    std::vector<std::string> ips;

    while (std::getline(iss, line)) {
        ips.push_back(line);
    }

    if (ips.empty() || ips[0].empty()) {
        std::cerr << "No valid IP address found in response" << std::endl;
        return "";
    }

    return ips[0]; // 这里取第一个代理IP 实际应用时,根据需要自己选择
}

void loadPageWithProxy(const std::string& url, const std::string& proxy) {
    QApplication app(argc, argv);

    QWebView webView;
    QNetworkProxy networkProxy(QNetworkProxy::HttpProxy, QString::fromStdString(proxy), 80);
    QNetworkProxy::setApplicationProxy(networkProxy);

    QObject::connect(&webView, &QWebView::loadFinished, [&](bool ok) {
        if (!ok) {
            std::cerr << "Failed to open the URL" << std::endl;
        } else {
            std::cout << webView.page()->mainFrame()->toHtml().toUtf8().constData() << std::endl; // 打印返回的内容
        }
        app.quit();
    });

    webView.load(QUrl(QString::fromStdString(url)));
    app.exec();
}

int main(int argc, char *argv[]) {
    std::string api = ""; // 这里填写你购买后生成的API网址,如果没有或者不会,请找客服  如:http://www.soyunip.com/api?ddbh=s8349453653

    std::string myip = getProxyIp(api);
    if (myip.empty()) {
        return 1;
    }

    std::string ipport = "http://" + myip;

    std::string url = "http://myip.ipip.net";

    loadPageWithProxy(url, myip);

    return 0;
}


654888147
在线时间
8:00-24:00
扫码加客服微信
400-692-3698
反馈类型
反馈内容
联系人姓名
联系电话