• 0

[C]gethostbyename in linux


Question

I have a project for university and I need to write a c program that checks some website from time to time. So I need to resolve the address and then connect at the ip that I get when resolving the website address with gethostbyname. The only problem that I have is that I need to check for redirect. But each time I request a header I always get a 302 found answer and a Location field in the header even after I follow the redirect. I never get a 200 answer or one of the other 2xx answers. Any ideas why I get this answer?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Your question seems completely unrelated to the function in the title, but okay.

Are you actually sending the Host: header in your HTTP/1.1 request? This must be set to the hostname of the website you are asking for. It's needed with most sites today, because the server often actually hosts multiple sites on the same IP address.

So, are you doing this?

Link to comment
Share on other sites

  • 0

OK,what you said gave me a hint and I solved the problem,apparently I was assigning the old website address and not the redirected address to the request. Now my last question is that sometimes I get redirected to htttp://www.website.com/sub/ and my question is if I can resolve those addresses too in the same way ?. I noticed that I need to remove the http:// from the redirects or else it cannot resolve the address. Or some general advices about how to resolve different redirects.

Link to comment
Share on other sites

  • 0

The function only resolves hostnames. It doesn't understand the concept of URLs. In http://www.website.com/sub/,'>http://www.website.com/sub/, only www.website.com is the actual hostname. This holds true for all URLs. The http:// specifies the protocol of the URL, while the text that comes after the / is protocol-specific information (in this case, the path to the file that is used in the HTTP request). There is nothing special about a "redirect." A hostname is a hostname.

To get to http://www.website.com/sub/ you would resolve www.website.com, connect to its IP, send a "GET /sub/ HTTP/1.1" and "Host: www.website.com". You'll then get the document, or a redirect. If you get a redirect, you start over with the new URL.

Link to comment
Share on other sites

  • 0

The function only resolves hostnames. It doesn't understand the concept of URLs. In http://www.website.com/sub/,'>http://www.website.com/sub/, only www.website.com is the actual hostname. This holds true for all URLs. The http:// specifies the protocol of the URL, while the text that comes after the / is protocol-specific information (in this case, the path to the file that is used in the HTTP request). There is nothing special about a "redirect." A hostname is a hostname.

To get to http://www.website.com/sub/ you would resolve www.website.com, connect to its IP, send a "GET /sub/ HTTP/1.1" and "Host: www.website.com". You'll then get the document, or a redirect. If you get a redirect, you start over with the new URL.

Thank you,managed to make it work. Sorry for the noobish questions :).

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.