JasonMahdjoub / BouncyCastle

Other
0 stars 2 forks source link

HTTPS in j2me #1

Open MiShtaz opened 1 year ago

MiShtaz commented 1 year ago

I have a j2me program. Previously the program took data from the site http://. Now the site is https://. Tell me please, in which place of the program code do I need to insert your code in order for the application to work with https?

public static synchronized String sendGET(String request) {
    System.out.println("GET " + request);
    HttpConnection httpConn = null;
    InputStream in = null;
    OutputStream os = null;
    try {
        // Open an HTTP Connection object
        httpConn = (HttpConnection) Connector.open(request);
        // Setup HTTP Request
        httpConn.setRequestMethod(HttpConnection.GET);
        httpConn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Confirguration/CLDC-1.1");
        httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpConn.setRequestProperty("Connection", "keep-alive");
        httpConn.setRequestProperty("Accept-Language", "en");
        httpConn.setRequestProperty("Accept-Charset", "utf-8");
        /** Initiate connection and check for the response code. If the
         response code is HTTP_OK then get the content from the target
         **/
        int respCode = httpConn.getResponseCode();
        if (respCode == httpConn.HTTP_OK) {
            byte[] buff = new byte[BUFFER_LENGTH];
            in = httpConn.openInputStream();
            if (in == null) {
                throw new IOException("InputStream is null, please check Access Point configuration");
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int n = in.read(buff);
            while (n > -1) {
                baos.write(buff, 0, n);
                n = in.read(buff);
            }
            baos.flush();
            baos.close();
            String response = new String(baos.toByteArray(), "UTF-8");
            if(response == null){
                throw new IOException();
            }
            return response;
        } else {
            //ignoring
            return null;
        }
    } catch (IOException e) {
        ScreenStack.showAlert("Failed to connect to the Internet");
        e.printStackTrace();
        return null;
    } catch (SecurityException e){
        ScreenStack.showAlert("For work, you need the Internet. Please allow access to the network");
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (in != null)
                in.close();
            if (os != null)
                os.close();
            if (httpConn != null)
                httpConn.close();
        } catch (IOException e) {
            //ignoring
        }
    }
}

Thanks in advance!

JasonMahdjoub commented 1 year ago

Hi, I have just recompiled BouncyCastle and changed the base package. The official repository of BouncyCastle is https://github.com/bcgit/bc-java Moreover, the HTTP URL appear to be defined with this code : "Connector.open(request)" You can try to change the request content as next "https://example.com/..." However, I don't known the class Connector. Instead of the class Connector, you can use the class URL by getting inspiration from in this tutorial : https://www.digitalocean.com/community/tutorials/java-httpurlconnection-example-java-http-request-get-post Have a nice day.