0

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!

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Sounds easy enough. I am new to Java. How would I go about using the read() method?
There are lots of examples about "read the contents from an inputstream"
1

You must read() from the InputStream.

2 Comments

Sounds easy enough. I am new to Java. How would I go about using the read() method?
see docs.oracle.com/javase/7/docs/api/java/io/InputStream.html for details on how to use the IntputStream
-1

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

}

1 Comment

Why can´t he use the (perfectly functional) Java API? And making a system call (creating a new process) for that is a good idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.