A/C + + socket sends HTTP requests to visit Web sites

Source: Internet
Author: User
Tags get ip md5 encryption urlencode

These days less classes, the campus network to authenticate to the Internet, every time must enter the number code, in order to facilitate, wrote an automatic login and if online, log in from the service system to force the offline gadget.

Forced offline thinking: Get SessionID----------"Get Verification Code picture------------" aspriseocrlib identification code---------------"MD5 encryption, URL Encode----------------"Login from service system----------------" forced downline---------------------"log out.

Issues to be aware of:

1, to obtain the verification code, send the request should sleep 1 seconds, waiting to receive the completion, and then call recv read buffer, otherwise the picture may be accepted.

2, the login request after delay, waiting for the server to put the login status set into the session, can be operated.

3. Each request needs to use a different socket connection.

Paste part of the following code: (Code has a lot of omissions, welcome correction, common progress)

Reprint please indicate the source.

1. Get the connection

1BOOL Cforceoutlinedlg::connecttoserver (ConstCString strserverurl,socket& Socketclient,ConstUnsigned Shortnport)2 {   3BOOL BRet =FALSE; 4 wsadata wsadata;5WORD wversion = Makeword (2,2);6 7      Do   8     {  9         if(0! = WSAStartup (wversion, &wsadata)) Ten         {   One              Break;  A         }   -  -         if(Lobyte (wsadata.wversion)! =2|| Hibyte (wsadata.wversion)! =2 )   the         {   - WSACleanup ();  -              Break;  -         }   +  - lphostent lphosttent;  +Lphosttent = gethostbyname (Strserverurl);///get IP based on URL A         if(NULL = =lphosttent) at         { -              Break;  -         } -  -          -         if(Socketclient = =invalid_socket) in         {   - WSACleanup ();  to              Break;  +         }   -  theSockaddr_in Socketserver;//Server *socketserver.sin_family =af_inet;  $Socketserver.sin_port =htons (Nport); Panax NotoginsengSOCKETSERVER.SIN_ADDR = * ((LPIN_ADDR) *lphosttent->h_addr_list);  -  the  +Socketclient = socket (af_inet, Sock_stream,0); A  the         if(Socket_error = = Connect (socketclient, (LPSOCKADDR) &socketserver,sizeof(sockaddr_in)))  +         { -             intNerrorcode =WSAGetLastError ();  $ closesocket (socketclient);  $              Break; -         }   -  theBRet =TRUE;  -} while(FALSE);Wuyi  the     returnBRet; -}

2. Receive Verification Code

BOOL Cforceoutlinedlg::getvalidatecode (/*socket s,*/cstring sessionid) {SOCKET client; Connecttoserver ("IP", client);//Initialize connection CString cstrsenddata;cstrsenddata = "get/randomcodeaction.action?randomnum= 0.1 http/1.1\r\n "; Cstrsenddata + =" Host: \ r \ n "; Cstrsenddata + =" connection:keep-alive\r\n "; Cstrsenddata + =" accept:i Mage/webp,image/*,*/*;q=0.8\r\n "; Cstrsenddata + =" user-agent:mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/49.0.2623.87 safari/537.36\r\n "; Cstrsenddata + =" referer:\r\n "; Cstrsenddata + = "accept-encoding:gzip, deflate, sdch\r\n"; Cstrsenddata + = "accept-language:zh-cn,zh;q=0.8\r\n"; Cstrsenddata + = "cookie:jsessionid=" + SessionID + "\ r \ n"; Cstrsenddata + = "\ r \ n"; if ( -1 = = Send (client, CSTRSENDDATA.GETB Uffer (), Cstrsenddata.getlength (), 0)) {return FALSE;} Char buff[4096]={0}; Sleep (1000);//delay 1 seconds, wait for the picture stream to be received recv (client, buff, sizeof (buff), 0); CString temp = buff;int Nfirst = temp. Find ("Close", 0); if (Nfirst = =-1) return False;nfirst + = 9;//PlusClose two \ r \ n A 0 length int piclength = Getpiclength (Buff+nfirst); Ofstream out ("Aa.png", ios::out|ios::binary); Out.write ( Buff+nfirst,piclength); Out.flush (); Out.close (); closesocket (client); return TRUE;} int cforceoutlinedlg::getpiclength (char* pic)//Find the position of 5 0x00 consecutively {int count = 0,i;for (i = 0;; i++) {if (pic[i]==0) {count++;if (count==5) {break;}} Else{count = 0;}} return i-4;}

3, UrlEncode

CString cforceoutlinedlg::urlencode (const cstring& sztoencode) {  std::string src = sztoencode;  Char hex[] = "0123456789ABCDEF";  CString DST;  for (size_t i = 0; i < src.size (); ++i)  {  unsigned char cc = src[i];if (cc = = ' \ \ ') continue;if (cc >= ' A ' &A mp;& cc <= ' z '   | | cc >= ' a ' && cc <= ' Z '  | | cc >= ' 0 ' && cc <= ' 9 '  | | cc = = ' . '  | | CC = = ' _ ' |  | cc = = '-'  | | cc = = ' * ')  {  if (cc = = ')  {DST + =  "+";  }  else  DST + = cc;  }  else  {  unsigned char c = static_cast<unsigned char> (src[i]);  DST + = '% ';  DST + = hex[c/16];  DST + = hex[c%];  }  }  return DST;}  

Reprint please indicate the source.

A/C + + socket sends HTTP requests to visit Web sites

Related Article

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.