I have made the following win32 socket program to browse web pages. I am using wingw to avoid dependency on any runtime. To get ipaddresses I ping urls such as www.google.com, www.yahoo.com through command prompt and use those ip addreses in my program. Port is ofcourse 80.
I am able to get default pages of google, yahoo etc by using "GET /\r\n". I am also able to get non-default pages, even those inside directories, such as http://yasini.com/newsite/index.aspx by using "GET /newsite/index.aspx". The output of the program is in the form of html received from webserver, saved on hard disk. This file is later opened in firefox to see how did the communication go.
I have made a test webpage, http://a.domaindlx.com/trysite/hello.asp, which I can open in firefox. Then I ping the domain, a.domaindlx.com and get this ipaddress, 66.36.238.30. I try to access the said page by using "GET /trysite/hello.asp" but get this in response, "No web site is configured at this address. No web site is configured at this address."
I know that the said response is sent by the webserver, so I was able to connect to the webserver. The problem is that the webserver is not recognizing the url I am trying to access. I have used different webpages, both htm and asp and none is accessible.
When trying to open website using ipaddress directly in browser, I get the same error, "No website is configured...".
The basic puzzle is, why are these pages accessible through a browser such as firefox, but not through my code, when my code is essentially a browser, mean open connection with webserver at port 80.
#include windows.h #include stdio.h WSADATA ws; int d; char aa[1000]; struct sockaddr_in a; SOCKET s; int li; void abc(char *p) { FILE *fp = fopen("c:\\data.htm", "a+"); fprintf(fp, "%s\n", p); fclose(fp); } _stdcall WinMain (HINSTANCE i, HINSTANCE j, char * k, int l) { d = WSAStartup(0x101, &ws); sprintf(aa, "WSASTARTUP = %d", d); abc(aa); s = socket(AF_INET, SOCK_STREAM, 0); sprintf(aa, "SOCKET = %d", s); abc(aa); a.sin_family = AF_INET; a.sin_port = htons(80); //a.sin_addr.s_addr = inet_addr("74.125.236.145"); a.sin_addr.s_addr = inet_addr("66.36.238.30"); //a.domaindlx.com //a.sin_addr.s_addr = inet_addr("206.225.85.18"); //www.domaindlx.com //a.sin_addr.s_addr = inet_addr("87.248.122.122"); //www.yahoo.com //a.sin_addr.s_addr = inet_addr("72.167.153.9"); //www.yasini.com d = connect(s, (struct sockaddr *) &a, sizeof(a)); strcpy(aa, "GET /trysite/hello.asp\r\n"); strcat(aa, "HTTP 1.0 \r\n\r\n"); send(s, aa, sizeof(aa), 0); li = 1; while(li != 0) { li = recv(s, aa, 1000, 0); abc(aa); } }
Note: Please enclose the header file names in the include line in angle brackets for the code to work. I had to remove that to property format the html.