Code Examples
Basic code examples for popular programming languages
Choosing your programming language
Smartproxy offers code examples for 7 of the most used programming languages on the market:
Need more examples? Visit our Github page.
Setting up the code
To set up our proxies, simply choose the code according to the programming language you wish to use in the table below:
import requests
url = 'http://ip.smartproxy.com/json'
username = 'username'
password = 'password'
proxy = f'http://{username}:{password}@gate.smartproxy.com:7000'
response = requests.get(url, proxies={'http': proxy, 'https': proxy})
print(response.text)
const axios = require("axios");
const url = "https://ipinfo.io/ip";
const resp = axios.get(url, {
proxy: {
host: 'gate.smartproxy.com',
port: 7000,
auth: {
username: 'username',
password: 'password'
}
}
}). then(resp => {
console.log(resp.data);
});
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class ProxyTest
{
public static void main(String[] args) throws Exception
{
InetSocketAddress proxyAddress = new InetSocketAddress("gate.smartproxy.com", 7000); // Set proxy IP/port.
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
URL url = new URL("http://ip.smartproxy.com"); //enter target URL
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("username","password".toCharArray())); //enter credentials
}
};
Authenticator.setDefault(authenticator);
URLConnection urlConnection = url.openConnection(proxy);
//Scanner to view output
Scanner scanner = new Scanner(urlConnection.getInputStream());
System.out.println(scanner.next());
scanner.close();
}
}
<?php
$username = 'username';
$password = 'password';
$proxy = 'gate.smartproxy.com:7000';
$target = curl_init('http://ip.smartproxy.com/');
curl_setopt($target, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($target, CURLOPT_PROXY, $proxy);
curl_setopt($target, CURLOPT_PROXYUSERPWD, "$username:$password");
$result = curl_exec($target);
curl_close($target);
if ($result)
echo $result;
?>
require "uri"
require 'net/http'
proxy_host = 'gate.smartproxy.com'
proxy_port = 7000
proxy_user = 'username'
proxy_pass = 'password'
uri = URI.parse('http://ip.smartproxy.com/json')
proxy = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass)
req = Net::HTTP::Get.new(uri.path)
result = proxy.start(uri.host, uri.port) do |http|
http.request(req)
end
puts result.body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
Task t = new Task(DownloadPageAsync);
t.Start();
Console.ReadLine();
}
static async void DownloadPageAsync()
{
string page = "http://ip.smartproxy.com/json";
var proxy = new WebProxy("gate.smartproxy.com:7000")
{
UseDefaultCredentials = false,
Credentials = new NetworkCredential(
userName: "username",
password: "password")
};
var httpClientHandler = new HttpClientHandler()
{
Proxy = proxy,
};
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();
}
}
}
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
)
const (
resourceUrl = "http://ip.smartproxy.com/json"
proxyHost = "gate.smartproxy.com:7000"
username = "username"
password = "password"
)
func main() {
proxyUrl := &url.URL{
Scheme: "http",
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()
var body map[string]interface{}
if err = json.NewDecoder(resp.Body).Decode(&body); err != nil {
log.Fatal(err)
}
fmt.Println(body)
}
Changing parameters
To edit your connection details, change the according lines in your code:
url = 'http://ip.smartproxy.com/json'
username = 'username'
password = 'password'
const url = "https://ipinfo.io/ip";
username: 'username',
password: 'password'
InetSocketAddress proxyAddress = new InetSocketAddress("gate.smartproxy.com", 7000); // Set proxy address and port
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
URL url = new URL("http://ip.smartproxy.com"); //enter target URL
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("username","password".toCharArray())); //enter credentials
$username = 'username';
$password = 'password';
$proxy = 'gate.smartproxy.com:7000';
$target = curl_init('http://ip.smartproxy.com/json');
proxy_host = 'gate.smartproxy.com'
proxy_port = 7000
proxy_user = 'username'
proxy_pass = 'password'
uri = URI.parse('http://ip.smartproxy.com/json')
string page = "http://ip.smartproxy.com/json";
var proxy = new WebProxy("gate.smartproxy.com:7000")
{
UseDefaultCredentials = false,
Credentials = new NetworkCredential(
userName: "username",
password: "password")
};
resourceUrl = "http://ip.smartproxy.com/json"
proxyHost = "gate.smartproxy.com:7000"
username = "username"
password = "password"
Make sure that the endpoints and username:password
stays within punctuation marks ' '.
Support
Need help or just want to say hello? Our customer support is available 24/7. You can also reach us anytime via email at [email protected].
Updated 19 days ago