最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to identify if domain is http or https? - Stack Overflow

programmeradmin2浏览0评论

Given a url, let say : "example"

How do I know if it is or in Java or at client side in javascript.

I want to do it for arbitrary domain, on my domain. I am creating a website scanning tool , and I want to find the protocol of website entered by user(in case user enters example without https)

Given a url, let say : "example."

How do I know if it is http://www.example. or https://www.example. in Java or at client side in javascript.

I want to do it for arbitrary domain, on my domain. I am creating a website scanning tool , and I want to find the protocol of website entered by user(in case user enters example. without https)

Share Improve this question edited Aug 31, 2018 at 7:22 steven asked Aug 31, 2018 at 7:12 stevensteven 1991 gold badge3 silver badges9 bronze badges 2
  • 4 Scheme (HTTP, HTTPS, or something else) is entirely distinct from the domain name. – chrylis -cautiouslyoptimistic- Commented Aug 31, 2018 at 7:13
  • 1 You should better configure your webserver in a way that all http requests will be forwarded to https connections! – PowerStat Commented Aug 31, 2018 at 12:30
Add a ment  | 

4 Answers 4

Reset to default 5

There are a number of misconceptions here.

  1. The string "example." is not a URL. It doesn't have a protocol.

  2. A domain is just that. A domain. It is not a URL. It is not a website. It is just a sequence of characters that the DNS can resolve to ... something.

  3. You can't assume that there is any website associated with a domain.

  4. The "www." convention is redundant, old fashioned and ... (IMO) ... ugly. Most places who use "www.example." will also have a server at "example.", and one will redirect to the other as appropriate.

  5. You can't always assume that the server is on the standard port (80 or 443). It is even possible that an https server uses port 80 or an http server uses 443. (Crazy ... but possible)

So how do you figure out if a given domain name has an associated website, what the website's URL is, and whether it is "http:" or "https:"?

Trial and error. There is no other way.

  1. Try send an HTTP request on the standard port and possibly mon alternative ports; e.g. port 8080. If you get a valid response, you have identified a HTTP server.

  2. Try the same with an HTTPS request.

  3. If the domain name doesn't start with "www." try prefixing it with that.

But resign yourself to the possibility that none of the above work.

You can check the location.protocol

if (location.protocol === 'https:') {
  // is https
} else {
  // is http
}

in javascript you can do something like this:

console.log(window.location.protocol) 

and that will log http or https

You could verify document.location.protocol to see if it's "http:" or "https:" or consult this page Parsing a URL on java

import java.*;
import java.io.*;
public class ParseURL {
public static void main(String[] args) throws Exception {

    URL aURL = new URL("http://example.:80/docs/books/tutorial"
                       + "/index.html?name=networking#DOWNLOADING");

    System.out.println("protocol = " + aURL.getProtocol());
}
}
发布评论

评论列表(0)

  1. 暂无评论