Results 1 to 10 of 13
Guys,
I'm supposed to display the source code of a webpage, and i came up with this program...i'm supposed to embed the GET request in my program..
but i get ...
- 08-03-2009 #1Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
help with HTTP GET REQUEST
Guys,
I'm supposed to display the source code of a webpage, and i came up with this program...i'm supposed to embed the GET request in my program..
but i get the error "Network is unreachable" and sometimes "connection timed out"
Is there something wrong in my program syntax or is it the network that is unreachable.....
guys pls help me out with this.....
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(int argc,char argv[])
{
int startup,connectres,bytes,i=0,sock,strlen;
struct sockaddr_in connto;
struct hostent *server;
char source=" ";
char buffer[1024];
char *outmsg="GET "webpage" HTTP/1.1"; // GET REQUEST
sock=socket(AF_INET,SOCK_STREAM,0);
connto.sin_family=AF_INET;
connto.sin_port=htons(8080);
connto.sin_addr.s_addr=inet_addr("The corresponding IP"); // IP ADDRESS
if(connectres=connect(sock,(struct sockaddr*)&connto,sizeof(struct sockaddr))==-1)
{
perror("unable to connect");
return -3;
}
printf("connection successful");
bytes=send(sock,outmsg,1000000,0);
printf("bytes sent are %d \n",bytes);
do
{
i=recv(sock,buffer,sizeof(buffer),0);
printf("%s",buffer);
printf("still reciveing data \n");
source+=buffer;
}while(i!=0);
printf("closing socket \n");
close(sock);
}
- 08-03-2009 #2
Have you changed "The corresponding IP" to the IP address of the server you are trying to contact?
Linux User #453176
- 08-03-2009 #3Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
Yes.
I did that......it still doesnt work
This is wat I get
connection successful
bytes sent are 5672
still recieving data
closing socket
so i'm getting all the printf statements printed except the HTML content i need...
- 08-03-2009 #4
That's because you're collecting the server's responce in a variable called source but you don't print it out
Linux User #453176
- 08-04-2009 #5Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
Thanks...but i'm a little confused about the do while part.....i'm pretty sure if i get right i'd be able to pull the required source code...
do
{
i=recv(sock,buffer,20000,0);
printf("still recieving data \n");
source=source+buffer;
printf("%s",source);
}while(i!=0);
i get an error invalid operand to binary +
so what am i missing over here???
- 08-04-2009 #6
You need to change:
Back toCode:i=recv(sock,buffer,20000,0);
as your new version allows the recv function to receive more data than buffer cnan storeCode:i=recv(sock,buffer,sizeof(buffer),0);
Also, I missed your printf("%s",buffer); line in your first post so forget my previous post about not printing the server's response.
So if you use your original post I think the problem may be the server not knowing what to do with your GET request. Try changing this line to something like:
Code:char *outmsg="GET / HTTP/1.1"; // GET REQUEST
Linux User #453176
- 08-04-2009 #7Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
thanks....i followed your advice but all i get now are a couple statements of garbage values.....
something like
connection successful
bytes sent are 24
¨¿¨ê·still recieving data
».¿¨¿¨ê·closing socket
does this mean i'm getting the response but unable to view it or is ther a problem on the server side???
- 08-04-2009 #8Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
thanks....i followed your advice but all i get now are a couple statements of garbage values.....
something like
connection successful
bytes sent are 24
¨¿¨ê·still recieving data
».¿¨¿¨ê·closing socket
does this mean i'm getting the response but unable to view it or is ther a problem on the server side???
- 08-04-2009 #9andCode:
char source=" "; char buffer[1024];
Code:source+=buffer;
Hello. As I see you are trying to add string (array of chars) to the single char. I mean that variable source is declared as char, so it means that only one symbol can be stored there, but you are trying to initialize that variable with string constant " " (space character plus null-terminator). You may try to declare variable source as array (the same as buffer variable). The second thing is that you cannot just concatenate two array of chars using + or += operands, they are not defined in C language (this is not string class from C++ for which these operators are overloaded). To concatenate two arrays of chars (aka strings) you need to use strncat() (this function is preferred over strcat()). Also, when sendind a request you should use
instead of random numbers greater than message size.Code:sizeof(outmsg)
And it is better to write:
insteadCode:char outmsg[] ="GET / HTTP/1.1";
Code:char *outmsg="GET "webpage" HTTP/1.1";
- 08-04-2009 #10
I have written a small example for you. This is not an ideal variant, but at least it works.
Have a nice day.Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #define BUFFER_SIZE 8192 #define HOST_ADDRESS "74.125.127.100" /* Google ;) */ #define HOST_PORT 80 int ReadData(int iSocketDescriptor, char *achDataBuffer, unsigned int uiBufferSize); int main(int iArgumentsCount, char *aszArguments[]) { /* Declare variables */ struct sockaddr_in saRemoteHost; int iSocketDescriptor = -1; char achDataBuffer[BUFFER_SIZE]; int iDataSizeRecieved = 0; const char szRequest[] = "GET / HTTP/1.1\r\n\r\n"; do { /* Create socket */ iSocketDescriptor = socket(AF_INET, SOCK_STREAM, 0); if (iSocketDescriptor == -1) { puts("Socket creation error"); break; } /* Fill data buffer and address/port structure with zeros */ memset(&saRemoteHost, 0, sizeof(saRemoteHost)); memset(achDataBuffer, 0, sizeof(achDataBuffer)); /* Initialize address and port information */ saRemoteHost.sin_family = AF_INET; saRemoteHost.sin_port = htons(HOST_PORT); saRemoteHost.sin_addr.s_addr = inet_addr(HOST_ADDRESS); if (saRemoteHost.sin_addr.s_addr == -1) { puts("Invalid IP address"); break; } /* Connect to remote host*/ if (connect(iSocketDescriptor, (struct sockaddr *)&saRemoteHost, sizeof(saRemoteHost)) == -1) { puts("Connection failed"); break; } /* Send request */ if (write(iSocketDescriptor, szRequest, sizeof(szRequest)) == -1) { puts("Request failed"); break; } /* Read data */ iDataSizeRecieved = ReadData(iSocketDescriptor, achDataBuffer, BUFFER_SIZE); if (iDataSizeRecieved == -1) { puts("Read error"); break; } /* Print result */ printf("Recieved: %d bytes\n", iDataSizeRecieved); printf("\n%s\n", achDataBuffer); } while (0); /* Close socket if opened */ if (iSocketDescriptor != -1) { close(iSocketDescriptor); } return EXIT_SUCCESS; } int ReadData(int iSocketDescriptor, char *achDataBuffer, unsigned int uiBufferSize) { int iReadResult = -1; int iDataSizeRecieved = 0; iReadResult = read(iSocketDescriptor, achDataBuffer, uiBufferSize); while (iReadResult != 0) { if (iReadResult == -1) { puts("Read error"); break; } iDataSizeRecieved += iReadResult; iReadResult = read(iSocketDescriptor, achDataBuffer + iDataSizeRecieved, uiBufferSize - iDataSizeRecieved); } if (iReadResult != 0) { iDataSizeRecieved = -1; } return iDataSizeRecieved; }


Reply With Quote