Discussion:
[JSch-users] http request
Ali Koyuncu
2014-06-28 13:41:31 UTC
Permalink
I have two server. I use first server installed tor application on it for
hide my ip adress. It will be named as SSH Tunnel Server henceforth. Second
server is to send http request on ssh tunnelling. And it will be named as
Client Sever.
I wrote the codes below, for the purpose of above. But I can't read SSH
Tunnel Server response. Where Am I doing wrong ?

String user = "root";
String password = "password";
String host = "198.199."11.111";
int port=22;

JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");


session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");
Channel channel=session.openChannel("direct-tcpip");


((ChannelDirectTCPIP)channel).setHost("whatismyipaddress.com");
((ChannelDirectTCPIP)channel).setPort(80);

String cmd = "GET / HTTP/1.1" ;

InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect(10000);

byte[] bytes = cmd.getBytes();
InputStream is = new ByteArrayInputStream(cmd.getBytes("UTF-8"));

int numRead;

while ( (numRead = is.read(bytes) ) >= 0) {

out.write(bytes, 0, numRead);
System.out.println(numRead);
}

out.flush();



try {
BufferedReader reader = new BufferedReader(new
InputStreamReader(in));
for (String line; (line = reader.readLine()) != null;){
System.out.println(line);
}
} catch (java.io.IOException exc) {
System.out.println(exc.toString());
}
channel.disconnect();
session.disconnect();
System.out.println();


--

Loading...