Ver Mensaje Individual
  #1 (permalink)  
Antiguo 01/04/2013, 03:58
fox_rrb
 
Fecha de Ingreso: abril-2013
Mensajes: 2
Antigüedad: 11 años, 1 mes
Puntos: 0
Pregunta Petición ssl funciona en windows error en unix

Buenas días,

El método siguiente se encarga de realizar una petición ssl a una web que devuelve una cadena con parámetros para utilizar su API, se conecta a la web, envía la cabecera correcta de la petición POST y a continuación los parámetros de usuario y contraseña, después lee la respuesta y la devuelve.

El problema es que el método funciona correctamente en windows pero al ejecutarlo en ubuntu o mac no se obtiene ninguna respuesta.

Incluso he probado a realizar la petición por telnet en ubuntu de la siguiente manera:

telnet sitio.com 443

POST /user/login?client_id=xxx HTTP/1.1
Host: sitio.com
Content-type: application/x-www-form-urlencoded
Content-length: 33
Accept: application/json, text/javascript, */*; q=0.01

user=usuario&password=contrasena

y en vez de recibir la cadena esperada obtengo la respuesta :

Connection closed by foreign host.


Y el código del método sería similar a este:

Código:
public static String SSLSocketCliente(){
		String stRespuesta = "";
	     try {
	          Security.addProvider ( new com.sun.net.ssl. internal .ssl.Provider());
	          SSLSocketFactory factory =
	              (SSLSocketFactory)SSLSocketFactory.getDefault();
	          SSLSocket socket =
	              (SSLSocket)factory.createSocket( "sitio.com" , 443 );
	          /*
	           * send http request
	           *
	           * Before any application data is sent or received, the
	           * SSL socket will do SSL handshaking first to set up
	           * the security attributes.
	           *
	           * SSL handshaking can be initiated by either flushing data
	           * down the pipe, or by starting the handshaking by hand.
	           *
	           * Handshaking is started manually in this example because
	           * PrintWriter catches all IOExceptions (including
	           * SSLExceptions), sets an internal error flag, and then
	           * returns without rethrowing the exception.
	           *
	           * Unfortunately, this means any error messages are lost,
	           * which caused lots of confusion for others using this
	           * code.   The only way to tell there was an error is to call
	           * PrintWriter.checkError().
	           */
	          socket.startHandshake();
	          PrintWriter out = new PrintWriter( new BufferedWriter(
	          new OutputStreamWriter(socket.getOutputStream())));
	          
	          out .println( "POST /urlPeticion/user/login?client_id=xxxx HTTP/1.1" );
	          out .println( "Host: sitio.com" );
	          out .println( "Content-type: application/x-www-form-urlencoded" );
	          out .println( "Content-length: "+"user=usuario&password=contraseña".length());
	          out .println( "Accept: application/json, text/javascript, */*; q=0.01" );
	          out .println();
	          out .println( "user=usuario&password=contaseña" );
	          out .println();
	          out .flush();
	          /*
	           * Make sure there were no surprises
	           */
	          if ( out .checkError())
	              System. out .println( "SSLSocketClient:   java.io.PrintWriter error" );
	          /* read response */
	          BufferedReader in = new BufferedReader(
	              new InputStreamReader(socket.getInputStream()));
	          String inputLine;
	          while ((inputLine = in.readLine()) != null ){
	              System.out.println(inputLine);
	          	  stRespuesta += inputLine;
	          }
	          in.close();
	          out.close();
	          socket.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }      
	        return stRespuesta;
	}

Alguna idea de porque funciona en windows únicamente?, el proyecto es el mismo en todas las plataformas sincronizado por svn y además también he probado a copiar el proyecto completo directamente