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