You are here:

C++/URL check

Advertisement


Question
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!

Answer
The standard C++ library does not provide any facility to do this. You have to use a third-party library for this.

You could use libcurl, a free, open source, widely portable client-side URL transfer library.
http://curl.haxx.se/libcurl/

The documentation on using libcurn from C/C++ is here: http://curl.haxx.se/libcurl/c/

Programming with the library is simple, for example this is how you would use libcurl in unix (FreeBSD):

#include <curl/curl.h>
#include <string>
#include <iostream>
#include <cstdio>

int main()
{
 curl_global_init( CURL_GLOBAL_ALL ) ;

 CURL* handle = curl_easy_init() ;

 curl_easy_setopt( handle, CURLOPT_HEADER, 1 ) ;
 curl_easy_setopt( handle, CURLOPT_NOBODY, 1 ) ;
 curl_easy_setopt( handle, CURLOPT_FOLLOWLOCATION, 1 ) ;
 curl_easy_setopt( handle, CURLOPT_MAXREDIRS, 10) ;

 // suppress all output by sending it to the null device
 FILE* null_device = std::fopen( "/dev/null", "w" ) ;
 curl_easy_setopt( handle, CURLOPT_WRITEDATA, null_device ) ;

 std::string url ;
 while( std::cout << "url? " && std::cin >> url )
 {
   curl_easy_setopt( handle, CURLOPT_URL, url.c_str() ) ;
   if( curl_easy_perform( handle ) != 0 )
       std::cerr << "the url '" << url << "' does not seem to exist or is inaccessible\n" ;
   else
       std::cerr << "a site with the url '" << url << "' was found\n" ;
 }

 curl_global_cleanup() ;
}


compile and link using gcc:
g++44 -Wall -std=c++98 -pedantic -Werror  -lcurl -I /usr/local/include -L /usr/local/lib program.cc


sample output:

url? google.com
a site with the url 'google.com' was found

url? www.google.com
a site with the url 'www.google.com' was found

url? http:/www.google.com
the url 'http:/www.google.com' does not seem to exist or is inaccessible

url? http://www.google.com
a site with the url 'http://www.google.com' was found

url? zebra.org
the url 'zebra.org' does not seem to exist or is inaccessible

url? <EOF>  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.