# 代理轮换

ISP代理支持代理轮换。要使用此功能，您需要将端口号更改为 `8000`。每次新请求时，您都会从您的代理列表中获得一个随机 IP。

**代码示例**

{% tabs %}
{% tab title="cURL" %}

```sh
curl -x isp.oxylabs.io:8000 -U "user-USERNAME:PASSWORD" https://ip.oxylabs.io/location 
```

{% endtab %}

{% tab title="Python" %}

<pre class="language-python"><code class="lang-python">#pip install requests
<strong>import requests
</strong>
username = 'USERNAME'
密码 = 'PASSWORD'
proxy = 'isp.oxylabs.io:8000'

代理 = {
   "https": ('https://user-%s:%s@%s' % (username, password, proxy))
}

response=requests.get("https://ip.oxylabs.io/location", 代理=代理)

print(response.content)
</code></pre>

{% endtab %}

{% tab title="Node.js" %}

<pre class="language-javascript"><code class="lang-javascript">//npm install axios
<strong>const axios = require("axios");
</strong><strong>const https = require("https");
</strong>
const client = axios.create({
    httpsAgent: new https.Agent({
        rejectUnauthorized: false,
    }),
});
const username = 'USERNAME';
const password = 'PASSWORD'

client
    .get("https://ip.oxylabs.io/location", {
        proxy: {
            protocol: "https",
            host: "isp.oxylabs.io",
            端口：8000,
            认证：{
                用户名：`user-${username}`,
                密码：password,
            },
        },
    })
    .then((res) => {
        console.log(res.data);
    })
    .catch((err) => console.error(err));


</code></pre>

{% endtab %}

{% tab title="PHP" %}

```php
<?php

$username = 'USERNAME';
$password = 'PASSWORD';
$proxy = 'https://isp.oxylabs.io:8000';
$target = 'https://ip.Oxylabs.io/location';

$request = curl_init($target);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_PROXY, $proxy);
curl_setopt($request, CURLOPT_PROXYUSERPWD, "user-$username:$password");
$responseBody = curl_exec($request);
$error = curl_error($request);
curl_close($request);

if ($responseBody !== false) {
    echo '响应：' . $responseBody;
} 否则 {
    echo '连接代理失败：' . $error;
}
```

{% endtab %}

{% tab title="Go" %}

```go
package main

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

func main() {
	username, password, entry := "USERNAME", "PASSWORD", "isp.oxylabs.io:8000"
	proxy, err := url.Parse(fmt.Sprintf("https://user-%s:%s@%s", username, password, entry))
	if err != nil {
		panic(err)
	}

	transport := &http.Transport{
		Proxy: http.ProxyURL(proxy),
	}
	client := &http.Client{Transport: transport}
	target := "https://ip.oxylabs.io/location"
	response, err := client.Get(target)
	if err != nil {
		panic(err)
	}
	defer response.Body.Close()

	body, err := io.ReadAll(response.Body)
	if err != nil {
		panic(err)
	}

	fmt.Println("响应：")
	fmt.Println(string(body))
}
```

{% endtab %}

{% tab title="Java" %}

```java
package com.example;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Base64;

import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.HttpHost;

public class App {
    public static void main(String[] args) throws IOException, URISyntaxException {
        String target = "http://ip.oxylabs.io/location";
        String username = "USERNAME";
        String password = "PASSWORD";
        String proxy = "isp.oxylabs.io:8000";

        URI proxyURI = new URI(String.format("https://user-%s:%s@%s", username, password, proxy));

        String basicAuth = new String(
                Base64.getEncoder()
                        .encode(
                                proxyURI.getUserInfo().getBytes()));
        String response = Request.get(target)
                .addHeader("Proxy-Authorization", "Basic " + basicAuth)
                .viaProxy(HttpHost.create(proxyURI))
                .execute().returnContent().asString();

        System.out.println(response);
    }
}

```

{% endtab %}

{% tab title="C#" %}

```csharp
using System.Net;

// .NET 目前不支持 HTTPS 代理
var proxy = new WebProxy {
    Address = new Uri("http://isp.oxylabs.io:8000"),
    Credentials = new NetworkCredential(
        userName: "user-USERNAME",
        password: "PASSWORD"
    )
};

var httpClientHandler = new HttpClientHandler {Proxy = proxy};

using var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);

var result = await client.GetStringAsync("https://ip.oxylabs.io/location");
Console.WriteLine(result);
```

{% endtab %}
{% endtabs %}

