http请求通过ssh隧道

时间:2014-06-28 13:53:35

标签: java ssh jsch ssh-tunnel

我有两个服务器。我使用第一个服务器安装了tor应用程序来隐藏我的ip地址。从此以后它将被命名为SSH隧道服务器。第二个服务器是在ssh隧道上发送http请求。它将被命名为Client Sever。 为了上述目的,我在下面写了代码。但我无法读取SSH隧道服务器响应。我在哪里做错了?

            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");



            session.connect();

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

1 个答案:

答案 0 :(得分:0)

您没有在GET命令后输出任何行终止符。尝试将GET字符串更改为:

String cmd = "GET / HTTP/1.1\r\n\r\n";