JohnDotOwl
JohnDotOwl

Reputation: 3755

Adding "Http://" before my address

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("");
        }
    }

Upvotes: 4

Views: 6664

Answers (6)

Adam Batkin
Adam Batkin

Reputation: 53024

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.

Upvotes: 19

SharkAlley
SharkAlley

Reputation: 11659

The current highest voted answer contains an incorrect regular expression. The solution below will match any protocol, upper or lower case:

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

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

String s = "www.google.com";

Pattern p = Pattern.compile("http://");

Matcher m = p.matcher(s);

if (m.lookingAt()){


  System.out.println("Its already there");

}
else{

       String ss = "http://"+s;

       System.out.println(ss);

 }

Upvotes: 0

kgautron
kgautron

Reputation: 8283

You can check if the adress starts with "http://" using the startsWith() method :

url.startsWith("http://");

which returns true if it is the case.

You are probably right about the missing "http://" being the problem. The documentation seems to indicate that the protocol (http here) is mandatory.

Upvotes: 6

Jon Lin
Jon Lin

Reputation: 143926

Well, based on your stack trace I'd say before you call new URL(your_url), check if your_url starts with either an http://, https://, ftp://, or any other protocols you think you need to check for:

if(!your_url.startsWith("http://")) 

If it's missing, add a default http:// in front of your_url.

your_url = "http://" + your_url;

Do you think this error is caused by the http:// ? –

Yes, in your stack trace:

java.net.MalformedURLException: no protocol: www.speedtest.net

You have a url that is www.speedtest.net and it needs to be http://www.speedtest.net. The error is caused by a missing protocol. Alternatively, it doesn't have to be http://, just a valid protocol like https://, file://, etc.

Upvotes: 2

xthexder
xthexder

Reputation: 1565

String url = "www.speedtest.net"
if (!url.toLowerCase().startsWith("http://")) url = "http://" + url

However this does not work in all cases, because some websites may have https:// or other protocols.

Upvotes: 2

Related Questions