Warning: Table './petronio_db1/hijack_cache_page' is marked as crashed and should be repaired query: SELECT data, created, headers, expire FROM hijack_cache_page WHERE cid = 'http://hijack.it/node/4' in /accounts/petronio/www/drupal/includes/database.mysql.inc on line 174

Warning: Cannot modify header information - headers already sent by (output started at /accounts/petronio/www/drupal/includes/database.mysql.inc:174) in /accounts/petronio/www/drupal/includes/bootstrap.inc on line 569

Warning: Cannot modify header information - headers already sent by (output started at /accounts/petronio/www/drupal/includes/database.mysql.inc:174) in /accounts/petronio/www/drupal/includes/bootstrap.inc on line 570

Warning: Cannot modify header information - headers already sent by (output started at /accounts/petronio/www/drupal/includes/database.mysql.inc:174) in /accounts/petronio/www/drupal/includes/bootstrap.inc on line 571

Warning: Cannot modify header information - headers already sent by (output started at /accounts/petronio/www/drupal/includes/database.mysql.inc:174) in /accounts/petronio/www/drupal/includes/bootstrap.inc on line 572

Warning: Table './petronio_db1/hijack_watchdog' is marked as crashed and should be repaired query: INSERT INTO hijack_watchdog (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '<em>Cannot modify header information - headers already sent by (output started at /accounts/petronio/www/drupal/includes/database.mysql.inc:174)</em> in <em>/accounts/petronio/www/drupal/includes/common.inc</em> on line <em>141</em>.', 2, '', 'http://hijack.it/node/4', '', '54.224.158.232', 1506176830) in /accounts/petronio/www/drupal/includes/database.mysql.inc on line 174

Warning: Table './petronio_db1/hijack_watchdog' is marked as crashed and should be repaired query: INSERT INTO hijack_watchdog (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '<em>Table &amp;#039;./petronio_db1/hijack_watchdog&amp;#039; is marked as crashed and should be repaired\nquery: INSERT INTO hijack_watchdog (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (0, &amp;#039;flickr&amp;#039;, &amp;#039;Could not connect to Flickr, Error: Forbidden&amp;#039;, 1, &amp;#039;&amp;#039;, &amp;#039;http://hijack.it/node/4&amp;#039;, &amp;#039;&amp;#039;, &amp;#039;54.224.158.232&amp;#039;, 1506176831)</em> in <em>/accounts/petronio/www/drupal/includes/database.mysql.inc</em> on line <em>174</em>.', 2, '', 'http://hijack.it/node/4', '', '54.224.158.232', 1506176831) in /accounts/petronio/www/drupal/includes/database.mysql.inc on line 174
Client HTTP in java | Hijack.it di Giacomo Petronio

