會話控制
會話控制類型包含輪換會話和粘性會話兩種
輪換會話:
如果您希望在每次請求時都能獲取新的IP,您可以在端點產生器中切換會話設定為輪換模式並使用。
粘性會話:
如果您希望在一定時間內保留相同的IP地址來運行多個請求,您可以在端點產生器中切換會話設定為粘性模式並設定持續時間或在用戶名後使用sessid
參數+隨機創建的字母數字字符串+sesstime
參數+設定的持續時間(分鐘),例如設定粘性模式持續時間為10分鐘:sessid-a123123-sesstime-10
。
比如您查詢sessid-a123123-sesstime-10
被分配至代理IP地址1.1.1.1
,只要您保持使用相同的sessid-a123123-sesstime-10
發送請求並且IP地址在線可用的情況下,您請求的代理IP地址將不會變更。當會話時間超過設定的10分鐘時,您在sessid-a12123-sesstime-10
的下一個請求將分配不同的代理IP地址,例如1.1.1.2
。
請注意:當前我們支持設定最高持續時間為90分鐘。
您也可以設定多條不同的會話代理地址,比如:
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-a123123-sesstime-10
在此示例中,使用與第一次請求相同的美國IP,並且持續時間為10分鐘
curl -x t.pr.thordata.net:12233 -U "user-USERNAME-region-US-sessid-a123123-sesstime-10:PASSWORD" ipinfo.thordata.com
using System;
using System.Net;
class Client : WebClient
{
public static string username = "USERNAME";
public static string password = "PASSWORD";
public string session_id = new Random().Next().ToString();
public static string session_time = "10";
public Client(string country_iso = null)
{
this.Proxy = new WebProxy("t.pr.thordata.net:12233");
var login = "user-"+username+(country_iso != null ? "-region-"+country_iso : "")
+"-sessid-"+session_id+"-sesstime-"+session_time;
this.Proxy.Credentials = new NetworkCredential(login, password);
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
request.ConnectionGroupName = session_id;
request.ConnectionGroupTime = session_time;
return request;
}
}
class Example
{
static void Main()
{
var client = new Client("us");
Console.WriteLine(client.DownloadString("https://ipinfo.thordata.com"));
}
}
<?php
$username = 'USERNAME';
$password = 'PASSWORD';
$country = 'US';
$session = mt_rand();
$sesstime = 10;
$proxy = 't.pr.thordata.net:12233';
$query = curl_init('https://ipinfo.thordata.com');
curl_setopt($query, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($query, CURLOPT_PROXY, "http://$proxy");
curl_setopt($query, CURLOPT_PROXYUSERPWD, "user-$username-region-$country-sessid-$session-sesstime-$sesstime:$password");
$output = curl_exec($query);
curl_close($query);
if ($output)
echo $output;
?>
import urllib.request
import random
username = 'USERNAME'
password = 'PASSWORD'
country = 'US'
session = random.random()
sesstime = 10
entry = ('http://user-%s-region-%s-sessid-%s-sesstime-%d:%s@t.pr.thordata.net:12233' %
(username, country, city, session, sesstime, password))
query = urllib.request.ProxyHandler({
'http': entry,
'https': entry,
})
execute = urllib.request.build_opener(query)
print(execute.open('https://ipinfo.thordata.com').read())
package example;
import java.io.*;
import java.util.Random;
import org.apache.http.HttpHost;
import org.apache.http.auth.*;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
class Client {
public static final String username = "USERNAME";
public static final String password = "PASSWORD";
public String session_id = Integer.toString(new Random().nextInt(Integer.MAX_VALUE));
public String sesstime = "10";
public CloseableHttpClient client;
public Client(String country) {
String login = "user-"+username+(country_iso != null ? "-region-"+country_iso : "")+"-sessid-"+session_id+"-sesstime-" +sesstime;
HttpHost entry_node = new HttpHost("t.pr.thordata.net:12233");
CredentialsProvider credentials_provider = new BasicCredentialsProvider();
credentials_provider.setCredentials(new AuthScope(entry_node),
new UsernamePasswordCredentials(login, password));
client = HttpClients.custom()
.setConnectionManager(new BasicHttpClientConnectionManager())
.setProxy(entry_node)
.setDefaultCredentialsProvider(credentials_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 class Example {
public static void main(String[] args) throws IOException {
Client client = new Client("us");
try {
System.out.println(client.request("https://ipinfo.thordata.com"));
} finally { client.close(); }
}
}
require 'uri'
require 'net/http'
require 'net/https'
entry_node = 't.pr.thordata.net'
entry_port = '12233'
username = 'USERNAME'
password = 'PASSWORD'
session_id = Random.rand(1000000)
sesstime = 10
uri = URI.parse("https://ipinfo.thordata.com")
headers = {
'Accept-Encoding' => 'gzip'
}
proxy = Net::HTTP::Proxy(entry_node, entry_port, "#{username}-region-US-sessid-#{session_id}-sesstime-#{sesstime}", password)
http = proxy.new(uri.host,uri.port)
if uri.scheme == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
req = Net::HTTP::Get.new(uri.path, headers)
result = http.start do |con|
con.request(req)
end
puts result.body
Last updated