I have sought in vain for a book entitled "Website Communication for Dummies". Can anyone suggest some good reading material / tutorial for me to consult?
Here is where I am at: I have a 32-bit Windows app I have written in C++ using Visual Studio 2010 C++ Express. The app facilitates User selection of an URL in text format == ie, www.maps.google.com -- and then creates a socket and connects it, etc. The problem is that I can use the "send" command w/o error, but I have no idea what content to send in the 2nd argument, which is a const char[].
I've tried simple commands like "dump" and "refresh" for various websites, but the recv() function merely returns 0 (bytes received) after a long delay.
Thanks for attending to this.
To understand what sort of data goes back and forth between web server and a client, look at the RFC (or start with a tutoral ).
When you have the understanding of the protocol and played with raw sockets, look for C or C++ implementations. libcurl would be one such. I also think Windows has build-in support for HTTP clients in Windows SDK.
You'll probably want to send something like
GET / HTTP/1.1
to get a proper http response. However most sites will disregard requests that don't include certain HTTP headers (eg Host). I would advise looking up http client libraries in C++ to do some of the grunt work for you, writing your own http request building code is very much reinventing the wheel.
First use send
( GET / HTTP/1.1..
) to the webserver to make a request, and after that use recv(Socket,buffer..)
to download the website HTML code into a buffer.
send(Socket,"GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n"),0);
Winsock code:
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main (){
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
cout << "WSAStartup failed.\n";
system("pause");
return 1;
}
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.google.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(80);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
cout << "Connecting...\n";
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
return 1;
}
cout << "Connected.\n";
send(Socket,"GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n"),0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
cout << buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
system("pause");
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.