如果你提供了 `country-` 参数，IP 将从指定国家的池中选择。例如，如果你只想轮换你的美国代理池，请使用参数 `country-` 并添加两位国家代码 `country-US` 到你的用户字符串中。&#x20;

**代码示例**

{% tabs %}
{% tab title="cURL" %}

```sh
curl -x https://isp.oxylabs.io:8000 -U 'user-USERNAME-country-COUNTRY:PASSWORD' https://ip.oxylabs.io/location
```

{% endtab %}

{% tab title="Python" %}

```python
#pip install requests
import requests

username = 'USERNAME'
密码 = 'PASSWORD'
country = 'COUNTRY'
proxy = 'isp.oxylabs.io:8001'

代理 = {
   "https": ('https://user-%s-country-%s:%s@%s' % (username, country, password, proxy))
}

response=requests.get("https://ip.oxylabs.io/location", 代理=代理)

print(response.content)
```

{% endtab %}

{% tab title="Node.js" %}

<pre class="language-javascript"><code class="lang-javascript">//npm install axios
<strong>const axios = require("axios");
</strong>const https = require("https");

const client = axios.create({
    httpsAgent: new https.Agent({
        rejectUnauthorized: false,
    }),
});
const username = 'USERNAME';
const country = 'COUNTRY'
const password = 'PASSWORD'

client
    .get("https://ip.oxylabs.io/location", {
        proxy: {
            protocol: "https",
            host: "isp.oxylabs.io",
            端口：8000,
            认证：{
                username: `user-${username}-country-${country}`,
                密码：password,
            },
        },
    })
    .then((res) => {
        console.log(res.data);
    })
    .catch((err) => console.error(err));

</code></pre>

{% endtab %}

{% tab title="PHP" %}

```php
<?php

$username = 'USERNAME';
$country = 'COUNTRY';
$password = 'PASSWORD';
$proxy = 'https://isp.oxylabs.io:8000';
$target = 'https://ip.Oxylabs.io/location';

$request = curl_init($target);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_PROXY, $proxy);
curl_setopt($request, CURLOPT_PROXYUSERPWD, "user-$username-country-$country:$password");
$responseBody = curl_exec($request);
$error = curl_error($request);
curl_close($request);

if ($responseBody !== false) {
    echo '响应：' . $responseBody;
} 否则 {
    echo '连接代理失败：' . $error;
}

```

{% endtab %}

{% tab title="Go" %}

```go
package main

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

func main() {
	username, country, password, entry := "USERNAME", "COUNTRY", "PASSWORD", "isp.oxylabs.io:8000"

	proxy, err := url.Parse(fmt.Sprintf("https://user-%s-country-%s:%s@%s", username, country, password, entry))
	if err != nil {
		panic(err)
	}

	transport := &http.Transport{
		Proxy: http.ProxyURL(proxy),
	}
	client := &http.Client{Transport: transport}
	target := "https://ip.oxylabs.io/location"
	response, err := client.Get(target)
	if err != nil {
		panic(err)
	}
	defer response.Body.Close()

	body, err := io.ReadAll(response.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println("响应：")
	fmt.Println(string(body))
}

```

{% endtab %}

{% tab title="Java" %}

```java
package com.example;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Base64;

import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.HttpHost;

public class App {
    public static void main(String[] args) throws IOException, URISyntaxException {
        String targetUrl = "http://ip.oxylabs.io/location";
        String username = "USERNAME";
        String country = "COUNTRY";
        String password = "PASSWORD";
        String proxy = "isp.oxylabs.io:8000";

        URI proxyURI = new URI(String.format("https://user-%s-country-%s:%s@%s", username, country, password, proxy));

        String basicAuth = new String(
                Base64.getEncoder()
                        .encode(
                                proxyURI.getUserInfo().getBytes()));
        String response = Request.get(targetUrl)
                .addHeader("Proxy-Authorization", "Basic " + basicAuth)
                .viaProxy(HttpHost.create(proxyURI))
                .execute().returnContent().asString();

        System.out.println(response);
    }
}

```

{% endtab %}

{% tab title="C#" %}

```csharp
using System.Net;

// .NET 目前不支持 HTTPS 代理
var proxy = new WebProxy {
    Address = new Uri("http://isp.oxylabs.io:8001"),
    Credentials = new NetworkCredential(
        userName: "user-USERNAME-country-COUNTRY",
        password: "PASSWORD"
    )
};

var httpClientHandler = new HttpClientHandler {Proxy = proxy};

using var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);

var result = await client.GetStringAsync("https://ip.oxylabs.io/location");
Console.WriteLine(result);
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.oxylabs.io/products/cn/dai-li/isp-proxies/proxy-rotation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
