• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Dealing with a simple web proxy in java

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 /127.0.0.1
null
Accepted connection from /127.0.0.1
GET http://stackoverflow.com/questions/12900825/how-do-i-forward-the-http-request-back-to-the-browser-proxy-java HTTP/1.0
Host: stackoverflow.com
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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lawrence Tan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From your code, you seem not to send the whole http request to the server, only what's in the "str" variable...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lawrence, were you able to resolve the coding issue you faced? I'm curious about how you managed the output request.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic