【发布时间】:2014-07-09 05:01:45
【问题描述】:
Java 程序员们。我一直面临着使用 SSH 连接到服务器从另一台服务器加载网页的任务。很快我发现我对网络协议的了解非常有限。首先,我尝试了动态端口转发 - 无济于事,它仅在商业图书馆中可用(而且所有这些都远远超出了我的经济能力)。然后我了解到,使用 JSch,您实际上可以创建一个称为直接 tcp-IP 通道的东西(我仍在尝试掌握这个概念),并且我在 stackoverflow 上找到了一些代码,应该使用它来发送 HTTP通过 SSH 连接到使用 JSch 的另一台服务器向远程服务器发出请求。这是代码(在TCP Connection over a secure ssh connection的原始版本中略有修改)
String host = "66.104.230.49";
String user = "admin";
String password = "default";
int port = 22;
String remoteHost = "souzpp.ru";
int remotePort = 80;
int localPort = 5001;
int assignedPort;
String localHost = "127.0.0.1";
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();
assignedPort = session.setPortForwardingL(localPort, remoteHost, remotePort);
Channel channel = session.openChannel("direct-tcpip");
System.out.println(assignedPort);
((ChannelDirectTCPIP)channel).setHost(localHost);
((ChannelDirectTCPIP)channel).setPort(assignedPort);
String cmd = "GET /files/inst 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();
}
此代码引发以下异常:com.jcraft.jsch.JSchException:通道未打开。 这里可能有什么问题?拜托,如果答案与网络逻辑中的错误有关,而不是与实施中的错误有关,如果您给我一个链接,其中包含有关我所犯错误的有用信息,我将非常高兴。
(编辑:按照 JavaCoderEx 的建议添加了端口转发)
【问题讨论】: