Austin Lovell
Austin Lovell

Reputation: 1049

Accessing data over HTTP

I am attempting to get information from an asp page on a separate server with java.

Here is what I am currently running for code:

<%@ 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>

And this is what I am getting back

Return Info = sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@5fc9f555

I am hoping to get a username back and not this connection string. Any suggestions on how to get the actual output of my other page would be very helpful!

Upvotes: 0

Views: 71

Answers (3)

nucatus
nucatus

Reputation: 2296

You need to use an http client to create a connection and get the content back. Or make a system call to a OS utility like curl.

Here is a sample on how to use the http Client

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

If you want to do it in an unmanaged way, this is a working example:

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

}

Upvotes: -1

SJuan76
SJuan76

Reputation: 24885

When you do <%=r2%>, what you get is out.print(r2.toString()), which just gives a description of the instance.

Use the methods to read from an InputStream, to get the server result.

Upvotes: 2

coyote
coyote

Reputation: 167

You must read() from the InputStream.

Upvotes: 1

Related Questions