Oxylabs Documentation
中文
搜索文档…
⌃K

会话控制

住宅
移动
轮换 ISP
会话 ID 参数允许您保持相同的 IP 地址来运行多个请求。如需多次重复使用相同的 IP,请在用户名后面使用 sessid 参数和随机创建的字母数字字符串,例如 sessid-abcde12345
Session control
标准会话时间为 10 分钟或不超过 60 秒的不活动时间(无请求)。之后,将自动分配一个新的 IP 地址。如需调整所需的会话时间,请参考会话时间
例如,您初始查询的 sessid-abcde12345 被分配给代理 IP 地址 1.1.1.1.只要您始终在使用相同的会话 ID 发送新请求,同时 IP 地址在线且可用,系统就会将您的查询通过 1.1.1.1. 路由如果您不再发送请求 60 秒或 IP 地址不再在线,则系统会分配一个新的代理 IP。您在 sessid-abcde12345 的下一个请求将通过不同的 IP 地址路由,例如 1.1.1.2.

凭据列表示例:

示例中展示了建立不同会话的凭据列表。
customer-USERNAME-sessid-iqwcp:PASSWORD
customer-USERNAME-sessid-tevab:PASSWORD
customer-USERNAME-sessid-6drwn:PASSWORD
customer-USERNAME-sessid-7eh7g:PASSWORD
customer-USERNAME-sessid-z7cao:PASSWORD

代码示例

我们为第一个请求选择了德国的 IP,并将在后续查询中保持相同的 IP(会话控制):
cURL
PHP
Python
Java
C#
Ruby
curl -x pr.oxylabs.io:7777 -U "customer-USERNAME-cc-DE-sessid-abcde12345:PASSWORD" https://ipinfo.io
<?php
$username = 'USERNAME';
$password = 'PASSWORD';
$country = 'DE';
$session = mt_rand();
$proxy = 'pr.oxylabs.io:7777';
$query = curl_init('https://ipinfo.io');
curl_setopt($query, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($query, CURLOPT_PROXY, "http://$proxy");
curl_setopt($query, CURLOPT_PROXYUSERPWD, "customer-$username-cc-$country-sessid-$session:$password");
$output = curl_exec($query);
curl_close($query);
if ($output)
echo $output;
?>
import urllib.request
import random
username = 'USERNAME'
password = 'PASSWORD'
country = 'DE'
city = 'munich'
session = random.random()
entry = ('http://customer-%s-cc-%s-city-%s-sessid-%s:%[email protected]:7777' %
(username, country, city, session, password))
query = urllib.request.ProxyHandler({
'http': entry,
'https': entry,
})
execute = urllib.request.build_opener(query)
print(execute.open('https://ipinfo.io').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 CloseableHttpClient client;
public Client(String country) {
String login = "customer-"+username+(country!=null ? "-cc-"+country : "")
+"-sessid-" + session_id;
HttpHost entry_node = new HttpHost("pr.oxylabs.io:7777");
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("de");
try {
System.out.println(client.request("https://ipinfo.io"));
} finally { client.close(); }
}
}
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 Client(string country_iso = null)
{
this.Proxy = new WebProxy("pr.oxylabs.io:7777");
var login = "customer-"+username+(country_iso != null ? "-cc-"+country_iso : "")
+"-sessid-"+session_id;
this.Proxy.Credentials = new NetworkCredential(login, password);
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
request.ConnectionGroupName = session_id;
return request;
}
}
class Example
{
static void Main()
{
var client = new Client("de");
Console.WriteLine(client.DownloadString("https://ipinfo.io"));
}
}
require 'uri'
require 'net/http'
require 'net/https'
entry_node = 'pr.oxylabs.io'
entry_port = '7777'
username = 'USERNAME'
password = 'PASSWORD'
session_id = Random.rand(1000000)
uri = URI.parse("https://ipinfo.io/")
headers = {
'Accept-Encoding' => 'gzip'
}
proxy = Net::HTTP::Proxy(entry_node, entry_port, "#{username}-cc-DE-sessid-#{session_id}", 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

会话时间

sesstime 参数须与 sessid 结合使用,其允许您将会话延长到 10 分钟以上,或者为其设置一个特定时间。该参数可以设置为不超过 30 分钟。
会话时间参数并不能确保您的所有查询都在会话结束时完成。会话将在设定的时限内过期,即使请求没有完成。

凭据列表示例:

示例中展示了建立具有不同会话时间(分钟)的不同会话的凭据列表。
customer-USERNAME-sessid-iqwcp-sesstime-5:PASSWORD
customer-USERNAME-sessid-tevab-sesstime-12:PASSWORD
customer-USERNAME-sessid-6drwn-sesstime-30:PASSWORD
customer-USERNAME-sessid-7eh7g-sesstime-6:PASSWORD
customer-USERNAME-sessid-z7cao-sesstime-16:PASSWORD

代码示例

我们选择了与上一个示例相同的德国 IP,这次我们加入了 sessidstringsesstime 参数,时间为 7 分钟:
cURL
PHP
Python
Java
C#
Ruby
curl -x pr.oxylabs.io:7777 -U "customer-USERNAME-cc-DE-sessid-abcde12345-sesstime-7:PASSWORD" https://ip.oxylabs.io
<?php
$username = 'USERNAME';
$password = 'PASSWORD';
$country = 'DE';
$session = mt_rand();
$sesstime = 7;
$proxy = 'pr.oxylabs.io:7777';
$query = curl_init('https://ip.oxylabs.io');
curl_setopt($query, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($query, CURLOPT_PROXY, "http://$proxy");
curl_setopt($query, CURLOPT_PROXYUSERPWD, "customer-$username-cc-$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 = 'DE'
session = random.random()
sesstime = 7
entry = ('http://customer-%s-cc-%s-sessid-%s-sesstime-%d:%[email protected]:7777' %
(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.io').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 = "7";
public CloseableHttpClient client;
public Client(String country) {
String login = "customer-"+username+(country_iso != null ? "-cc-"+country_iso : "")+"-sessid-"+session_id+"-sesstime-" +sesstime;
HttpHost entry_node = new HttpHost("pr.oxylabs.io:7777");
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("de");
try {
System.out.println(client.request("https://ipinfo.io"));
} finally { client.close(); }
}
}
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 Client(string country_iso = null)
{
this.Proxy = new WebProxy("pr.oxylabs.io:7777");
var login = "customer-"+username+(country_iso != null ? "-cc-"+country_iso : "")
+"-sessid-"+session_id;
this.Proxy.Credentials = new NetworkCredential(login, password);
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
request.ConnectionGroupName = session_id;
return request;
}
}
class Example
{
static void Main()
{
var client = new Client("de");
Console.WriteLine(client.DownloadString("https://ip.oxylabs.io"));
}
}
require 'uri'
require 'net/http'
require 'net/https'
entry_node = 'pr.oxylabs.io'
entry_port = '7777'
username = 'USERNAME'
password = 'PASSWORD'
session_id = Random.rand(1000000)
sesstime = 7
uri = URI.parse("https://ipinfo.io/")
headers = {
'Accept-Encoding' => 'gzip'
}
proxy = Net::HTTP::Proxy(entry_node, entry_port, "#{username}-cc-DE-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