Win32-google Browser

Source: Internet
Author: User

Ping Baidu to get an IP browser to send a GET request//Baidu Server received a request return something to you//return the thing that we want//////////////////////////////////////////////////////////////////////#include "stdafx.h" #include <iostream> #include <winsock2.h> #include <string> #pragma comment (lib, "Ws2_32.lib") using namespace std;/** * Parse URL * @param strurl the domain to be resolved * @param strhost out * @param strresource out * @return false parsing failed */bool P Arseurl (const string strURL, String &strhost, String &strresource) {BOOL bRet = false;const int maxresourcesize = 2 000;const int minhostsize = 5;do {string strhttp = "http://";//Find "//" from strURL "/" const char * szpos = STRSTR (strurl.c_st R (), Strhttp.c_str ());//If the first "http://" is not found, the description does not have httpif (NULL = = Szpos) {Szpos = Strurl.c_str ();} Else{szpos = Szpos + strhttp.length ();} Char Szhost[maxbyte] = {0};char Szresource[maxresourcesize] = {0};//standard notation: Length (maxresourcesize =) sscanf_s (SzPos, "%[^/]%s", Szhost,maxbyte, Szresource, MAXBYTE); if (Minhostsize > strlen (szhost)) {break;} Strhost = szhost;//if not equal to 0, the description szresource is not empty if (0! = strlen (Szresource)) {strresource = Szresource;} Else{strresource = "/";} BRet = TRUE;} while (FALSE); return bRet;} int _tmain (int argc, _tchar* argv[]) {BOOL bRet = FALSE; SOCKET Socketserver = invalid_socket;do {wsadata wsadata;//initialization socketif (0! = WSAStartup (Makeword (2, 2), &wsadata)) {b Reak;} if (2! = Lobyte (wsadata.wversion) | | 2! = hibyte (wsadata.wversion)) {break;} Server: Through programming, the domain name is converted into ipstring strURL = "WWW.WMPIC.ME/MEINV"; string strhost, Strresouce; parseURL (strURL, Strhost, Strresouce);p rintf ("host:%s \ t rosouce:%s \ r \ n", Strhost.c_str (), Strresouce.c_str ());// Get Host hostent * hserverhost = gethostbyname (Strhost.c_str ()); if (NULL = = hserverhost) {wsacleanup (); break;} Sockaddr_in Addrserver = {0};addrserver.sin_family = Af_inet;addrserver.sin_port = htons (80);//80 Port memcpy (& ( ADDRSERVER.SIN_ADDR), HSERVERHOST-&GT;H_ADDR, 4);//Create socketsocketserver = socket (af_inet, Sock_stream, IPPROTO_TCP); if (Invalid_socket = = socketserver) {closesocket (socketserver); WSACleanup (); break;} Connect socketif (Socket_error = = Connect (socketserver, (sockaddr*) &addrserver, sizeof (Addrserver))) {closesocket ( Socketserver); WSACleanup (); break;} Send GET Request: "Get" followed by a space, "http/1.1" HTTP preceded by a space//right value for the interception packet gets the string strrequest = "Get" + Strresouce + "Http/1.1\r\nhost : "+ strhost +" \r\nconnection:close\r\n\r\n "; if (socket_error = = Send (Socketserver, Strrequest.c_str (), Strrequest.length (), 0)) {closesocket (socketserver); WSACleanup (); break;} Char szbuffer[2000] = {0};//The returned data cannot be received at one time, so it must be read in a loop until Recv returns 0, received!int irecvsize = 1;while (0<irecvsize) { Irecvsize = recv (socketserver, szbuffer, 0);cout<< "szbuffer =" &LT;&LT;SZBUFFER&LT;&LT;ENDL;} Clean Socketif (Invalid_socket! = socketserver) {closesocket (socketserver); WSACleanup ();}} while (FALSE); GetChar (); return 0;}

  

Win32-google Browser

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.