top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C API to validate web address.

+2 votes
758 views

Is there any C API for validating a web address.

Here whats my requirement I don't need to validate the given address is active or not.
But the API should tell me is the given web address is in proper format.

for example:
www.helloworld.com --------> valid
wwwww.hello.com ------- > invalid
www.hello.world ---------> invalid

posted Dec 7, 2015 by Arshad Khan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
wwwww.hello.com can be a valid address, wwwww can be a subdomain.
Thanks I didn't know that.

1 Answer

+1 vote

As I suggested in the comment that wwwww.hello.com and www.hello.world are valid domain names. world can be valid suffix and wwwww can be a valid subdomain. So the best way is to check if server is returning the 200 OK or not (but this will tell if site is live or not.)

See the sample implementation the same can be used using system inside C. Its a tested program and used in QH extensively.

URL=www.queryhome.com
serverResponse=`curl -Is ${URL} | head -n 1`

echo $serverResponse
if [[ $serverResponse == *"200 OK"* ]]
then
    echo "Server is UP"
    exit 1
else
    echo "Server is DOWN"
    exit 0
fi
answer Dec 7, 2015 by Salil Agrawal
Hi Salil, Thanks for the sample.

whenever I am executing this script I am getting this

HTTP/1.1 200 OK
ts.sh: 7: ts.sh: [[: not found
Server is DOWN

One more thing, in my requirement we need a C api, and don't want to implement one as Somebody also told that there is standard C api for doing the above.
Just test the following
URL=www.queryhome.com
serverResponse=`curl -Is ${URL} | head -n 1`
echo $serverResponse

Rest is the printing code which is simple (may be my shell and your is different)

Now coming to the problem which u stated, for C/C++ you can install some curl library or curl wrapper. The process is same
1. create the HTTP request for the URL
2. get the response
3. on the response get the status if status is 200 then site is LIVE.

unfortunately I dont have the code with me :(
Thanks for the heads up (y)

is the status is always 200 ?.
One thing what if the address is valid but its inactive.

thank you very much.
In that case u wont receive 200 OK.
Similar Questions
+1 vote

This was asked today at Amazon interview -

Given two Binary Trees (not BST). Each node of both trees has an integer value. Validate whether both trees have the same integers, there could be repetitive integers.

Example
Tree1:
5
1 6
5 4 3 6

Tree2:
1
3 4
6 5

Output
Identical integers

Sample C/C++/Java code would be helpful.

+5 votes

Can we increase the size during run time? What if, we exceed the size of array elements?

+4 votes

What is the point of declaring Pointer of different types (eg. integer,float,char) as we all know pointer takes 4 bytes of space regardless which type of pointer it is and only contains address?

0 votes
old_fun()
{
  int a = 10;
  int *ptr = &a;
  new_fun(&a);  // passing address  case 1
  new_fun(ptr);  // passing address case 2 
}

new_fun(int *ptr)
{
  *ptr = 50; 
}

Here, case 1 will affect of "a" of old_fun but case 2 wont affect why ?

...