martes, 8 de mayo de 2012

Obtener peticion HTTP en Java

Una peticion HTTP no es mas que un String que envia un emisor para probocar una respuesta en un receptor.

Si se quiere ver una peticion HTTP lo mas facil es enviarla a un puerto donde este un socket escuchando.

Por ejemplo:

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
       ServerSocket server = new ServerSocket(8080);

       Socket so = server.accept();

       DataInputStream ent = new  DataInputStream(so.getInputStream());

       byte [] sal = new byte[256];
       while (ent.read(sal) != 0)
        System.out.println(new String(sal));
      }
}


Si se hace la peticion (por ejemplo con el SOAPUI) a localhost:8080/WS la respuesta seria:

POST /WS HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: Axis/1.4
Host: localhost:8080
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: "
"
Content-Length: 473

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<wsHola xmlns="http://es.indra.transporte.webservices.cscreplacement.core">

<respuesta>Hola Mundo</respuesta>

</wsHola>
</soapenv:Body>
</soapenv:Envelope>

Y ya esta.

No hay comentarios: