[英]How to open multiple URLs path in a same server in java spring
I am new to java world and I am trying to open multiple urls in a same server.. I am using java spring mvc. 我是Java世界的新手,我想在同一服务器上打开多个URL。我正在使用Java Spring MVC。
I have an external server which I need to retrieve data from different location in the server.. 我有一个外部服务器,我需要从服务器中的其他位置检索数据。
For example, The external server have urls like this : http://server.data.com:1234/dataStorage/thisgate/datalist/hereA 例如,外部服务器具有如下网址: 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) [Question]: Then, I need to retrieve another data stream in another path in the same server, How? 4)[问题]:然后,我需要在同一服务器的另一条路径中检索另一条数据流,怎么办?
Let's say the second path in the same server I need to get data http://server.data.com:1234/dataStorage/thisgate/datalist/hereB/ 假设同一台服务器中的第二条路径我需要获取数据 http://server.data.com:1234/dataStorage/thisgate/datalist/hereB/
so my code is like this: 所以我的代码是这样的:
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()));
.....
......
} }
How can I open or retrieve other data from the second path in the same-already-connected server in nr.4) 如何在nr.4中的同一连接的服务器中从第二条路径打开或检索其他数据)
I don't need to reconnect or re-openConnection... Because it is already connected and authenticated.. 我不需要重新连接或重新打开连接...,因为它已经连接并已通过身份验证。
I just need to do 4) give another path in the same server & get stream... 我只需要做4)在同一服务器上给另一个路径并获取流...
=> this could be redirect or forward but all the examples I found it is about POST example, I think mine is about GET... =>这可以重定向或转发,但是我发现的所有示例都是关于POST的示例,我认为我的是关于GET的...
Please aware this is NOT about posting redirect page on jsp View.. 请注意,这与在jsp View上发布重定向页面无关。
This is to get/open the redirected path and retrieve another data stream in already connected server 这是为了获取/打开重定向路径并在已连接的服务器中检索另一个数据流
Any advices? 有什么建议吗? Thanks a lot in advance 在此先多谢
Should be easier if you use Spring RestTemplate which simplify communication with web server. 如果使用Spring RestTemplate可以简化与Web服务器的通信,应该会更容易。
Check out the documentation - http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html 查看文档-http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html
In your case you should execute as much requests as you need. 在您的情况下,您应该根据需要执行尽可能多的请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.