Client HTTP in java

  • warning: Cannot modify header information - headers already sent by (output started at /accounts/petronio/www/drupal/includes/database.mysql.inc:174) in /accounts/petronio/www/drupal/includes/common.inc on line 141.
  • user warning: Table './petronio_db1/hijack_watchdog' is marked as crashed and should be repaired query: INSERT INTO hijack_watchdog (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (0, 'flickr', 'Could not connect to Flickr, Error: Forbidden', 1, '', 'http://hijack.it/node/4', '', '54.224.158.232', 1506176831) in /accounts/petronio/www/drupal/includes/database.mysql.inc on line 174.
java
Un semplice client HTTP scritto in java che consente di visualizzare i messaggi a livello HTTP sia in fase di invio che le risposte del server.
E' possibile settare il metodo della richiesta (GET o POST), impostare manualmente gli headers e scrivere un eventuale body da mandare al server (utile usando il metodo POST).
Il file dispone di un interfaccia grafica ma è possibile utilizzare solamente il file HTTPClient in un progetto esterno.

Mi limiterò a commentare la classe HTTPClient, l'interfaccia grafica è velocemente realizzabile con un qualsiasi IDE come Netbeans.
Per aver maggior flessibilità e poter analizzare i messaggi a livello HTTP non utilizzerò classi come UrlConnection o HTTPUrlConnection ma implementerò un client che lavora a livello di stream su socket.

Il metodo principale è send, iniziando dalla parte di networking:
// NETWORK STUFFS
Socket socket = new Socket(host,port);
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Creo il socket e i canali per comunicare con il server.

Prima di mandare qualsiasi messaggio al server, devo costruire la richiesta partendo dal metodo seguito dal path della risorsa richiesta e dal protocollo:
// crea la request
request += method + " /" + " HTTP/1.0\r\n";
NOTA SULLA STRINGA NULLA IN HTTP/1.0:
Impostando HTTP/1.1, il server non chiude il flusso dati del socket, lasciando il client in attesa del fine flusso che non arriva, per questo imposto HTTP/1.0 così che il server a fine pagina chiuda il socket.

Ora aggiungiamo gli HTTP headers presenti in una hashtable contenenti il nome dell' header e il valore. Mi assicuro anche di aggiungere l' header "Content-Length" indispensabile nel caso di metodo POST:
// aggiungi headers
Enumeration keys = headers.keys(); 
while(keys.hasMoreElements()){ 
    String key = (String) keys.nextElement(); 
    String value = (String) headers.get(key);
    request += key + ": " + value + "\r\n"; 
} // controllo content-length, indispensabile per il POST 
if(headers.get("Content-Length:") == null ) 
    request += "Content-Length: " + body.getBytes().length + "\r\n";

Concludo la scrittura degli header lasciando una riga vuota e aggiungo il body della richiesta:
// linea di fine headers 
request += "\r\n"; 
// aggiungo il body 
request += body;

A questo punto posso spedire la richiesta sul socket:
// invio 
System.out.println(request+"\n"); 
out.print(request); 
out.flush();

Leggiamo ora la risposta del server:
String s;
while ( (s = in.readLine()) != null ){
    response += s + "\n";
    System.out.println(s);
}

Infine non resta che chiudere gli stream usati e i socket aperti.
in.close(); 
out.close(); 
socket.close(); 

Ecco tutto il codice completo di HTTPClient.java:

package httpclient;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Hashtable;

/**
*
* @author Giacomo Petronio
*/
public class HTTPClient {

    private String host = "";
    private int port = 80;
    private String path = "";
    private String method = "GET";
    private String body = "";
    private Hashtable headers = new Hashtable();


    /** Creates a new instance of HTTPClient */
    public HTTPClient() {
    }

    public void setHost(String host, int port, String path) {
        this.host = host;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public void setBody(String body) {

        this.body = body;
    }

    public void addRequestHeader(String key, String value){
        headers.put(key, value);
    }

    /**
    * returns 2 strings
    * String[0] is the request
    * String[1] is the response
    */
    public String[] send() throws IOException{

        String response = "";
        String request = "";

        // NETWORK STUFFS
        Socket socket = new Socket(host,port);
        PrintWriter out = new PrintWriter(socket.getOutputStream());
        BufferedReader in =  new BufferedReader(new InputStreamReader(socket.getInputStream()));


        // crea la request
        request += method + " /" + " HTTP/1.0\r\n";

        // aggiungi headers
        Enumeration keys = headers.keys();
        while(keys.hasMoreElements()){
            String key = (String) keys.nextElement();
            String value = (String) headers.get(key);
            request += key + ": " + value + "\r\n";
        }
        // controllo content-length, indispensabile per il POST
        if(headers.get("Content-Length:") == null )
            request += "Content-Length: " + body.getBytes().length + "\r\n";

        // linea di fine headers
        request += "\r\n";

        // aggiungo il body
        request += body;

        // invio

        System.out.println(request+"\n");
        out.print(request);
        out.flush();


        String s;
        while ( (s = in.readLine()) != null ){
            response += s + "\n";
            System.out.println(s);
        }

        in.close();
        out.close();
        socket.close();

        String[] result = new String[2];
        result[0] = request;
        result[1] = response;

        return result;
    }
}


In allegato trovate uno zip con tutti i sorgenti e il client grafico
AttachmentSize
httpclient.zip70.7 KB

Comments

http get response

it is very interesting... but...
if i try to obtain something from the get
the cycle while (....!=-1) {} does not stop until the time out.
it seems that doesn't appear the EndOfFile in the response..
Why?????? I don't understand.. :-(


Warning: Table './petronio_db1/hijack_watchdog' is marked as crashed and should be repaired query: INSERT INTO hijack_watchdog (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '<em>Table &amp;#039;./petronio_db1/hijack_cache_page&amp;#039; is marked as crashed and should be repaired\nquery: LOCK TABLES hijack_cache_page WRITE</em> in <em>/accounts/petronio/www/drupal/includes/database.mysql.inc</em> on line <em>174</em>.', 2, '', 'http://hijack.it/node/4', '', '54.224.158.232', 1506176831) in /accounts/petronio/www/drupal/includes/database.mysql.inc on line 174

Warning: Table './petronio_db1/hijack_watchdog' is marked as crashed and should be repaired query: INSERT INTO hijack_watchdog (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '<em></em> in <em>/accounts/petronio/www/drupal/includes/database.mysql.inc</em> on line <em>174</em>.', 2, '', 'http://hijack.it/node/4', '', '54.224.158.232', 1506176831) in /accounts/petronio/www/drupal/includes/database.mysql.inc on line 174

Warning: Table './petronio_db1/hijack_watchdog' is marked as crashed and should be repaired query: INSERT INTO hijack_watchdog (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '<em>Table &amp;#039;./petronio_db1/hijack_sessions&amp;#039; is marked as crashed and should be repaired\nquery: UPDATE hijack_sessions SET uid = 0, cache = 0, hostname = &amp;#039;54.224.158.232&amp;#039;, session = &amp;#039;messages|a:1:{s:5:\\&amp;quot;error\\&amp;quot;;a:2:{i:0;s:225:\\&amp;quot;user warning: Table &amp;amp;#039;./petronio_db1/hijack_cache_page&amp;amp;#039; is marked as crashed and should be repaired\\nquery: LOCK TABLES hijack_cache_page WRITE in /accounts/petronio/www/drupal/includes/database.mysql.inc on line 174.\\&amp;quot;;i:1;s:88:\\&amp;quot;user warning: in /accounts/petronio/www/drupal/includes/database.mysql.inc on line 174.\\&amp;quot;;}}&amp;#039;, timestamp = 1506176831 WHERE in /accounts/petronio/www/drupal/includes/database.mysql.inc on line 174