- Forward


Proxies in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • A Common Situation:
    • An application must communicate through a proxy
  • The Layers/Protocol:
    • At the application layer HTTP requests might need to be sent through an HTTP proxy
    • At the transport layer TCP segments (or UDP segments) may need to be sent through a SOCKS proxy
The Participants
Back SMYC Forward
  • The Abstract SocketAddress Class:
    • Encapsulates a socket address with no protocol
  • The Proxy Class:
    • Encapsulates a proxy setting/protocol and a socket address
  • The Abstract URLConnection Class:
    • Encapsulates an abstract communications link with a URL
Proxy Types
Back SMYC Forward
  • Actual Proxies:
    • Proxy.Type.HTTP
    • Proxy.Type.SOCKS
  • No Proxy:
    • Proxy.Type.DIRECT
The Mechanism
Back SMYC Forward
  • Getting Started:
    • Construct a concrete specialization of a SocketAddress
    • Construct a Proxy
  • Using the Proxy:
    • Construct a concrete speciaization of a URLConnection or a Socket
    • Use the URLConnection or Socket to connect() through the Proxy
The Mechanism (cont.)
Back SMYC Forward

An HTTP Example

// Getting Started SocketAddress address = new InetSocketAddress(proxyname, proxyport); Proxy proxy = new Proxy(Proxy.Type.HTTP, address); // Using the Proxy URL url = new URL(resource); URLConnection conn = url.openConnection(proxy); // Use the URLConnection
The Mechanism (cont.)
Back SMYC Forward

A TCP Example

// Getting Started SocketAddress address = new InetSocketAddress(hostname, port); Proxy proxy = new Proxy(Proxy.Type.SOCKS, address); // Using the Proxy Socket socket = new Socket(proxy); SocketAddress server = new InetSocketAddress(servername, serverport); socket.connect(server); // Use the Socket
Dynamic Proxy Selection
Back SMYC Forward
  • Possible Uses:
    • Load balancing across proxies
    • Different proxies for different hosts
  • The Approach:
    • Install a ProxySelector (by calling its static setDefault() method) that has a select() method that is called by the protocol handler to determine which proxy to use
There's Always More to Learn
Back -