File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Dealing with a simple web proxy in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Dealing with a simple web proxy in java" Watch "Dealing with a simple web proxy in java" New topic

Dealing with a simple web proxy in java

Lawrence Tan

Joined: Oct 17, 2012
Posts: 3
Hi All, I am dealing with a Proxy Assignment that

1) Uses a FireFox Browser --> Send request to Proxy (My program) --> Forward Request to HTTP Server
2) HTTP Server ---> Respond to Proxy (My Program) ---> Respond send back to FireFox Browser to load page

This is my code :

This is my Output :

Creating server socket on port 8128
Accepted connection from /
Accepted connection from /
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101 Firefox/15.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Proxy-Connection: close
Pragma: no-cache

After getting this information, i have trouble forwarding this back to the browser to load the page. Am not sure whats wrong, i tried opening another socket but it doesnt seems to work.
Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

A very basic approach would require you to have a ServerSocket to accept a client request on some port, which will yield you a Socket connection to the client. You can then can read the HTTP request sent by the client using the socket's InputStream and buffer the request in a byte array. Do not close the Socket connection to the client! After that you'll need to parse the Host: request header to get the host address and port (typically 80) and use that information to create a new Socket connection to the intended host. Once that's done you write out the byte buffer to that Socket's OutputStream and read the response from that Socket's InputStream. There's no real need to buffer the reponse this time, and you can simply write out the bytes you read from the host Socket's InputStream directly to the client Socket's OutputStream, and clean up all the resources when that's done.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Lawrence Tan

Joined: Oct 17, 2012
Posts: 3
thanks for your help! however am still faced witth a situation here, i tried loading youtube, the front page loaded but when i tried accessing the videos with path /watch?tv .... something, youtube displays a page saying video not found. i am guessing my get is wrong. i send GET the entire path name + host, i tried taking out its the same.

i used firebug to track my header request and it says 404.
Don Ho

Joined: Mar 07, 2012
Posts: 9

From your code, you seem not to send the whole http request to the server, only what's in the "str" variable...
Pete Wallace

Joined: May 03, 2013
Posts: 1
Lawrence, were you able to resolve the coding issue you faced? I'm curious about how you managed the output request.
I agree. Here's the link:
subject: Dealing with a simple web proxy in java
jQuery in Action, 3rd edition