# 會話控制

會話控制類型包含輪換會話和黏性會話兩種

### **輪轉會話**

當會話類型設為 **輪轉會話** 時：

**動態IP分配**：每次請求自動分配**全新住宅IP**

**適用場景**：高匿名性業務需求

**配置規則**：

* 無需拼接會話標識參數
* 在端點生成器中啟用 **輪轉模式** 即可實時生效

{% hint style="success" %}
[**點擊此處**](https://doc.thordata.com/doc/zh-hk/dai-li/zhu-zhai-dai-li/wei-zhi-she-zhi/guo-jia-di-qu-ru-kou-jie-dian) 查看輪換會話-白名單國家/地區入口節點
{% endhint %}

### **粘性會話**

粘性會話允許保留單個 IP 地址，以便發起多個請求。要多次重複使用同一個 IP 地址，選擇以下任一方式：

* **端點生成器配置**

▸ 切換會話模式為黏性會話

▸ 設定持續時長（單位：分鐘）

* **參數化配置**

在使用者名稱後拼接：**`sessid-[自定義會話ID]+sesstime-[持續時間]`**

▸ 會話ID：字母數字組合（如abc123）

▸ 持續時間：整數分鐘值（如10）

例如設定黏性模式持續時間為10分鐘：`sessid-abc123-sesstime-10`

<figure><img src="https://637457805-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbV8SoKVnOm2o3eQR4D1v%2Fuploads%2FiCWCpYCv5Ix3aQHSdq23%2Fscreenshot_2025-08-01_14-39-53.gif?alt=media&#x26;token=30b8dc7a-90a3-48bb-bf3e-148c2378f0de" alt=""><figcaption></figcaption></figure>

新的 IP 地址。在標準會話時間為 10 分鐘或不超過 60 秒的非活動時間（無請求）之後，將自動分配一個新的 IP 地址。

> 例如您使用 `sessid-a123123+sesstime-10` 被分配至代理 IP `1.1.1.1` 後，只要持續使用該會話參數發送請求且代理節點保持可用狀態，您的請求將始終通過 `1.1.1.1` 進行傳輸。當會話時間超過預設的 10 分鐘時，下一個使用 `sessid-a123123+sesstime-10` 的請求將自動分配新代理 IP，例如 `1.1.1.2`。

您也可以設定多條不同的會話代理地址，比如：

```
user-USERNAME-sessid-a234234-sesstime-15:PASSWORD
user-USERNAME-sessid-a345345-sesstime-30:PASSWORD
user-USERNAME-sessid-a456456-sesstime-45:PASSWORD
user-USERNAME-sessid-a567567-sesstime-90:PASSWORD
```

**代码示例：**

在此範例中，演示了如何通過包含`sessid`字串和`sesstime`（10分鐘）參數的美國IP進行首次請求。在10分鐘會話有效期內，所有後續查詢將繼續使用該美國IP地址。

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

```sh
curl -x "https://td-customer-USERNAME-country-us-sessid-a123123-sesstime-10:PASSWORD@t.na.thordata.net:9999" "https://ipinfo.thordata.com"
```

{% endtab %}

{% tab title="C#" %}

```csharp
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

class csharp_https
{
    static void Main(string[] args)
    {
        Task t = new Task(DownloadPageAsync);
        t.Start();
        Console.ReadLine();
    }

    static async void DownloadPageAsync()
    {
        string page = "https://ipinfo.thordata.com";

        var proxy = new WebProxy("https://t.na.thordata.net:9999")
        {
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(
                userName: "td-customer-USERNAME-country-us-sessid-a123123-sesstime-10",
                password: "PASSWORD")
        };

        var httpClientHandler = new HttpClientHandler()
        {
            Proxy = proxy,
            ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
        };

        var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);
        var response = await client.GetAsync(page);

        using (HttpContent content = response.Content)
        {
            string result = await content.ReadAsStringAsync();
            Console.WriteLine(result);
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}
```

{% endtab %}

{% tab title="GO" %}

```go
package main

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

const (
	resourceUrl = "https://ipinfo.thordata.com"
	proxyHost   = "t.na.thordata.net:9999"
	username    = "td-customer-USERNAME-country-us-sessid-a123123-sesstime-10"
	password    = "PASSWORD"
)

func main() {
	proxyUrl := &url.URL{
		Scheme: "https",
		User:   url.UserPassword(username, password),
		Host:   proxyHost,
	}

	client := http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyURL(proxyUrl),
		},
	}

	resp, err := client.Get(resourceUrl)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	bodyString := string(body)
	fmt.Println(bodyString)
}
```

{% endtab %}

{% tab title="Java" %}

```java
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class JavaHttps {
    public static final String username = "td-customer-USERNAME-country-us-sessid-a123123-sesstime-10";
    public static final String password = "PASSWORD";
    public static final int port = 9999;
    public static final String proxyHost = "t.na.thordata.net";
    public CloseableHttpClient client;

    public JavaHttps() {
        HttpHost proxy = new HttpHost(proxyHost, port, "https");
        CredentialsProvider cred_provider = new BasicCredentialsProvider();
        cred_provider.setCredentials(new AuthScope(proxy),
                new UsernamePasswordCredentials(username, password));
        client = HttpClients.custom()
                .setConnectionManager(new BasicHttpClientConnectionManager())
                .setProxy(proxy)
                .setDefaultCredentialsProvider(cred_provider)
                .build();
    }

    public String request(String url) throws IOException {
        HttpGet request = new HttpGet(url);
        CloseableHttpResponse response = client.execute(request);
        try {
            return EntityUtils.toString(response.getEntity());
        } finally { response.close(); }
    }

    public void close() throws IOException { client.close(); }

    public static void main(String[] args) throws IOException {
        JavaHttps client = new JavaHttps();
        try {
            System.out.println(client.request("https://ipinfo.thordata.com"));
        } finally { client.close(); }
    }
}

```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
  $url = 'https://ipinfo.thordata.com';
  $proxy = 't.na.thordata.net';
  $port = 9999;
  $user = 'td-customer-USERNAME-country-us-sessid-a123123-sesstime-10';
  $psw = 'PASSWORD';

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_PROXY, "$proxy:$port");
  curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$user:$psw");
  $result = curl_exec($ch);
  curl_close($ch);

  if ($result)
  {
      echo $result . PHP_EOL;
  }
