Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Get HTML Code - get.cpp

get.cpp

Caricato da:
Scarica il programma completo

  1. /*
  2. Get HTML Code
  3. Descrizione:    Scarica il codice HTML di un link passato come argomento
  4. Autore:         __GiReX__
  5. Linguaggio:     C++
  6. Site:           http://girex.altervista.org
  7. Compilare con Dev-C++ e linkare a mano la libreria libwsock32.a
  8. */
  9.  
  10. #include <iostream>
  11. #include <fstream>
  12. #include <cstring>
  13. #include <winsock.h>
  14. using namespace std;
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.  int error;
  19.  
  20.  WSADATA info;
  21.  SOCKET sock;
  22.  SOCKADDR_IN sock_addr;
  23.  LPHOSTENT host;
  24.  
  25.  char crec, buff[1024];
  26.   ofstream file("log.html", ios::app);
  27.  
  28.  if(argc != 3) {
  29.         cout << "\nUsage: <prog> <link> <path>\n";
  30.         cout << "Example: get-html.exe  girex.altervista.org  /index.php\n";
  31.         return 1;
  32. }
  33.    
  34.     WSAStartup(MAKEWORD(2,2), &info);
  35.      sock = socket(PF_INET, SOCK_STREAM, 0);
  36.       sock_addr.sin_family = PF_INET;                     //  Protocollo in uso
  37.       sock_addr.sin_port = htons(80);                     //  Porta
  38.        host = gethostbyname(argv[1]);                     //  IP come argomento
  39.        
  40.         if(!host) {
  41.            cout << "\nErrore, impossibile risolvere l'host, ricontrolla il formato d'immissione\n";
  42.            cout << "Example: get-html.exe  girex.altervista.org  /index.php\n";
  43.           return 1;
  44.         }                            
  45.        
  46.        sock_addr.sin_addr = *((LPIN_ADDR)*host->h_addr_list);        // Risolvo l'IP
  47.          
  48.         error = connect(sock,(struct sockaddr*)&sock_addr,sizeof(struct sockaddr)); // Connessione con l'host
  49.          
  50.           if(error == SOCKET_ERROR) {
  51.            cout << "\nErrore di connessione\n";
  52.           return 1;
  53.         }    
  54.        
  55.          strcpy(buff, "GET ");                 // Richiesta GET
  56.          strcat(buff, argv[2]);
  57.          strcat(buff, " HTTP/1.1\r\n");
  58.          strcat(buff, "Host: ");
  59.          strcat(buff, argv[1]);
  60.          strcat(buff, "\r\nUser-Agent: __GiReX__ Software\r\n");
  61.          strcat(buff, "Connection: Close\r\n\r\n");
  62.              
  63.           send(sock, buff, strlen(buff), 0);
  64.  
  65.            do {
  66.               error = recv(sock, &crec, sizeof(crec), 0);
  67.               file.put(crec);
  68.         } while(error);
  69.        
  70.         cout << "\nOperazione completata!\n";
  71.        
  72.        file.close();
  73.      closesocket(sock);
  74.    WSACleanup();
  75.  return 0;
  76. }