How can I read an ASP page from my Java code?

Tim Rohaly

I have found that Microsoft's IIS web server requires a full URL in the GET command - not just a path. This is something you have to watch out for if you are accessing URLs directly from a Socket. However, if you are using URLConnection, this seems to be done properly for you. So your problem in this case is probably a typo in the URL you are entering, or something of that sort.

For example, running the following program using the command:

    java URLReader http://www.microsoft.com/windows2000/
        downloads/recommended/sp1/default.asp
will correctly return the contents of the specified ASP page.
import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) {
        try {
            URL url = new URL(args[0]);
            URLConnection con = url.openConnection();

            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    con.getInputStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null)
                    System.out.println(inputLine);

            in.close();
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact