2013-10-15 173 views
0

我正试图从一个单独的服务器上的一个ASP页面获取信息与Java。通过HTTP访问数据

这里是我目前运行代码:

<%@ page contentType="text/html;charset=UTF-8"%> 
<%@ page import="java.util.*" %> 
<%@ page import="java.text.*" %> 
<%@ page import="java.net.*" %> 
<%@ page import="java.io.*" %> 
<%@ page import="com.nse.common.text.*" %> 
<%@ page import="com.nse.common.admin.*" %> 
<%@ page import="com.nse.common.util.*" %> 
<%@ page import="com.nse.common.config.*" %> 
<%@ page import="com.nse.ms.*" %> 
<% 

     String targetUrl = "http://******/dash_auth/getmsuser.asp"; 
     InputStream r2 = new URL(targetUrl).openStream(); 

%> 

<html> 
<head> 
    <title>get username</title> 
</head> 

<body> 
Return Info = <%=r2%> 
</body> 
</html> 

这就是我找回

Return Info = [email protected]f555 

我希望能得到一个用户名后面,而不是此连接字符串。关于如何获得我的其他页面的实际输出的任何建议将会非常有帮助!

回答

2

当你做<%=r2%>,你得到的是out.print(r2.toString()),这只是给实例的描述

使用该方法从InputStream中读取,以获得服务器结果。

+0

听起来很简单。我是Java新手。我将如何去使用read()方法? –

+0

有很多关于“从输入流中读取内容”的例子。 – SJuan76

1

您必须从InputStream中读取()。

+0

听起来很容易。我是Java新手。我将如何去使用read()方法? –

+0

有关如何使用IntputStream的详细信息,请参阅http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html – coyote

-1

您需要使用http客户端创建连接并获取内容。或者对卷曲等OS实用程序进行系统调用。

这里是如何。如果你想做到这一点在非托管的方式来使用HTTP客户端

http://hc.apache.org/httpclient-legacy/tutorial.html

的样本,这是一个工作示例:

public class URLStreamExample { 

public static void main(String[] args) { 
    try { 
     URL url = new URL("http://www.google.com"); 
     InputStream is = url.openStream(); 
     byte[] buffer = new byte[2048]; 
     StringBuilder sb = new StringBuilder(); 
     while (is.read(buffer) != -1){ 
      sb.append(new String(buffer)); 
     } 

     System.out.println(sb.toString()); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
    } catch (IOException e) { 
     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
    } 
} 

}

+1

为什么他不能使用(功能完善的)Java API?为此创建一个系统调用(创建一个新进程)是一个好主意? – SJuan76