proxy en java
Hola
Estoy intentando hacer un proxy en java para http. El programa me hace las conexiones correctamente,
pero tengo problemas al descargar algunas imagenes y de hecho tambien algunas paginas. Este es el codigo,
quiza alguien pueda orientarme un poco y sabe porque ocurre esto.
import java.awt.*;
import java.net.*;
import java.io.*;
public class Proxy {
public static void connectar(FileWriter fw) {
ServerSocket ss = null;
String nomWeb=null;
String dades=null;
PrintWriter sortida=null;
InetAddress inet=(InetAddress)null;
int port=0;
String web;
Socket sk=null;
try { ss = new ServerSocket(8080);}
catch(Exception e) {System.out.println("error al abrir el socket" + e);}
while (true) {
try {
sk=ss.accept(); //socket de lectura
inet = sk.getInetAddress();
port = sk.getPort();
String host = inet.getHostName();
//prueba
System.out.println("PORT (origen): "+port);
System.out.println("HOST (client): "+host);
//leyendo datos del socket
dades = getDades(sk);
nomWeb = getNom(dades);
//hacemos la connexion con la web
web= connectarWeb(nomWeb, dades);
//enviar sotida al client
sortida= new PrintWriter(new OutputStreamWriter(sk.getOutputStream()));
sortida.println(web);
sortida.flush();
sk.close();
}
catch(Exception e) {System.out.println("error al abrir el socket" + e);}
System.out.println("escuchando");
}
}
public static String getDades(Socket s) { //1r
String dades="";
String linea="";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//guardant les dades dela peticio
while (((linea=br.readLine()).length()!=0)) {
dades+=linea+"n";}
System.out.println("peticion n"+dades);
//br.close();
} catch (Exception e) {System.out.println("Error respuestaConexion" + e);}
return dades;
}
public static String getNom(String dades) {
String nom=null;
try {
int longitutDades = dades.length();
byte dato;
byte [] cadena= new byte[longitutDades];
byte [] nomW= new byte[5000];
int i = 0;
int a = 0;
cadena=dades.getBytes();
while ((cadena[i]!=47) && i<longitutDades-1) i++;
i=i+2;
while ((cadena[i]!=47) && i<longitutDades-1) {
nomW[a]=cadena[i];
a++;
i++;
}
nom = new String (nomW,0,a);
System.out.println("HOST (desti) --> "+ nom);
} catch (Exception e) {System.out.println("Error getNom " + e);}
return nom;
}
public static String connectarWeb(String nomWeb, String dades) {
int pdst=80; //port destino
String resposta="";
try {
//crear socket
Socket sw = new Socket(nomWeb, pdst);
//canal d'entrada
BufferedReader ent = new BufferedReader(new InputStreamReader(sw.getInputStream()));
//canal de salida
PrintWriter sort = new PrintWriter(sw.getOutputStream());
//envia comando GET al servidor
sort.println(dades);
sort.flush();
// cadena donde se lee la respuesta
String line = null;
//lee la respuesta
while ((line = ent.readLine()) != null) {
resposta+=line+"n";
}
//System.out.println("respostan"+resposta );
FileWriter fl = new FileWriter(".\log.htm");
fl.write(resposta);
//cerramos socket
sw.close();
} catch(IOException e) {System.out.println("Error conectarWeb" + e);}
return resposta;
}
public static void main(String args[]) {
String web=null;
try {
FileWriter fw = new FileWriter(".\log.txt");
System.out.println("conectando"); //prueba
connectar(fw);
}
catch(IOException ioE) {System.out.println("error al crear el log");}
}
}
Estoy intentando hacer un proxy en java para http. El programa me hace las conexiones correctamente,
pero tengo problemas al descargar algunas imagenes y de hecho tambien algunas paginas. Este es el codigo,
quiza alguien pueda orientarme un poco y sabe porque ocurre esto.
import java.awt.*;
import java.net.*;
import java.io.*;
public class Proxy {
public static void connectar(FileWriter fw) {
ServerSocket ss = null;
String nomWeb=null;
String dades=null;
PrintWriter sortida=null;
InetAddress inet=(InetAddress)null;
int port=0;
String web;
Socket sk=null;
try { ss = new ServerSocket(8080);}
catch(Exception e) {System.out.println("error al abrir el socket" + e);}
while (true) {
try {
sk=ss.accept(); //socket de lectura
inet = sk.getInetAddress();
port = sk.getPort();
String host = inet.getHostName();
//prueba
System.out.println("PORT (origen): "+port);
System.out.println("HOST (client): "+host);
//leyendo datos del socket
dades = getDades(sk);
nomWeb = getNom(dades);
//hacemos la connexion con la web
web= connectarWeb(nomWeb, dades);
//enviar sotida al client
sortida= new PrintWriter(new OutputStreamWriter(sk.getOutputStream()));
sortida.println(web);
sortida.flush();
sk.close();
}
catch(Exception e) {System.out.println("error al abrir el socket" + e);}
System.out.println("escuchando");
}
}
public static String getDades(Socket s) { //1r
String dades="";
String linea="";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//guardant les dades dela peticio
while (((linea=br.readLine()).length()!=0)) {
dades+=linea+"n";}
System.out.println("peticion n"+dades);
//br.close();
} catch (Exception e) {System.out.println("Error respuestaConexion" + e);}
return dades;
}
public static String getNom(String dades) {
String nom=null;
try {
int longitutDades = dades.length();
byte dato;
byte [] cadena= new byte[longitutDades];
byte [] nomW= new byte[5000];
int i = 0;
int a = 0;
cadena=dades.getBytes();
while ((cadena[i]!=47) && i<longitutDades-1) i++;
i=i+2;
while ((cadena[i]!=47) && i<longitutDades-1) {
nomW[a]=cadena[i];
a++;
i++;
}
nom = new String (nomW,0,a);
System.out.println("HOST (desti) --> "+ nom);
} catch (Exception e) {System.out.println("Error getNom " + e);}
return nom;
}
public static String connectarWeb(String nomWeb, String dades) {
int pdst=80; //port destino
String resposta="";
try {
//crear socket
Socket sw = new Socket(nomWeb, pdst);
//canal d'entrada
BufferedReader ent = new BufferedReader(new InputStreamReader(sw.getInputStream()));
//canal de salida
PrintWriter sort = new PrintWriter(sw.getOutputStream());
//envia comando GET al servidor
sort.println(dades);
sort.flush();
// cadena donde se lee la respuesta
String line = null;
//lee la respuesta
while ((line = ent.readLine()) != null) {
resposta+=line+"n";
}
//System.out.println("respostan"+resposta );
FileWriter fl = new FileWriter(".\log.htm");
fl.write(resposta);
//cerramos socket
sw.close();
} catch(IOException e) {System.out.println("Error conectarWeb" + e);}
return resposta;
}
public static void main(String args[]) {
String web=null;
try {
FileWriter fw = new FileWriter(".\log.txt");
System.out.println("conectando"); //prueba
connectar(fw);
}
catch(IOException ioE) {System.out.println("error al crear el log");}
}
}
Tu problema está donde haces:
while ( (line = ent.readLine ()) != null) {
resposta += line + "\n" ;
}
Por un lado, veo que retarda mogollón la respuesta. Y cuando estás leyendo las imágenes, metes saltos de lÃnea dentro de las mismas, con lo que pudres el codigo original.
while ( (line = ent.readLine ()) != null) {
resposta += line + "\n" ;
}
Por un lado, veo que retarda mogollón la respuesta. Y cuando estás leyendo las imágenes, metes saltos de lÃnea dentro de las mismas, con lo que pudres el codigo original.
