Hey guys, I'm trying to make a program that checks a specific internet address and gives me back the HTTP Status Code, I have to do it in C because it's a school project.
I'm using the Cygwin compiler on a Windows 7 (64-bit version)
I've searched Google for some good examples, but all I come across is C# and C++ codes. -
HttpWebRequest HttpWReq =
-
(HttpWebRequest)WebRequest.Create("http://www.google.com/services/");
-
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
-
Response.Write(HttpWResp.StatusCode);
-
This is the code I've found that makes the much sense to me but as far as I know, this has nothing to do with C.
Any suggestions / code examples I can cast my eyes on?
As for suggestions, what the full purpose of this is, is to check if a specific webpage is a valid internet link.
Two examples:
www.google.com/services/ is a valid link (This page exist)
www.google.com/something/ is not a valid link (This page do not exist)
I've also tried it with this code: -
#include <stdio.h>
-
#include <string.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netdb.h>
-
#include <arpa/inet.h>
-
-
void tjeklinkonline(char *linkIND) {
-
-
struct addrinfo hints, *res, *p;
-
int status;
-
char ipstr[INET6_ADDRSTRLEN];
-
-
// Determines IP-type and Socket
-
-
memset(&hints, 0, sizeof hints);
-
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
-
hints.ai_socktype = SOCK_STREAM;
-
-
//Pings net address, and write response
-
-
if ((status = getaddrinfo(linkIND , NULL, &hints, &res)) != 0) {
-
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
-
printf("Not a Valid Link \n");
-
}
-
else {
-
printf("Valid Link\n");
-
}
-
-
freeaddrinfo(res); // free the linked list
-
} // End of function
-
-
-
-
-
-
int main(void)
-
{
-
-
char link[40] = "www.google.com";
-
char link2[40] = "www.google.com/services/";
-
-
tjeklinkonline(link2);
-
-
-
return 0;
-
}
-
But this code, is only capable of contacting domain addresses which just isn't enough.
Thanks in advance
Okay... So now I've fixed my problem, thanks for the guidance though.
So to do this short, if you want to understand how the network contacts work, try to understand how Sockets work.
This place explains it pretty well: http://www.linuxhowtos.org/C_C++/socket.htm
And then go and get cURL, if you use Cygwin, you can just include the cURL package when you install it.
And in order to learn cURL, well... Go to their page :) http://curl.haxx.se/
And then the function you will need after understanding how cURL works would be, curl_easy_getinfo()
More on that here: http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
Hope this is enough for anyone having the same problem to get it done :)
If you need an actual example, I would be happy to help.
6 11391
To read HTTP Status code you need to connect with the HTTP server,
Send HEADER Request(not POST, not GET) and then parse from it.
Do you know how to connect with HTTP server.
??
Not really no, I'm new to programing, and I've only mastered the general functions in C
But now I've found a page: http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx
That seams to have all the functions I'll need, but I'm just a tad confused as of how the code should look when I establish the connection, could you give me an example?
First Learn SOCKET
Then study a little about HTTP. You will get thousands of application/examples which can teach you a bit about how to develop SOCKET based application. ......
Hey again Johny, I've been studying the SOCKETS and I'd say I understood it enough to start making my first attempt on making my own program using SOCKETS, but I'm trying to compile this code that I found at http://www.linuxhowtos.org/C_C++/socket.htm but it won't compile. And I'm thinking I might have a problem with my Cygwin setup?
I get the Error code:
onlinelink.c: In function 'main':
onlinelink.c:47: warning: passing arg 2 of 'connect' from incompatible pointer type
I've tried to cast it, but that doesn't help either. -
#include <stdio.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <netdb.h>
-
-
void error(char *msg)
-
{
-
perror(msg);
-
exit(0);
-
}
-
-
int main(int argc, char *argv[])
-
{
-
-
// Variables
-
int sockfd, portno, n;
-
struct sockaddr_in serv_addr;
-
struct hostent *server;
-
char buffer[256];
-
-
-
if (argc < 3) {
-
fprintf(stderr,"usage %s hostname port\n", argv[0]);
-
exit(0);
-
}
-
-
-
portno = atoi(argv[2]);
-
sockfd = socket(AF_INET, SOCK_STREAM, 0);
-
-
if (sockfd < 0)
-
error("ERROR opening socket");
-
server = gethostbyname(argv[1]);
-
if (server == NULL) {
-
fprintf(stderr,"ERROR, no such host\n");
-
exit(0);
-
}
-
-
bzero((char *) &serv_addr, sizeof(serv_addr));
-
serv_addr.sin_family = AF_INET;
-
bcopy((char *)server->h_addr,
-
(char *)&serv_addr.sin_addr.s_addr,
-
server->h_length);
-
serv_addr.sin_port = htons(portno);
-
-
if (connect(sockfd , &serv_addr , sizeof(serv_addr)) < 0)
-
error("ERROR connecting");
-
-
-
// This last part is just to keep it as I found it, not really a part of the function I'm looking for.
-
-
-
printf("Please enter the message: ");
-
bzero(buffer,256);
-
fgets(buffer,255,stdin);
-
n = write(sockfd,buffer,strlen(buffer));
-
if (n < 0)
-
error("ERROR writing to socket");
-
bzero(buffer,256);
-
n = read(sockfd,buffer,255);
-
if (n < 0)
-
error("ERROR reading from socket");
-
printf("%s\n",buffer);
-
return 0;
-
}
-
If you'd take a look at it, it would really help me a lot.
Never mind my last message, learned that it works anyway.
Just didn't know that you could run it even if you get a warning :P
But learned so at the university today ^^
So... Next is the HTTP! Hello world, here I come! :D
---------------
But where do I get a HTTP guide for C? To me, it doesn't look like HTTP functions is a standard thing in C.
But, I've read about cURL, should I download this function set and try using that?
Okay... So now I've fixed my problem, thanks for the guidance though.
So to do this short, if you want to understand how the network contacts work, try to understand how Sockets work.
This place explains it pretty well: http://www.linuxhowtos.org/C_C++/socket.htm
And then go and get cURL, if you use Cygwin, you can just include the cURL package when you install it.
And in order to learn cURL, well... Go to their page :) http://curl.haxx.se/
And then the function you will need after understanding how cURL works would be, curl_easy_getinfo()
More on that here: http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
Hope this is enough for anyone having the same problem to get it done :)
If you need an actual example, I would be happy to help.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Geoff Soper |
last post by:
I would like to get the HTTP status code of a URL using PHP. I think
CURL is the way to go but I can't see how to do it. I see I can use
curl_getinfo on the CURL session after I've curl_exec'ed it...
|
by: Don Miller |
last post by:
I used a Response.Redirect "page.asp" as the last command in ASP code that
processes a submitted form from an Adobe PDF form (using HTML format). For
some reason, if there is any linked file...
|
by: probashi |
last post by:
Hi,
I have an ASP page that inserts a record into database. Some times I
find that multiple records are being inserted (with same data) in to
database. I checked IIS log and found the following:...
|
by: Jon Maz |
last post by:
Hi All,
Here's the code:
HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();...
|
by: BuddyWork |
last post by:
Hello,
I'm doing a performance test and my test graudually
increases the connections to the webservice. Everything
works well until my connection hits around 350
connections. Then future...
|
by: Nick Horrocks |
last post by:
I have set up a custom error page for 404 errors.
However the HTTP status code returned is 302 followed by 200, this causes
search engines not to remove old pages from their index.
How can I...
|
by: George |
last post by:
Hi,
I have a production IIS server with ASPX application.
In logs i sometimes see 206 HTTP code which means "Partial Content". It happens only on images though.
Is it something i must be...
|
by: Kiky |
last post by:
Hi guys,
Does anyone know how to get a http status code? (ok=200, 404=not found,
500=internal server error)?
I can catch exceptions from WebException when I check for a url, but I still
can't...
|
by: buntyindia |
last post by:
Hi,
When we get request on server after processing that it writes back the response to client that also contains HTTP status code like 200 for Ok, 404 for page not found.
How can I have access...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |