mohit jain
mohit jain

Reputation: 407

How to verify that a webpage exist

I am sending HTTP GET request and receiving data here:

ssize_t numBytes = recvfrom(sock, request, 1000, 0,
                   (struct sockaddr *) &myaddr, &fromAddrLen);
    if(numBytes < 0)
        printf("The requested resource does not exist.\n");
    else
        printf("the webpage exist :)\n");

I want to check whether the requested page exist or not. But even if a page does not exist, status message is often sent from server (something like "404 not found"). It is still some data, thus I cannot get numBytes<0.

How can I check the status of response to be able to verify the existence of the page?

Upvotes: 1

Views: 186

Answers (3)

user1202136
user1202136

Reputation: 11567

Checking for numBytes < 0 only allows you to determine whether there is a fatal TCP error, such as connection failed. To find out whether the page exists, you need to implement a part of the HTTP protocol. I recommend you to use a library, such as libcurl or libwww.

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318568

Well you have to read the first line of the response and extract the status code. If it's a 4xx or 5xx code something went wrong (404 = Not Found, 403 = Access Denied).

Upvotes: 3

Related Questions