Skip to content
登录 注册

代码集成

Shifter 网关在 p.shifter.io:443 上支持纯 HTTP(S) 和 SOCKS5 协议。任何语言中支持代理认证的 HTTP(S) 客户端都可以使用,无需 SDK。

用户名编码了你的定向和会话偏好设置,密码则是你的账户密码。完整的标志列表请参见 Gateway & auth

最常用的选项是配合 proxies 字典使用的 requests:

import requests
proxy = "customer-USERNAME-country-us-sid-9f3a2b7c-ttl-600:PASSWORD@p.shifter.io:443"
proxies = {"http": proxy, "https": proxy}
r = requests.get("https://ipinfo.io/json", proxies=proxies)
print(r.text)

对于异步代码,httpx 的用法相同(httpx.AsyncClient(proxies=proxy))。

使用 node-fetch(或 Node 18+ 内置的 fetch)搭配 https-proxy-agent:

import fetch from 'node-fetch';
import { HttpsProxyAgent } from 'https-proxy-agent';
const agent = new HttpsProxyAgent(
'customer-USERNAME-country-us-sid-9f3a2b7c-ttl-600:PASSWORD@p.shifter.io:443'
);
const res = await fetch('https://ipinfo.io/json', { agent });
console.log(await res.text());

axiosgotundici 都接受类似的 agent 选项。

运行时环境与 Node.js 相同,只是带有类型。以下是使用 axios 的示例:

import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';
const agent = new HttpsProxyAgent(
'customer-USERNAME-country-us-sid-9f3a2b7c-ttl-600:PASSWORD@p.shifter.io:443'
);
const { data } = await axios.get<string>('https://ipinfo.io/json', {
httpAgent: agent,
httpsAgent: agent,
});
console.log(data);

无需转译器的纯 Node CommonJS 写法:

const fetch = require('node-fetch');
const { HttpsProxyAgent } = require('https-proxy-agent');
const agent = new HttpsProxyAgent(
'customer-USERNAME-country-us-sid-9f3a2b7c-ttl-600:PASSWORD@p.shifter.io:443'
);
fetch('https://ipinfo.io/json', { agent })
.then((r) => r.text())
.then(console.log);

标准库,无需第三方依赖:

package main
import (
"fmt"
"io"
"net/http"
"net/url"
)
func main() {
proxyURL, _ := url.Parse("customer-USERNAME-country-us-sid-9f3a2b7c-ttl-600:PASSWORD@p.shifter.io:443")
client := &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
}
resp, _ := client.Get("https://ipinfo.io/json")
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}

在代理上使用基本认证的 reqwest:

use reqwest::blocking::Client;
use reqwest::Proxy;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let proxy = Proxy::all("http://p.shifter.io:443")?
.basic_auth("customer-USERNAME-country-us-sid-9f3a2b7c-ttl-600", "PASSWORD");
let client = Client::builder().proxy(proxy).build()?;
let body = client.get("https://ipinfo.io/json").send()?.text()?;
println!("{body}");
Ok(())
}

内置的 java.net.http.HttpClient(Java 11+):

import java.net.*;
import java.net.http.*;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("p.shifter.io", 443)))
.authenticator(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"customer-USERNAME-country-us-sid-9f3a2b7c-ttl-600",
"PASSWORD".toCharArray()
);
}
})
.build();
HttpRequest req = HttpRequest.newBuilder(URI.create("https://ipinfo.io/json")).build();
HttpResponse<String> res = client.send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(res.body());
}
}

C#

带有 HttpClientHandler.ProxyHttpClient:

using System.Net;
using System.Net.Http;
var handler = new HttpClientHandler
{
Proxy = new WebProxy("http://p.shifter.io:443")
{
Credentials = new NetworkCredential(
"customer-USERNAME-country-us-sid-9f3a2b7c-ttl-600",
"PASSWORD"
)
},
UseProxy = true
};
using var client = new HttpClient(handler);
var ip = await client.GetStringAsync("https://ipinfo.io/json");
Console.WriteLine(ip);

使用 proxy 请求选项的 Guzzle:

<?php
require 'vendor/autoload.php';
$client = new GuzzleHttp\Client([
'proxy' => 'customer-USERNAME-country-us-sid-9f3a2b7c-ttl-600:PASSWORD@p.shifter.io:443',
]);
$res = $client->get('https://ipinfo.io/json');
echo $res->getBody();

纯 curl 绑定(curl_setopt($ch, CURLOPT_PROXY, '...'))同样可以使用。

该网关是纯 HTTP/HTTPS/SOCKS 代理。只要你的客户端支持其中任意一种协议并配合基本认证,即可正常使用。对于特定产品的工具(Scrapy、Puppeteer、Playwright、SwitchyOmega、Multilogin),请参见 Residential Proxies → Integrations