# 轮换类型

在代理会话领域，我们提供两种不同类型的轮换：“粘性”和“随机”。每种类型都有不同的用途，以满足用户的不同需求。

### 粘性

此选项能够让您在会话期间始终保持代理不变。使用粘性会话，您可以配置“生命周期”参数，该参数决定在切换到新代理之前使用相同代理的时间。这对于需要持续连接到同一IP地址的任务特别有用，例如在访问具有基于会话的身份验证或追踪的Web资源时始终保持会话不变。

1. `_session-` 值会指示我们的路由系统为连接创建或解析一个唯一的会话。分配给它的值必须是一个由字母和数字组成的随机字符串，长度精度为**8个字符**。这样就保证了会话的唯一性和完整性。
2. `_lifetime-` 值会指示路由器会话保持有效的持续时间。最短持续时间设置为**1秒**，最长持续时间设置为**7天**。注意这里的格式至关重要：只能指定一个时间单位。这个参数对于定义粘性会话的操作跨度、平衡会话稳定性和安全需求方面都起到非常关键的作用。<br>

示例：

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

```
curl -v -x http://username123:password321_country-br_session-sgn34f3e_lifetime-10m@geo.iproyal.com:12321 -L http://example.com
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
$username = 'username123';
$password = 'password321_country-br_session-sgn34f3e_lifetime-10m';
$proxy = 'geo.iproyal.com:12321';
$url = 'http://example.com';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    echo $response;
}

curl_close($ch);
?>
```

{% endtab %}

{% tab title="Python" %}

```python
import requests
from requests.auth import HTTPProxyAuth

username = 'username123'
password = 'password321_country-br_session-sgn34f3e_lifetime-10m'
proxy = 'geo.iproyal.com:12321'
url = 'http://example.com'

proxies = {
    'http': f'http://{proxy}',
    'https': f'http://{proxy}',
}

auth = HTTPProxyAuth(username, password)

response = requests.get(url, proxies=proxies, auth=auth)

print(response.text)
```

{% endtab %}

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

```javascript
const http = require('http');

const username = 'username123';
const password = 'password321_country-br_session-sgn34f3e_lifetime-10m';
const proxyHost = 'geo.iproyal.com';
const proxyPort = 12321;
const targetUrl = 'http://example.com';

const targetUrlObj = new URL(targetUrl);
const targetHost = targetUrlObj.host;
const targetPath = targetUrlObj.pathname;

const auth = 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64');

const options = {
  host: proxyHost,
  port: proxyPort,
  method: 'GET',
  path: targetUrl,
  headers: {
    'Host': targetHost,
    'Proxy-Authorization': auth
  }
};

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

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

  res.on('end', () => {
    console.log(data);
  });
});

req.on('error', (error) => {
  console.error('Error:', error.message);
});

req.end();
```

{% endtab %}
{% endtabs %}

### 随机

随机会话为每个请求提供一个新的代理。这意味着每次您发出请求时，它都来自不同的IP地址。这种方法对于需要高度匿名性和降低可追溯性的任务来说是理想的，比如网页抓取或访问内容，而不会暴露相同的身份。

**要使用这种类型的轮换会话，您无需在代理字符串中添加任何内容。**
