【问题标题】:How to open multiple URLs path in a same server in java springjava spring 如何在同一服务器中打开多个URL路径
【发布时间】: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


【解决方案1】:

如果您使用简化与 Web 服务器通信的 Spring RestTemplate 应该会更容易。

查看文档 - http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

在您的情况下,您应该根据需要执行尽可能多的请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2016-07-05
    相关资源
    最近更新 更多