Solved

Requesting a URL

Posted on 2004-09-15
9
223 Views
Last Modified: 2010-03-31
I have an application where I need a java program that will open up a URL and cause the web page to be ran.  The java program will be opening a web page that is itself a PHP script.

The code that I have been playing around with is like this:
------------------------------------------------------------------------
import java.net.*;
import java.io.*;

class Link {
      public static void main(String[] args) {
            try
               {
                       URL url = new URL("http://192.168.5.100/index.php");
                        URLConnection connection = url.openConnection();
               }
               catch (Exception e)
               {
                        System.out.println(e.toString());
               }
      }
}
-----------------------------------------------------------------------

Any help would be appreciated.

Also I am using an older version of Java if that matters, I believe it is version 1.2.2.  Please explain your solutions in detail as I have very little Java knowledge.

--
Thanks in Advance
Blaine
0
Comment
Question by:blainehilton
  • 5
  • 3
9 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
Comment Utility
to read the page use:

class Link {
     public static void main(String[] args) {
          try
             {
                    URL url = new URL("http://192.168.5.100/index.php");
                     URLConnection connection = url.openConnection();
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String line = null;
                    while (null!=(line=in.readLine()))
                    {
                       System.out.println(line);
                    }
             }
             catch (Exception e)
             {
                     System.out.println(e.toString());
             }
     }
}
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
if you want to actually render the html page you'll need to use JEditorPane.
Let me know if you need more details.
0
 
LVL 1

Author Comment

by:blainehilton
Comment Utility
Thank you for your fast response, no I do not need to view the page, just have it run the PHP.

I ran the code you sent and it complied fine but when I ran it I received an error saying "java.net.MalformedURLException".  This is the same error I was having with my example.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
strange it looks alright, is the URL you are using the same as what is posted here?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:blainehilton
Comment Utility
The URL is pretty much the same, but is different.  It is an IP address and not a hostname and I am using the http:// and the page does exist, and the device can ping the server.

Does it matter that this is running on a TINI Module?  (http://www.ibutton.com/TINI/index.html)

When I run it with the command "java Link.class" from the DOS prompt I receive a different error.  The dos prompt gives me:

Exception in thread "main" java.lang.NoClassDefFoundError: Link/class

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> When I run it with the command "java Link.class"

should be:

java Link
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> The URL is pretty much the same, but is different.

what is it exactly then?
0
 
LVL 1

Author Comment

by:blainehilton
Comment Utility
Using the command "java Link" it worked great, however it is not working on the TINI.
0
 
LVL 2

Accepted Solution

by:
SergeiKo earned 250 total points
Comment Utility
Hello, blainehilton.

I have some practice in working with TINI. So I have some advices for you.

1. Tune your build command, you have to add HTTP classes, they are not included in the Core API
    (see <TINISDK>/docs/BuildDependency.txt for details).
    Here is the build file for my test class, which source I provide at the end of this question.

    Compiling classes:
javac.exe -classpath C:\Projects\TINISDK\tini1.13\bin\tiniclasses.jar -d bin -target 1.1 *.java

    Building .tini module.
java -classpath C:\Projects\TINISDK\tini1.13\bin\tini.jar BuildDependency -x C:\Projects\TINISDK\tini1.13\bin\owapi_dep.txt -p C:\Projects\TINISDK\tini1.13\bin\owapi_dependencies_TINI.jar;C:\Projects\TINISDK\tini1.13\bin\modules.jar;bin -f bin -o MiniBrowser.tini -d C:\Projects\TINISDK\tini1.13\bin\tini.db -add HTTP

    -f parameter is about the path to your compiled classes.
    About  -add  parameter see <TINISDK>/docs/BuildDependency.txt.

    After that copy *.tini file to the TINI and run, e.g.
java MiniBrowser.tini


//Example from The TINI Specification and Developer's Guide  Don Loomis
import java.net.*;
import java.io.*;
import com.dalsemi.tininet.TININet;

class MiniBrowser {
      public static void main( String[] args ) {
            try {
                  URL u = new URL(args[0]);
                  InputStream in = u.openConnection().getInputStream();

                  byte[] content = new byte[512];
                  int count = 0;
                  do {
                        count = in.read(content);
                        System.out.write(content, 0, count);
                  } while (count != -1);
            } catch ( Exception e ) {
                  System.out.println( e.getMessage() );
                  e.printStackTrace();
            }
      }
}


2. Visit http://discuss.dalsemi.com/ for more info on TINI.


Regards.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now