zafar142003
zafar142003

Reputation: 2159

HttpUrlConnection's response omits the word 'http'

I create the URL object using a string like "http://www.example.com/a?s=12". I read the HTML response in the string serverResponse. This string is expected to have the entire HTML of a page, which has JavaScript and CSS includes. But strangely, the word "http:" is missing from all the URLs present in the response, eg in place of "http://example.com" I get "//asd.com". Any ideas?

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer serverResponse = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        serverResponse.append(inputLine);
        System.out.println(inputLine);
    }
    in.close();

    System.out.println(serverResponse);

Upvotes: 0

Views: 46

Answers (2)

user207421
user207421

Reputation: 311028

This string is expected to have the entire HTML of a page, which has javascript and CSS includes.

Why? A properly-constructed site will use relative URLs as much as possible. This seems to be one of them. Well done them, or you if it's your work.

But strangely, the word "http:" is missing from all the URLs present in the response, eg in place of "http://example.com" I get "//asd.com". Any ideas?

It's called a protocol-relative URL.

Upvotes: 1

e.dan
e.dan

Reputation: 7507

See here: Protocol-relative URLs

Upvotes: 2

Related Questions