0

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

user1915570
  • 386
  • 2
  • 7
  • 27
  • I think you're referring to [Persistent Connections](http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html). Checkout this post on [persistent-httpurlconnection-in-java](http://stackoverflow.com/questions/3304006/persistent-httpurlconnection-in-java) – asgs Mar 18 '15 at 13:44

1 Answers1

0

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.

Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29