?>
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

username = "td-customer-USERNAME-country-us-sessid-a123123-sesstime-10"
password = "PASSWORD"
proxy_server = "t.na.thordata.net:9999"

proxies = {"https": f"https://{username}:{password}@{proxy_server}"}

response = requests.get("https://ipinfo.thordata.com", proxies=proxies)
print(response.text)
```

{% endtab %}

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

```
const rp = require('request-promise');

const username = "td-customer-USERNAME-country-us-sessid-a123123-sesstime-10";
const password = "PASSWORD";
const proxyServer = "t.na.thordata.net:9999";

rp({
    url: 'https://ipinfo.thordata.com',
    proxy: `https://${username}:${password}@${proxyServer}`,
})
.then(function(data) {
    console.log(data);
})
.catch(function(err) {
    console.error(err);
});
```

{% endtab %}

{% tab title="Untitled" %}

```ruby
require "uri"
require 'net/http'
require 'openssl'

proxy_host = 't.na.thordata.net'
proxy_port = 9999
proxy_user = 'td-customer-USERNAME-country-us-sessid-a123123-sesstime-10'
proxy_pass = 'PASSWORD'

uri = URI.parse('https://ipinfo.thordata.com')

proxy = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass)

req = Net::HTTP::Get.new(uri)

result = proxy.start(uri.host, uri.port, use_ssl: true, ssl_version: :TLSv1_2) do |http|
  http.request(req)
end

puts result.body
```

{% endtab %}
{% endtabs %}

{% hint style="success" %}
[**點擊此處**](https://doc.thordata.com/doc/zh-hk/dai-li/zhu-zhai-dai-li/wei-zhi-she-zhi/guo-jia-di-qu-ru-kou-jie-dian) 查看粘性會話-白名單國家/地區入口節點
{% endhint %}
