Solved

Exception: no protocol ...

Posted on 2004-10-10
9
400 Views
Last Modified: 2012-06-21
Hey everyone,
I've basically made a program that, basically; let's you/the user know whether the links on a webpage have changed/(new ones added/old ones removed).

Here's the code:

//:::::::::::::::::::::::::::::::::::::::  DomainChangeCheck.java :::::::::::::::::::::::::::::::::::::::::\\
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.text.html.*;
import javax.swing.text.*;
import java.text.*;

public class DomainChangeCheck {
      
      static String sDomain = "";
      static String sPage   = "";
      static int http       = 80;
      
      public static void main ( String [] args ) {
            
            DomainChangeCheck dcc = new DomainChangeCheck ();
            
            // Verify that an argument has been passed:
            if ( args.length == 0 ) {
                  System.out.println("Usage: java EeJavaInformer www.domain.com/page.htm");
                  System.exit( 1 );
            }
            
            String s1 = "",
                     s2 = "";
            
            sDomain = args[0];
            if ( args[1].length() > 0 ) sPage = args[1];
            
            // Verify domain existance:
            boolean exist = false;
          try {
              InetAddress addr = InetAddress.getByName( sDomain );
   
              // This constructor will block until the connection succeeds
              Socket socket = new Socket(addr, http);
              exist = true;
              socket.close();
          } catch (Exception e) {      }
          
          // Exit if domain does not exist.
          if ( /*domain does not*/ !exist ) {
                System.out.println( "Domain: " + sDomain + " cannot be found." );
                System.exit( 1 );
          }
            
            boolean bFirstTime = true;
            
      for (;;) {
            
            s2 = s1;
          
          // Retrieve all links in HTML document:
          String [] links = dcc.getLinks( "http://" + sDomain + sPage );
          
          if ( links.length == 0) {
                System.out.println( "Error: Could not find any links in specified document" );
                System.exit( 1 );
          }
          
          s1 = "";
          
          // Check to see if any changes to the links on the page have been made:
          for ( int i=0; i<links.length; i++ ) {
                s1 += links[i];
          }
          
          if ( !(dcc.doDiff( s1, s2 )) ) {
                if ( bFirstTime ) {
                      System.out.println( "\nStarted Successfully..." );
                      bFirstTime = false;
                } else {
                      System.out.println( "\nA change to the page has been made." );
                }
          }
    }
          
    }
      
      private boolean doDiff ( String s1, String s2 ) {
            // return true if the two strings are the same. Else, false.
            
            if ( s1.length() != s2.length() ) return false;
            
            char [] c1 = s1.toCharArray();
            char [] c2 = s2.toCharArray();
            
            boolean bSame = true;
            
            for ( int i=0; i<c1.length; i++ ) {
                  if ( c1[i]!=c2[i] && bSame ) bSame = false;
            }
            
            return bSame;
            
      }
      
    public static String[] getLinks(String uriStr) {
        List result = new ArrayList();
   
        try {
            // Create a reader on the HTML content
            URL url = new URI(uriStr).toURL();
            URLConnection conn = url.openConnection();
            Reader rd = new InputStreamReader(conn.getInputStream());
                
            // Parse the HTML
            EditorKit kit = new HTMLEditorKit();
            HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
            doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
            kit.read(rd, doc, 0);
                
            // Find all the A elements in the HTML document
            HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
            while (it.isValid()) {
                SimpleAttributeSet s = (SimpleAttributeSet)it.getAttributes();
                      
                String link = (String)s.getAttribute(HTML.Attribute.HREF);
                if (link != null) {
                    // Add the link to the result list
                    result.add(link);
                }
                it.next();
            }
        } catch (Exception e) {
              System.out.println( "Exception: " + e.getMessage() + "\n" );
              e.printStackTrace();
        }
          
        // Return all found links
        return (String[])result.toArray(new String[result.size()]);
    }
      
}
//:::::::::::::::::::::::::::::::::::::::  DomainChangeCheck.java :::::::::::::::::::::::::::::::::::::::::\\

Okay now; When I type something like:

java DomainChangeCheck www.positive-websolutions.co.uk /Index.htm

It works fine, but, let's assume that one puts:
java DomainChangeCheck www.experts-exchange.com /Java/

:o\ -- It doesn't work!! I get the following exception (from within the getLinks() method):
well, the  stackTrace:

java.net.MalformedURLException: no protocol: /Programming/Programming_Langauges/Java/
etc..

Any ideas why?

ThanQ :)
[r.D]
0
Comment
Question by:DrWarezz
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12272005
Are you sure you're not calling getLinks from elsewhere and that the only code you're running is the code you posted?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12272023
Also, instead of, or in addition to:

>>System.out.println( "Exception: " + e.getMessage() + "\n" );

could you do

System.err.println( "URI was " + uriStr );

?


0
 
LVL 9

Author Comment

by:DrWarezz
ID: 12272141
I'm sure that all the code I'm running is what's been posted.

I inserted:
System.err.println( "URI was " + uriStr );
into it, and it outputs:

URI was http://www.experts-exchange.com/Java/

:o\ ?

ThanQ,
[r.D]
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 12272148
According to the stackTrace, it would seem that it's line 106 that the error is occuring on:

Reader rd = new InputStreamReader(conn.getInputStream());

?
0
IT, Stop Being Called Into Every Meeting

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 86

Expert Comment

by:CEHJ
ID: 12272154
Yes but i'd like to see *all* the error output with that in it too ;-)
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 12272179
:)
Okay, here's EVERYTHING that's outputted:

Exception: no protocol: /Programming/Programming_Languages/Java/

URI was http://www.experts-exchange.com/Java/
java.net.MalformedURLException: no protocol: /Programming/Programming_Languages/Java/
   at java.net.URL.(init)(URL.java.579)
   at java.net.URL.(init)(URL.java.476)
   at java.net.URL.(init)(URL.java.425)
   at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:1090)
   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:681)
   at DomainChangeCheck.getLinks(DomanChangeCheck.java:106)
   at DomainChangeCheck.getLinks(DomanChangeCheck.java:55)
Error: Could not find any links in specified document

And here's the command I type:
java DomainChangeCheck www.experts-exchange.com /Java/

Thanks.
[r.D]
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12272229
At the risk of repeating myself (i said this yesterday and was accused of being 'unhelpful') the html packages are pretty flaky and/or intolerant when it comes to parsing html, so you'll have to accept uneven results. That

>>http://www.experts-exchange.com/Java/

does a redirect to

'/Programming/Programming_Languages/Java/'

So if you were to enter http://www.experts-exchange.com/Programming/Programming_Languages/Java/ at the outset, you might have more success
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 12272240
lol - You were right! :D -- It's the redirection.

Thanks alot CEHJ :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12272245
8-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

708 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

16 Experts available now in Live!

Get 1:1 Help Now