Java proxy server for IE

N

Notre Poubelle

Hi,

I hope someone can help me (I am an inexperienced Java programmer).
What I'm trying to do is write a Java Application that acts as a proxy
server for Internet Explorer. The goal of the little Java application
is to be able to log HTTP requests and responses between IE and a web
server.

I've pasted my application as it exists so far. It only handles HTTP
GETs right now, which is fine for my purposes at the moment. It works
for simple HTML pages. The problem is that I can't get it to
read/write graphic information correctly, such as when a web page
requests a gif (the graphic appears messed up in IE). I have 2
questions for all the Java experts out there:
(1) How can I handle binary content so it's properly rendered in the
browser.
(2) Using writeBytes from the DataOuputStream class seems to write one
byte at time, even though I'm passing in a string. I'd like a method
that writes in batches to the socket endpoint. Is that possible?

Thanks for any and all help!

import java.io.*;
import java.net.*;

class SimpleProxy {
public static void main(String[] args) {

ServerSocket ss = null;
Socket s = null; // client socket connection with client (browser)
DataOutputStream os = null; //client (browser) output;
DataInputStream is = null; //client (browser) input
String sInput;
int iPosURLEnd;
String sURL;
Socket sServ = null; //client socket connection with server (web
server)
DataOutputStream osServ = null; //server (web server) output;
DataInputStream isServ = null; //client (web server) input
int iPosHostStart;
String sHost;


System.out.println ("Starting app...\r\n");

try {
ss = new ServerSocket(45678);
}
catch (Exception e) {
System.out.println ("Error establishing ServerSocket
connection.\r\n");
System.out.println ("Error: " + e.getMessage());

}

for (; ;) {
try {
s = ss.accept();
System.out.println("Connection established.\r\n");
os = new DataOutputStream(s.getOutputStream());
is = new DataInputStream(s.getInputStream());
}
catch (Exception e) {
System.out.println ("Error establishing connection.\r\n");
System.out.println ("Error: " + e.getMessage());
}

System.out.println("--------- Read REQUEST from client
--------\r\n");
for (; ;) {
try {

sInput = is.readLine();

if (sInput == null)
break;
System.out.println("Client: " + sInput);
if (sInput.startsWith("GET",0)) {
iPosURLEnd = sInput.indexOf(" ",4); // 4 = Len("GET ")
sURL = sInput.substring(4, iPosURLEnd);
iPosHostStart = sURL.indexOf("://") + 3; // 3 = Len("://")
sHost = sURL.substring(iPosHostStart,
sURL.indexOf("/",iPosHostStart+1));
try {
sServ = new Socket(sHost, 80); //assume port 80
osServ = new
DataOutputStream(sServ.getOutputStream());
isServ = new DataInputStream(sServ.getInputStream());
}
catch (Exception e) {
System.out.println ("Error establishing connection with
server.\r\n");
System.out.println ("Error: " + e.getMessage());
}

}
osServ.writeBytes(new String(sInput + "\r\n"));
if (sInput.compareTo("") == 0)
break;

}
catch (Exception e) {
System.out.println ("Error reading information from
client.\r\n");
System.out.println ("Error: " + e.getMessage());
}
}

System.out.println("--------- Write RESPONSE from server
--------\r\n");

for (; ;) {
try {
sInput = isServ.readLine();
if (sInput == null)
break;
System.out.println("Server: " + sInput);
os.writeBytes(new String(sInput + "\r\n"));
}
catch (Exception e) {
System.out.println ("Error reading information from
server.\r\n");
System.out.println ("Error: " + e.getMessage());
}

}


try {
isServ.close();
osServ.close();
sServ.close();
is.close();
os.close();
s.close();
}
catch (IOException e) {
System.out.println ("Error closing sockets.\r\n");
System.out.println ("Error: " + e.getMessage());
}


}

}

}
 
R

Robert Olofsson

: Hi,

: I hope someone can help me (I am an inexperienced Java programmer).
: What I'm trying to do is write a Java Application that acts as a proxy
: server for Internet Explorer. The goal of the little Java application
: is to be able to log HTTP requests and responses between IE and a web
: server.

If all you want to do is to log requests, then I suggest you use
an existing solution like the squid proxy (written in C) or if
you really want one in java RabbIT (http://www.khelekore.org/rabbit/)
is a HTTP/1.1 compliant proxy (which I have written).

Why do you still want to write your own proxy?

/robo
 
N

Notre Poubelle

I have two reasons for writing my own proxy. One, I need to log all
HTTP traffic between IE and a webserver to track down an intermittent
problem I'm troubleshooting. And two, I thought it would be a good
opportunity to learn a little bit of Java (and Java seemed easier to
use for the task than C++).
 
R

Robert Olofsson

: I have two reasons for writing my own proxy. One, I need to log all
: HTTP traffic between IE and a webserver to track down an intermittent
: problem I'm troubleshooting.

Ok, _IF_ you really want to log the data between IE and a server
then I would suggest that you use ethereal or another similar network
data sniffer tool.
If you use a proxy IE will talk differently so it may be that you will
not see the problem...
If you aim to write a fully HTTP/1.1 compliant proxy then plan good,
it will take lots of time (yes, I do know, I have written such a
proxy, of course if you skip caching its easier).

: And two, I thought it would be a good
: opportunity to learn a little bit of Java (and Java seemed easier to
: use for the task than C++).

That is a valid point, wanting to learn is always a good reason....

Have fun
/robo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top