// 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); // 设置访问的域名

        res = curl_easy_perform(curl); // 执行请求
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }

        curl_easy_cleanup(curl); // 清理
    }
    return 0;
}

                
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)

func writeBuffData(data []byte, buff *[]byte) {
    *buff = append(*buff, data...)
}

func GetUrlHTTP(targetUrl string, proxyUrl string) ([]byte, error) {
    proxy, err := url.Parse(proxyUrl)
    if err != nil {
        return nil, fmt.Errorf("invalid proxy URL: %v", err)
    }

    transport := &http.Transport{
        Proxy: http.ProxyURL(proxy),
    }

    client := &http.Client{
        Transport: transport,
    }

    resp, err := client.Get(targetUrl)
    if err != nil {
        return nil, fmt.Errorf("failed to get URL: %v", err)
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, fmt.Errorf("failed to read response body: %v", err)
    }

    return body, nil
}

func main() {
    url := "http://example.com"
    proxy := "http://代理服务器地址:端口"

    data, err := GetUrlHTTP(url, proxy)
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    fmt.Printf("Response: %s\n", data)
}
                
const http = require('http');
const https = require('https');
const url = require('url');

function getUrlHTTP(targetUrl, proxyUrl, callback) {
    const parsedUrl = url.parse(targetUrl);
    const parsedProxyUrl = url.parse(proxyUrl);

    const options = {
        hostname: parsedProxyUrl.hostname,
        port: parsedProxyUrl.port,
        path: targetUrl,
        headers: {
            Host: parsedUrl.hostname
        }
    };

    const protocol = parsedProxyUrl.protocol === 'https:' ? https : http;

    const req = protocol.request(options, (res) => {
        let data = '';

        res.on('data', (chunk) => {
            data += chunk;
        });

        res.on('end', () => {
            callback(null, data);
        });
    });

    req.on('error', (err) => {
        callback(err);
    });

    req.end();
}

const targetUrl = 'http://example.com';
const proxyUrl = 'http://代理服务器地址:端口';

getUrlHTTP(targetUrl, proxyUrl, (err, data) => {
    if (err) {
        console.error('Error:', err);
    } else {
        console.log('Response:', data);
    }
});
                
//
function getUrlHTTP($targetUrl, $proxyUrl) {
    $proxy = parse_url($proxyUrl);

    $contextOptions = [
        'http' => [
            'proxy' => sprintf('tcp://%s:%s', $proxy['host'], $proxy['port']),
            'request_fulluri' => true,
        ],
    ];

    $context = stream_context_create($contextOptions);
    $response = file_get_contents($targetUrl, false, $context);

    if ($response === false) {
        throw new Exception('Failed to get URL: ' . $targetUrl);
    }

    return $response;
}

try {
    $url = 'http://example.com';
    $proxy = 'http://代理服务器地址:端口';

    $data = getUrlHTTP($url, $proxy);
    echo "Response: " . $data . "\n";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

                
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

public class HttpClient {

    public static String getUrlHTTP(String targetUrl,
                        String proxyUrl, int proxyPort) throws Exception {
        URL url = new URL(targetUrl);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrl, proxyPort));
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);

        connection.setRequestMethod("GET");
        connection.connect();

        int responseCode = connection.getResponseCode();
        if (responseCode != 200) {
            throw new Exception("Failed to fetch URL: " +
                        targetUrl + " with HTTP response code: " + responseCode);
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();
    }

    public static void main(String[] args) {
        try {
            String targetUrl = "http://example.com";
            String proxyUrl = "代理服务器地址";
            int proxyPort = 端口;

            String response = getUrlHTTP(targetUrl, proxyUrl, proxyPort);
            System.out.println("Response: " + response);
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
                
import requests
from requests.exceptions import RequestException

def get_url_http(target_url, proxy_url):
    proxies = {
        'http': proxy_url,
        'https': proxy_url,
    }

    try:
        response = requests.get(target_url, proxies=proxies)
        response.raise_for_status()
        # Raise an HTTPError if the HTTP request returned an unsuccessful status code
        return response.text
    except RequestException as e:
        raise Exception(f"Failed to get URL: {target_url}, error: {e}")

if __name__ == "__main__":
    try:
        url = "http://example.com"
        proxy = "http://代理服务器地址:端口"

        data = get_url_http(url, proxy)
        print("Response:", data)
    except Exception as e:
        print("Error:", e)

                
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

def get_url_http_with_selenium(target_url, proxy_url, proxy_port):
    # Set up Chrome options to use a proxy
    chrome_options = Options()
    chrome_options.add_argument(f'--proxy-server={proxy_url}:{proxy_port}')

    # Set up the WebDriver (you need to have chromedriver installed and in your PATH)
    service = Service('path/to/chromedriver')  # Update the path to your chromedriver
    driver = webdriver.Chrome(service=service, options=chrome_options)

    try:
        driver.get(target_url)

        # Wait for the page to load completely
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))

        # Get the page source
        page_source = driver.page_source
        return page_source
    except TimeoutException as e:
        raise Exception(f"Failed to load URL: {target_url}, error: {e}")
    finally:
        driver.quit()

if __name__ == "__main__":
    try:
        url = "http://example.com"
        proxy = "代理服务器地址"
        port = 端口

        data = get_url_http_with_selenium(url, proxy, port)
        print("Response:", data)
    except Exception as e:
        print("Error:", e)

                

找IP吧注册

获取验证码

忘记密码

获取验证码