Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding "Http://" before my address

Tags:

java

i'm extracting address from a source but some extract doesnt have http:// infront of the address, how do i check if the address has http:// and if they dont how do i add the http:// infront? :O

Getting this error which i guess is due to the "lack" of http:// infront

java.net.MalformedURLException: no protocol: www.speedtest.net
at java.net.URL.<init>(URL.java:583)
at java.net.URL.<init>(URL.java:480)
at java.net.URL.<init>(URL.java:429)
at a.PageRead.r(PageRead.java:29)
at a.ThreadDownloaderWriter.run(ThreadDownloaderWriter.java:35)
at java.lang.Thread.run(Thread.java:722)






 public StringBuilder readPage() {
        try {

            URL url = new URL(this.strURL);
            System.out.println(this.strURL);
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            reader.close();

            return sb;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return new StringBuilder("");
        } catch (IOException e) {
            e.printStackTrace();
            return new StringBuilder("");
        }
    }
like image 276
CodeGuru Avatar asked Nov 27 '22 09:11

CodeGuru


1 Answers

The literal answer to your question looks something like this:

String url = ... ; // Whatever
if (!url.startsWith("http://")) {
    url = "http://" + url;
}

But that's still not a very good solution. For example, what about https URLS? How about ftp or even filesystem URLs (file://). And then you may want to think about things like case-sensitivity ("http://" != "HTTP://" != "HttP://" even though in reality they all mean the same thing and will be accepted by Java's URL class).

You could try being a little more careful:

if (!url.toLowerCase().matches("^\\w+://.*")) {
    url = "http://" + url;
}

This matches the beginning of the URL string against any "word character" followed by a colon (:) and two slashes (//), and then defaults it to http:// if the protocol part of the URL is missing. This will cover significantly more cases than the original (literal) answer.

Ultimately, if someone gives you a URL without the protocol part, it's an invalid URL.

You should consider picking up a book on Java programming, as these are all basic logic/Java API issues.

like image 175
Adam Batkin Avatar answered Dec 15 '22 13:12

Adam Batkin