1. Choosing your programming language
Smartproxy offers code examples for 7 most used programming languages on the market:
Need more examples? Visit our Github page.
2. Setting up the code
To set up our proxies simply choose the code accordingly to the programing language you wish to use in the table below:
import urllib.request as request
url = 'http://ip.smartproxy.com/json'
username = 'username'
password = 'password'
proxy = f'http://{username}:{password}@gate.smartproxy.com:7000'
query = request.build_opener(request.ProxyHandler({'http': proxy, 'https': proxy}))
print(query.open(url).read())
require('request-promise') ( {
url: 'http://ip.smartproxy.com/json',
proxy: 'http://username:[email protected]:7000'
}).then(
function(data ){
console.log(data);
},
function(err){
console.error(err);
}
);
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/json"); //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)
}
3. Changing parameters
To edit your connection details, change the according lines in your code:
url = 'http://ip.smartproxy.com/json'
username = 'username'
password = 'password'
url: 'http://ip.smartproxy.com/json',
proxy: 'http://username:[email protected]:7000'
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/json"); //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 in within the punctuation marks(''
).
Updated 3 months ago