> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/products/proxies/mobile-proxies/session-control.md).

# Session Control

### Set up a default session

The `sessid` parameter lets you route several requests through the same IP address. Add `sessid` after your `username` with a randomly generated alphanumeric string, for example `sessid-abcd1234`.

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzrXw45naRpCZ0Ku9AjY1%2Fuploads%2FxR5NdybM3rjlbhAbPBZN%2Fsession-id.mp4?alt=media>" %}

For example, an initial request using `sessid-abcd1234` might be assigned the IP `1.1.1.1`. As long as you keep sending requests with the same session ID and that IP stays online, your queries continue to route through `1.1.1.1`. If you pause for 60 seconds or the IP goes offline, the system assigns a new one, so your next request with `sessid-abcd1234` is routed through a different IP, such as `1.1.1.2`.

By default, a session lasts 10 minutes, or ends after 60 seconds without any requests – whichever comes first. Once that happens, a new IP address is assigned automatically. To change the session length, please refer to [**Extend session duration**](#extend-session-duration)**.**

#### **Credentials list example:**

Example represents a list of credentials that establish different sessions.

```
customer-USERNAME-cc-DE-sessid-qwert:PASSWORD
customer-USERNAME-cc-US-sessid-asdfg:PASSWORD
customer-USERNAME-cc-GB-sessid-zxcvb:PASSWORD
```

#### Code example

In this example we are using German IP with `sessid-abcd1234` in the username with the first request. All following requests will keep the same German IP with further queries:

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

```shell
curl -x pr.oxylabs.io:7777 -U "customer-USERNAME-cc-DE-sessid-abcd1234:PASSWORD" https://ip.oxylabs.io/location
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
$username = 'USERNAME';
$password = 'PASSWORD';
$country = 'DE';
$session = mt_rand();
$proxy = 'pr.oxylabs.io:7777';
$query = curl_init('https://ip.oxylabs.io/location');
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;
?>
```

{% endtab %}

{% tab title="Python" %}

```python
import urllib.request
import random
username = 'USERNAME'
password = 'PASSWORD'
country = 'DE'
session = random.random()
entry = ('http://customer-%s-cc-%s-sessid-%s:%s@pr.oxylabs.io:7777' %
    (username, country, session, password))
query = urllib.request.ProxyHandler({
    'http': entry,
    'https': entry,
})
execute = urllib.request.build_opener(query)
print(execute.open('https://ip.oxylabs.io/location').read())
```

{% endtab %}

{% tab title="Java" %}

```java
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://ip.oxylabs.io/location"));
        } finally { client.close(); }
    }
}
```

{% endtab %}

{% tab title="C#" %}

```csharp
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/location"));
    }
}
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
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://ip.oxylabs.io/location")
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
```

{% endtab %}
{% endtabs %}

### Extend session duration

The `sesstime` parameter is used together with `sessid` to keep a session running longer than the default 10 minutes or up to 60 seconds of inactivity (no requests).

{% hint style="success" %}
**Note:** You can use the `sesstime` parameter to **maintain the same IP for up to 1440 minutes** (24 hours). \
However, because the mobile proxy pool is dynamic, your connection may end sooner. If that happens, start a session with a new session parameter.
{% endhint %}

{% hint style="warning" %}
**Disclaimer:** The session time parameter does not guarantee that all of your requests finish before the session ends. The session expires once the time limit is reached, even if some requests are still in progress.
{% endhint %}

#### **Credentials list example:**

Example represents a list of credentials that establish different sessions with different sessions time (minutes).

```
customer-USERNAME-cc-DE-sessid-qwert-sesstime-10:PASSWORD
customer-USERNAME-cc-US-sessid-asdfg-sesstime-20:PASSWORD
customer-USERNAME-cc-GB-sessid-zxcvb-sesstime-30:PASSWORD
```

#### Code example

We chose the same German IP as in the previous example, this time we are adding `sessid`string and `sesstime` parameter for 7 minutes:

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

```shell
curl -x pr.oxylabs.io:7777 -U "customer-USERNAME-cc-DE-sessid-abcd1234-sesstime-7:PASSWORD" https://ip.oxylabs.io/location
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
$username = 'USERNAME';
$password = 'PASSWORD';
$country = 'DE';
$session = mt_rand();
$sesstime = 7;
$proxy = 'pr.oxylabs.io:7777';
$query = curl_init('https://ip.oxylabs.io/location');
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;
?>
```

{% endtab %}

{% tab title="Python" %}

```python
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:%s@pr.oxylabs.io: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://ip.oxylabs.io/location').read())
```

{% endtab %}

{% tab title="Java" %}

```java
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://ip.oxylabs.io/location"));
        } finally { client.close(); }
    }
}
```

{% endtab %}

{% tab title="C#" %}

```csharp
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/location"));
    }
}
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
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://ip.oxylabs.io/location")
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
```

{% endtab %}
{% endtabs %}

To set up sticky proxy entry nodes, learn more [**here**](/products/proxies/mobile-proxies/session-control/sticky-proxy-entry-nodes.md).

### Control session IP rotation

The `sessid_oneip` parameter is an alternative to `sessid` for cases where you must keep the exact same IP for the entire session.

With a regular `sessid`, if the assigned exit node goes offline before the session time expires, the system automatically assigns a new IP and your session continues. With `sessid_oneip`, the session is bound to a single exit node: once that IP is no longer available, instead of rotating to a new one, the request fails with an HTTP `502` response. At that point, start a new session with a fresh `sessid_oneip` value. `sessid_oneip` can be combined with `sesstime` in the same way as `sessid`.

{% hint style="info" %}
**Note:** `sessid_oneip` is an optional alternative to `sessid`. The default `sessid` behavior (automatic rotation to a new IP when the exit node expires) is unchanged.
{% endhint %}

#### **Credentials list example:**

The following credentials each establish a separate single-IP session, with a different country and session time:

```
customer-USERNAME-cc-DE-sessid_oneip-qwert-sesstime-10:PASSWORD
customer-USERNAME-cc-US-sessid_oneip-asdfg-sesstime-20:PASSWORD
customer-USERNAME-cc-GB-sessid_oneip-zxcvb-sesstime-30:PASSWORD
```

#### Code example

This example uses a German (`DE`) IP with `sessid_oneip-qwert` and a `sesstime` of 10 minutes. The IP stays fixed for the session; if it becomes unavailable, the request returns HTTP `502` instead of rotating:

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

```shell
curl -x pr.oxylabs.io:7777 -U "customer-USERNAME-cc-DE-sessid_oneip-qwert-sesstime-10:PASSWORD" https://ip.oxylabs.io/location
```

{% endtab %}
{% endtabs %}

To use another language, take any `sessid` sample from the sections above and replace `sessid` with `sessid_oneip` in the login string.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://developers.oxylabs.io/products/proxies/mobile-proxies/session-control.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
