代码示例
各种编程语言和使用场景的完整代码示例
HTTP CONNECT 代理示例
Node.js - 基础HTTP请求
const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');
// IPFixed代理配置
const proxyUrl = 'http://项目名:密码@proxy.ipfixed.com:80';
const agent = new HttpsProxyAgent(proxyUrl);
// 发送HTTPS请求
async function makeRequest() {
try {
const response = await axios.get('https://api.example.com/data', {
httpsAgent: agent
});
console.log('请求成功:', response.data);
return response.data;
} catch (error) {
console.error('请求失败:', error.message);
throw error;
}
}
makeRequest();Python - 基础HTTP请求
import requests
# IPFixed代理配置
proxies = {
'http': 'http://项目名:密码@proxy.ipfixed.com:80',
'https': 'http://项目名:密码@proxy.ipfixed.com:80'
}
def make_request():
try:
response = requests.get('https://api.example.com/data', proxies=proxies)
response.raise_for_status()
print('请求成功:', response.json())
return response.json()
except requests.exceptions.RequestException as e:
print('请求失败:', e)
raise
make_request()环境变量配置示例
设置环境变量
# Linux/macOS
export HTTP_PROXY=http://项目名:密码@proxy.ipfixed.com:80
export HTTPS_PROXY=http://项目名:密码@proxy.ipfixed.com:80
# Windows
set HTTP_PROXY=http://项目名:密码@proxy.ipfixed.com:80
set HTTPS_PROXY=http://项目名:密码@proxy.ipfixed.com:80验证代理连接
# 使用curl测试
curl -x http://项目名:密码@proxy.ipfixed.com:80 https://httpbin.org/ip
# 预期返回固定IP地址基础错误处理
Python - 简单错误处理
import requests
def safe_request(url):
proxies = {
'http': 'http://项目名:密码@proxy.ipfixed.com:80',
'https': 'http://项目名:密码@proxy.ipfixed.com:80'
}
try:
response = requests.get(url, proxies=proxies, timeout=30)
response.raise_for_status()
return response.json()
except requests.exceptions.ProxyError:
print("代理连接失败,请检查项目名和密码")
except requests.exceptions.Timeout:
print("请求超时")
except requests.exceptions.HTTPError as e:
print(f"HTTP错误: {e}")
except Exception as e:
print(f"未知错误: {e}")
# 使用示例
result = safe_request('https://api.example.com/data')
if result:
print(result)