简体   繁体   中英

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.

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

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?

Let's say the second path in the same server I need to get data 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)

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...

=> this could be redirect or forward but all the examples I found it is about POST example, I think mine is about GET...

Please aware this is NOT about posting redirect page on 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.

Check out the documentation - 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM