Java JSch create channel to access remote server via HTTP(s)

前端 未结 2 1074
無奈伤痛
無奈伤痛 2021-01-29 08:00

Using the code at http://www.jcraft.com/jsch/examples/StreamForwarding.java.html, I have tried to create code that uses JSch to connect to an SSH server, then use it to connect

相关标签:
2条回答
  • 2021-01-29 08:27
    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;){
    

    You are closing the channel and dropping the SSH session immediately after sending the HTTP command. If the remote HTTP server isn't logging the request, it's very possibly because you dropped the session before the remote sshd instance had a chance to forward the request to the HTTP server.

    Reading the response from the channel which you've already closed isn't going to work either.

    Don't close the channel or the session until you're done with them.

    0 讨论(0)
  • 2021-01-29 08:49

    How about reading data from 'in' stream before executing 'channel.disconnect()'?

    0 讨论(0)
提交回复
热议问题