Solved

Requesting a URL

Posted on 2004-09-15
9
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 12071178
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
ID: 12071183
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
ID: 12071250
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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 92

Expert Comment

by:objects
ID: 12071278
strange it looks alright, is the URL you are using the same as what is posted here?
0
 
LVL 1

Author Comment

by:blainehilton
ID: 12071305
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
ID: 12071393
> When I run it with the command "java Link.class"

should be:

java Link
0
 
LVL 92

Expert Comment

by:objects
ID: 12071394
> The URL is pretty much the same, but is different.

what is it exactly then?
0
 
LVL 1

Author Comment

by:blainehilton
ID: 12071467
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
ID: 12074346
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

729 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