0

How do I determine (using c++ and winsock) the site protocol based on the URL, for example (www.google.com) if the protocol is not known in advance?

Or how do I determine web server TCP port?

I want do an HTTP get request using the link which after www. and need to determine the port or protocol, in order to use http over tls or simple http.

Gardener
  • 2,591
  • 1
  • 13
  • 22
  • 3
    `www.google.com` is a domain name. protocol is supposed be leading part of url before `:`, for example in url `http://www.google.com` protocol will be `http` – user7860670 Oct 29 '18 at 12:53
  • I need to programmatically recognize the protocol by domain – Веталь Веталь Oct 29 '18 at 13:09
  • 1
    It is not possible to figure out protocol just by domain name alone because domain names are not bound to protocols. That is there could be dozens of protocols working with `www.google.com` at the same time. – user7860670 Oct 29 '18 at 15:27

2 Answers2

0

In your example, www.google.com is a domain name.
To get protocol you need full urls like https://www.google.com or http://www.google.com
In the above example, http and https are protocol types.

You can also use nmap to determine the open ports, service name and protocol used

Mayur
  • 2,583
  • 16
  • 28
0

You can't. You decide the protocol you're going to use to contact some server. If you haven't decided it, you don't know it. Certainly your computer can't tell you what it will be.

It's like asking a supermarket cashier what you're going to buy today. They don't know that. You are supposed to tell them that.

What you can do is to see whether a website on that server automatically redirects HTTP traffic to a HTTPS URI (thus enforcing SSL), or otherwise blocks non-HTTPS traffic. If that's what you want to do, you can achieve it by attempting to make an HTTP connection to that domain and see what happens.

Depending on your web browser make/model/version, that may be what it is doing when you enter "www.google.com" without specifying a protocol: assuming http:// then following any remote redirects that take you to https:// instead. Pretty soon, though, or already if you have certain extensions installed, the default is going to be https://. I must stress though, again, that this is still the client (i.e. the browser) making the decision, not the server; if you are writing your own browser then, again, you must choose what that default should be.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thank you for the answer, if I try connect to web server using port 80, but it works on 443, what server response to me? – Веталь Веталь Oct 29 '18 at 14:11
  • @ВетальВеталь If you try to connect to the web server using port 80, port 443 is completely irrelevant. All that matters is whether the server is listening on port 80 (if not, your connection will fail) and, if so, what it responds with if it does. I recommend you study the HTTP specification to see what possible valid responses exist; the ones that are intended to trigger redirection may be of particular interest to you, but really you ought to study all of it if you're implementing a web client (of course, you didn't tell us what you're doing, so we can only guess). – Lightness Races in Orbit Oct 29 '18 at 14:13
  • I write parser, html pages. Https server uses 443 port, if web server listening 443 port I may guess that server use https protocol. I am right? – Веталь Веталь Oct 29 '18 at 14:27
  • @ВетальВеталь Generally yes though you'll find out when you connect and must still handle the case that an HTTPS service is not what's found there! – Lightness Races in Orbit Oct 29 '18 at 15:16
  • @ВетальВеталь Not really, server listening on 443 port may support multiple protocols at the same time or none at all. – user7860670 Oct 29 '18 at 15:29
  • @VTT How then will you offer? – Веталь Веталь Oct 29 '18 at 16:14