Conexão HTTPS - Tomcat - J2ME

3 respostas
A

Olá caros colegas. Gostaria de saber se alguém poderia me dar uma ajuda quanto a estabelecer conexões seguras (Https). Utilizo como ferramenta de desenvolvimento o netbeans 6.1 e o tomcat que já vem instalado com ele, versão 6.0.16. No meu código para fazer a conexão, utilizo a classe HttpConnection instanciando um objeto dessa classe e como URL coloco um endereço tipo “https://127.0.0.1:8080/MeuProjeto/”. Por favor, me ajudem a estabelecer uma conexão Https por mais simples que seja. Não sei se precisa adicionar algum patch ao tomcat ou configurar o seu config.xml.

Obrigado pela atenção.

3 Respostas

dtondo

essa é uma conexãao não segura… mas acho que é facil transformar ela em https… parte-se desse principio

/**
 * @author dtondo
 */
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
 
public class Connection {
    HttpConnection connection = null;
    InputStream iStream = null;
    OutputStream oStream = null;
    byte[] dataReceve = null;
    byte[] dataSend = null;
    String xmlRequest;
    int code;    
    
    public String doGet(String connectionString) throws Exception{
        try {
             connection = (HttpConnection) Connector.open(connectionString);
             code = connection.getResponseCode();
             if (code == HttpConnection.HTTP_OK) {
                iStream = connection.openInputStream();
                int length = (int) connection.getLength();
                if (length > 0) {
                    dataReceve = new byte[length];
                    int totalBytes = 0;
                    int bytesRead = 0;
                    while ((totalBytes < length) | (bytesRead > 0)) {
                        bytesRead = iStream.read(
                            dataReceve, totalBytes, length - totalBytes);
                        if (bytesRead > 0) {
                            totalBytes += bytesRead;
                        }
                    }
                }
             }
        } catch (Exception e) {
             throw new Exception("to be implemented");
        }finally{
            try {
                if (iStream != null) {
                    iStream.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception ex) {
                throw new Exception("to be implemented"); 
            }
        }
        return new String(dataReceve);
    }
    public String doPost(String connectionString) throws Exception{
        try {
            connection = (HttpConnection) Connector.open(connectionString);
            connection.setRequestMethod(HttpConnection.POST);
            oStream = connection.openOutputStream();
            int xmlLength = xmlRequest.length();
            if (xmlLength > 0) {
               // dataSend have the xml size
               dataSend = new byte[xmlLength];
               dataSend = xmlRequest.getBytes("UTF-8");
               oStream.write(dataSend);
               oStream.flush();
            }
            //save the http answer. for more information see: 
            //http://www.helpwithpcs.com/courses/html/html_http_status_codes.htm    
            code = connection.getResponseCode();
            if (code != HttpConnection.HTTP_OK) {
                throw new Exception("to be implemented"); 
            }
            iStream = connection.openInputStream();
            int receveLength = (int) connection.getLength();
            if (receveLength > 0) {
               dataReceve = new byte[receveLength];
               int totalBytes = 0;
               int bytesRead = 0;
               while ((totalBytes < receveLength) | (bytesRead > 0)) {
                   bytesRead = iStream.read(
                       dataReceve, totalBytes, receveLength - totalBytes);
                   if (bytesRead > 0) {
                       totalBytes += bytesRead;
                   }
                }
            }
            // a good choise is return your string in utf-8
            return new String(dataReceve, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
             throw new Exception("to be implemented"); 
        } finally {
            try {
                //that does nothing
                if (iStream != null) {
                    iStream.close();
                }
                if (oStream != null) {
                    oStream.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception e) {
                 throw new Exception("to be implemented"); 
            }
        }
    }
}
vanessasouza

Ola andersontoshio, to fazendo projeto final em JME, no inicio do ano tive um problema desse tipo, pois tinha feito em http e queria colocar para https e nao conseguia, descobri o seguinte:

O acesso via https em celulares com J2ME só pode ser feito nas seguintes condições:

  • A MIDP utilizada deve ser a 2.0 ou superior

  • O autenticador de certificado (CA) não pode ser você (isto é , o certificado não pode ser gerado pelo keytool da JDK, isso é não pode ser o tomcat), e sim um certificador confiável (por exemplo Certsign).

Da uma olhada: http://developers.sun.com/mobility/midp/articles/midp20/

Espero ter ajudado.

A

Galera valeu pela ajuda! Vou estudar o conteúdo que me enviaram, tanto os código quanto as páginas de referência. Vlw mesmoo!!! Qlqr dúvida volto aqui pra pedir socorro!

Criado 11 de setembro de 2008
Ultima resposta 12 de set. de 2008
Respostas 3
Participantes 3