#include <stdio.h>#include<winsock.h>#include<string.h>#pragmaComment (lib, "Ws2_32.lib")voidGeturlChar*URL) {Wsadata Wsadata={0}; SOCKET SOCKFD; structsockaddr_in addr; structHostent *PURL; CharMyurl[bufsiz]; Char*phost =0, *pget =0; CharHost[bufsiz], Get[bufsiz]; CharHeader[bufsiz] =""; Static CharText[bufsiz]; inti; /** The socket used under Windows must be initialized with WSAStartup, otherwise it cannot be called*/ if(WSAStartup (Makeword (2,2), &wsadata)) {printf ("WSA failed\n"); return; } /** Detach the host address and relative path in the URL*/strcpy (myurl, URL); for(Phost = Myurl; *phost! ='/'&& *phost! =' /'; ++phost); if( (int) (phost-myurl) = =strlen (Myurl)) strcpy (GET,"/"); Elsestrcpy (GET, phost); *phost =' /'; strcpy (host, Myurl); printf ("%s\n%s\n", host, GET); /** Set socket parameters, not really initialized*/SOCKFD=sockets (Pf_inet, Sock_stream, ipproto_tcp); PURL=gethostbyname (host); Addr.sin_family=af_inet; Addr.sin_addr.s_addr= * ((unsignedLong*) purl->h_addr); Addr.sin_port= Htons ( the); /** Organization of information sent to the Web server * Why send the following information refer to the conventions of the HTTP protocol*/strcat (header,"GET"); Strcat (header, GET); Strcat (header,"http/1.1\r\n"); Strcat (header,"HOST:"); Strcat (header, host); Strcat (header,"\r\nconnection:close\r\n\r\n"); /** Connect to the server, send the request header, and accept feedback (ie Web source code)*/Connect (SOCKFD, (sockaddr*) &addr,sizeof(addr)); Send (SOCKFD, header, strlen (header),0); while(Recv (SOCKFD, text, Bufsiz,0) >0) {printf ("%s", text); Strnset (text,' /', Bufsiz); } closesocket (SOCKFD); WSACleanup ();}intMain () {Charurl[ the]; printf ("/ http"); scanf ("%s", URL); Geturl (URL); return 0;}
Using C language to get page encoding