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

不羁的心 提交于 2019-12-02 13:12:49
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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!