You are here:

C++/URL check



How are you?
I'm trying to write a program that, among other things, needs to validate a website. Basically I need to know if a user-entered website exists or not. I don't know where to start. I was wondering if you could give me a lead with this. How to do this using C/C++ Thank you so much!


First you need to decide what you mean by "web site exists".

Would you need to validate the form of the URL to start with (probably)?
If so then what URL schemes would you accept? (http, https, ftp etc?)

Does the domain having a DNS entry suffice?
What if the user entered raw IP address values?
Would pinging that address suffice?

Or would actually trying to obtain the page data from the site be what is required?

And in all scenarios what happens if a facility is not available - e.g. your Internet access is down, or the site or DNS server is temporarily down or there is some problem in between such that that portion of the Internet is unavailable? In thee cases the user may well have entered a valid URL - its just currently unreachable.

You will have to decide these sorts of things yourself.

So let us look at how you might achieve some of these things using C++.

First off, C++ has no built in support for networking at all.

Nor does it currently have any support for pattern matching as you might use to validate the form of an entered URL. However in this case there is some hope. The C++ TR1 library update to the current C++ standard (see includes regular expression support which of course can be used to create regular expressions that can be used to validate a URL string. Unfortunately not many compilers currently ship with TR1 library support - I currently only know of the SP1 update to Microsoft Visual C++ 2008 (and presumably later editions of MSVC++). However support is available in the Boost libraries, see:,

For networking support we have to look elsewhere.

You could use raw operating system socket and networking APIs such as gethostbyname (see and for example) to lookup a host name and obtain its address. Note that this would imply being able to extract the host name part of a URL from the whole URL string (regular expressions again).

You could use third party networking libraries such as ACE ( or the Boost asio library (

However these are all somewhat low level if you wish to perform say a HTTP request to the URL and see if you get any (valid) data back. In these cases maybe something like the C cURL library - libcurl - (see, as wrapped and used by PHP.

One library, the PoCo C++ libraries (see, seem particularly interesting for this sort of problem. They support many useful facilities including regular expressions, URI/UTLs and HTTP. Looking at some of the Poco library samples might be instructive - see the description document - the URI Sample (section 2.1.15) and HTTPGet Sample (section 2.4.4) in particular look relevant here.

Note that for the Boost and PoCo libraries you will require an up to date C++ compiler and standard library implementation - e.g.Microsoft Visual C++ 7.1 (with service packs) or later, GNU g++ 3.4.3 or later (depends on operating system which versions of g++ work)

So your course of action would be:

   - decide exactly what is it you need to do
   - look around for useful libraries/facilities to perform as much of the heavy lifting as possible.
   - install these libraries for your system / compiler etc.
   - use the facilities of these libraries to get the job done

If you do not like or want or are unable to use the libraries mentioned here then you can use Internet search sites (Google, Yahoo, Bing etc.) to locate such libraries and API information. One site that is trying to create a comprehensive database of such libraries is at - so you may like to poke around there to start with.

Hope this gives you some pointers at least.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.