I want a proxy in java that get response of a server in socket and change the html code and then pass the client as a response in socket connection.
Now My program can get request from client and can get response that from server and pass that to client Properly.
but I can not change the html code.This is my code:
Thread thread = new Thread() {
public void run() {
int bytesRead;
try {
while ((bytesRead = streamFromClient.read(request)) != -1) {
streamToServer.write(request, 0, bytesRead);
streamToServer.flush();
}
Logging incomingLog = new Logging("Incoming", tmpClient.toString());
incomingLog.doLog();
} catch (IOException e) {
Logging IOExceptionLog = new Logging("Error", "Proxy cannot read client request - Client: "
+ tmpClient.toString() + ".\nException : " + e.getMessage() + "\n");
try {
IOExceptionLog.doLog();
} catch (IOException e1) {
//Ignore me!!!
}
}
// the client closed the connection to us, so close our
// connection to the server.
try {
streamToServer.close();
} catch (IOException e) {
Logging log = new Logging("Error", "Proxy could not close connection to server.");
try {
log.doLog();
} catch (IOException e1) {
//Ignore me!!!
}
}
}
};
thread.start();// Start the client-to-server request thread running;
// Read the server's responses
// and pass them back to the client;
int bytesRead;
InputStream tempStreamFromServer = streamFromServer;
/*ConvertStream convertor = new ConvertStream();
String htmlCode = convertor.getStringFromInputStream(tempStreamFromServer);*/
try {
while ((bytesRead = streamFromServer.read(response)) != -1) {
streamToClient.write(response, 0, bytesRead);
streamToClient.flush();
}
Logging incomingLog = new Logging("OutComing", tmpClient.toString());
incomingLog.doLog();
} catch (IOException e) {
Logging IOExceptionLog = new Logging("Error", "Proxy cannot send client response - Client: "
+ tmpClient.toString() + ".\nException : " + e.getMessage() + "\n");
IOExceptionLog.doLog();
}
// The server closed its connection to us, so we close our
// connection to our client.
streamToClient.close();
Can anyone help me?