Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
123 views

SourceCode Proxy HTTP

The document contains code examples for creating a proxy HTTP client and socket client in Java, as well as adding authentication to the clients. The proxy HTTP client code connects to a URL through a proxy, reads the response, and prints it out. The socket client code connects to a host and port through a SOCKS proxy, sends an HTTP request, and prints the response. The authenticated clients add proxy authentication by encoding the username and password and sending it in the request header.

Uploaded by

Fakhri Malja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

SourceCode Proxy HTTP

The document contains code examples for creating a proxy HTTP client and socket client in Java, as well as adding authentication to the clients. The proxy HTTP client code connects to a URL through a proxy, reads the response, and prints it out. The socket client code connects to a host and port through a SOCKS proxy, sends an HTTP request, and prints the response. The authenticated clients add proxy authentication by encoding the username and password and sending it in the request header.

Uploaded by

Fakhri Malja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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);
}
}
}

You might also like