SourceCode proxy HTTP
package proxyhttp;
import java.io.*;
import java.io.IOException;
import java.net.*;
public class ProxyHttp {
/**
* @param args the command line arguments
* @throws java.net.MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException, IOException {
try {
String proxyHost = "127.0.0.1";
int proxyPort = 808;
SocketAddress proxyAddr = new InetSocketAddress (
proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddr);
System.out.println("Connecting...");
URL url = new URL ("http://aguskurniawan.net/book/testhalaman.html");
HttpURLConnection con = (HttpURLConnection) url.openConnection(proxy);
con.connect();
System.out.println("Server Respon");
System.out.println(" Response Code: " + con.getResponseCode());
BufferedReader in = new BufferedReader(
new InputStreamReader(
con.getInputStream()));
String line;
System.out.println("Data Respon:");
while ((line = in.readLine()) != null){
System.out.println(line);
}
in.close();
con.disconnect();
} catch (UnknownHostException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}
SourceCode Java Socket
package javasocket;
import java.io.*;
import java.net.*;
public class JavaSocket {
public static void main(String[] args) {
try {
String host = "aguskurniawan.net";
int port = 80;
String proxyHost = "127.0.0.1";
int proxyPort = 1080;
SocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
System.out.println("Connecting...");
InetSocketAddress dest = new InetSocketAddress(host, port);
Socket socket = new Socket(proxy);
socket.connect(dest);
System.out.println("Connected");
System.out.println("Kirim header");
// kirim header
String path = "/book/testhalaman.html";
BufferedWriter wr = new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream(), "UTF8"));
wr.write("GET "+ path +" HTTP/1.0\r\n");
wr.write("Host: "+ host +"\r\n");
wr.write("\r\n");
wr.flush();
// Menunggu response
System.out.println("Hasil Response:");
BufferedReader rd = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
socket.close();
} catch (UnknownHostException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}
SourceCode Proxy Client Autentikasi
package ProxyAuth;
import java.io.*;
import java.io.IOException;
import java.net.*;
import java.util.Base64;
public class SimpleHttpProxyClientAuth {
public static void main(String[] args) {
try {
String proxyHost = "127.0.0.1";
int proxyPort = 808;
SocketAddress proxyAddr = new InetSocketAddress(
proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddr);
System.out.println("Connecting...");
URL url = new URL ("http://aguskurniawan.net/book/testhalaman.html");
HttpURLConnection con = (HttpURLConnection) url.openConnection(proxy);
String proxyAuth = "userdemo:123";
byte[] bytesUserName = Base64.getDecoder().decode(proxyAuth.getBytes());
String encoded = new String(bytesUserName);
con.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
con.connect();
System.out.println("Server Respon:");
System.out.println(" Response Code: " + con.getResponseCode());
BufferedReader in = new BufferedReader (
new InputStreamReader(
con.getInputStream()));
String line;
System.out.println("Data Respon:");
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
con.disconnect();
} catch (UnknownHostException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}
SourceCode Java Socket Proxy Autentikasi
package JavaSocketProxyAuth;
import java.io.*;
import java.net.*;
public class JavaSocketProxyAuth {
public static void main(String[] args) {
try {
String host = "aguskurniawan.net";
int port = 80;
String proxyHost = "127.0.0.1";
int proxyPort = 1080;
final String userName = "userdemo";
final String password = "123";
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password.toCharArray());
}
});
SocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
System.out.println("Connecting...");
InetSocketAddress dest = new InetSocketAddress(host, port);
Socket socket = new Socket(proxy);
socket.connect(dest);
System.out.println("Connected");
System.out.println("Kirim Header");
//kirim header
String path = "/book/testhalaman.html";
BufferedWriter wr = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream(), "UTF8"));
wr.write("GET " + path + "HTTP/1.0\r\n");
wr.write("Host: " + host + "\r\n");
wr.flush();
//menunggu response
System.out.println("Hasil Response");
BufferedReader rd = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
socket.close();
} catch (UnknownHostException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}