【问题标题】:Java JSch create channel to access remote server via HTTP(s)Java JSch 创建通道以通过 HTTP(s) 访问远程服务器
【发布时间】:2014-05-24 02:00:52
【问题描述】:

使用http://www.jcraft.com/jsch/examples/StreamForwarding.java.html 处的代码,我尝试创建使用JSch 连接到SSH 服务器的代码,然后使用它连接到Web 中的服务器并将HTTP(s) 请求传递给服务器,基本上使用 SSH 服务器作为代理。这是我所拥有的:

    //SSH server and credentials
    String host = "00.000.000.00";
    String user = "login";
    String password = "password";
    int port = 22;

    //The host I want to send HTTP requests to - the remote host
    String remoteHost = "test.com";
    int remotePort = 80;

    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");

    try { 
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig(config);
        session.connect();

        Channel channel = session.getStreamForwarder(remoteHost, remotePort);

        //The GET request
        String cmd = "GET /foo/foo HTTP/1.0\r\n\r\n";

        InputStream in = channel.getInputStream();
        OutputStream out = channel.getOutputStream();

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

        out.flush();
        channel.disconnect();
        session.disconnect();

        System.out.println("Request supposed to have been sent");

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

    } catch (Exception e){
        e.printStackTrace();
    }

这在理论上听起来不错,但实际上它不起作用。我认为我可能可以实现本地端口转发以使其工作,但我不明白如何。有人可以帮帮我吗?

【问题讨论】:

  • 你说“它不起作用”。它以什么方式不起作用?您收到错误消息吗?例外?还有什么?失败时正在执行程序的哪一行?
  • 连接不上。就如此容易。 test.com/foo/foo 上有一个记录器,它没有记录脚本的访问。

标签: java http ssh jsch channel


【解决方案1】:
Channel channel = session.getStreamForwarder(remoteHost, remotePort);
[...]
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
[...]
while ((numRead = is.read(bytes)) >= 0)
    out.write(bytes, 0, numRead);
[...]
channel.disconnect();
session.disconnect();
[...]
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
for (String line; (line = reader.readLine()) != null;){

您正在关闭通道并在发送 HTTP 命令后立即删除 SSH 会话。如果远程 HTTP 服务器没有记录请求,很可能是因为您在远程 sshd 实例有机会将请求转发到 HTTP 服务器之前中断了会话。

从您已经关闭的频道读取响应也不起作用。

在完成之前不要关闭频道或会话。

【讨论】:

    【解决方案2】:

    在执行 'channel.disconnect()' 之前从 'in' 流中读取数据怎么样?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-04
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 2014-07-09
      • 2015-02-13
      • 1970-01-01
      相关资源
      最近更新 更多