Using C language to get page encoding

Source: Internet
Author: User
Tags set socket

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.