【发布时间】:2015-05-21 07:25:56
【问题描述】:
我是 Java 世界的新手,我正在尝试在同一服务器中打开多个 url.. 我正在使用java spring mvc。
我有一个外部服务器,我需要从服务器的不同位置检索数据..
例如, 外部服务器有这样的网址: http://server.data.com:1234/dataStorage/thisgate/datalist/hereA
1) open the first path with this url : works fine
URL url = new URL(....
URLConnection con = url.openConnection();
HttpURLConnection httpCon = (HttpURLConnection) con;
2) Then authenticate with username, password : works fine
3) I retrieve data and parse it : works fine
4) [问题]:那么,我需要在同一服务器的另一个路径中检索另一个数据流,如何?
假设我需要获取数据的同一服务器中的第二条路径 http://server.data.com:1234/dataStorage/thisgate/datalist/hereB/
所以我的代码是这样的:
public String HTTPConnection(Model model) throws IOException {
//>> 1) open the first path with the url <<//
URL url = new URL("http://server.data.com:1234/dataStorage/thisgate/datalist/hereA/");
URLConnection con = url.openConnection();
HttpURLConnection firstConn = (HttpURLConnection) con;
//>> 2) authentication routine, it works well <<//
//:I am not going to put all detail code here, it works fine.
//>> 3) retrieve data from the url (the first path after authentication) <<//
firstConn.setRequestMethod("GET");
BufferedReader firstPathData = new BufferedReader(new InputStreamReader(firstConn.getInputStream()));
// do the parsing and so on....
//>> 4) open the second path in the same server , How??????? <<//
URL redirectUrl = new URL("http://server.data.com:1234/dataStorage/thisgate/datalist/hereB/");
HttpURLConnection secondConn = (HttpURLConnection) redirectUrl.openConnection();
System.out.println(secondConn.getURL());
BufferedReader secondPathData = new BufferedReader(new InputStreamReader(secondConn.getInputStream()));
.....
......
}
如何在 nr.4 中打开或检索来自同一已连接服务器的第二条路径的其他数据)
我不需要重新连接或重新打开连接... 因为它已经连接并通过了身份验证..
我只需要做 4) 在同一服务器中提供另一条路径并获取流...
=> 这可以是重定向或转发,但我发现的所有示例都是关于 POST 示例,我认为我的示例是关于 GET...
请注意这是不是关于在 jsp 视图上发布重定向页面..
这是为了获取/打开重定向路径并在已连接的服务器中检索另一个数据流
有什么建议吗?提前非常感谢
【问题讨论】:
标签: java spring spring-mvc http-redirect