Package javax.microedition.io

Examples of javax.microedition.io.HttpsConnection


        }

        postData = tools.combineStrings(strings);
        String[] contentLen = {"Content-Length", String.valueOf(postData.length())};
        reqProps.addElement(contentLen);
        HttpsConnection sendCon = null;
        try{
            connMgr.open(url, "POST", reqProps, postData);
        }
        catch(ConnectionNotFoundException cnf)
        {
View Full Code Here


        return (sb.toString());
    }
   
    public static final String postViaHttpsConnection(String fullUrl) throws IOException, OAuthServiceProviderException {
        String[] urlPieces=split(fullUrl, "?");
        HttpsConnection c = null;
        DataInputStream dis = null;
        OutputStream os = null;
        int rc;
        String respBody = new String(""); // return empty string on bad things
        // TODO -- better way to handle unexpected responses
        try {
            System.out.println("UTIL -- posting to "+urlPieces[0]);
            c = (HttpsConnection)Connector.open(urlPieces[0], Connector.READ_WRITE); // hack for emulator?
           
            // Set the request method and headers
            c.setRequestMethod(HttpConnection.POST);
            c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
            c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            c.setRequestProperty("Content-Length", ""+urlPieces[1].length());
            // TODO -- add'l headers for t-mobile?
           
            // Getting the output stream may flush the headers
            os = c.openOutputStream();
            System.out.println("UTIL -- writing POST data: "+urlPieces[1]);
            os.write(urlPieces[1].getBytes());
            os.flush();           // Optional, getResponseCode will flush
           
            // Getting the response code will open the connection,
            // send the request, and read the HTTP response headers.
            // The headers are stored until requested.
            rc = c.getResponseCode();
           
            int len = c.getHeaderFieldInt("Content-Length", 0);
            //int len = (int)c.getLength();
            System.out.println("content-length="+len);
            dis = c.openDataInputStream();
           
            byte[] data = new byte[len];
            if (len == 0) {
                System.out.println("UTIL -- no length, reading individual characters...");
                ByteArrayOutputStream tmp = new ByteArrayOutputStream();
                int ch;
                while( ( ch = dis.read() ) != -1 ) {
                    tmp.write( ch );
                }
                data = tmp.toByteArray();
            } else {
                System.out.println("UTIL -- got a length, reading...");
                dis.readFully(data);
            }
            respBody=new String(data);
        } catch (ClassCastException e) {
            throw new IllegalArgumentException("Not an HTTP URL");
        } finally {
            if (dis != null)
                dis.close();
            if (os != null)
                os.close();
            if (c != null)
                c.close();
        }
        if (rc != HttpConnection.HTTP_OK) {
            throw new OAuthServiceProviderException("HTTP response code: " + rc, rc, respBody);
        }
        return respBody;
View Full Code Here

        }
        return respBody;
    }
   
    public static final String getViaHttpsConnection(String url) throws IOException, OAuthServiceProviderException {
        HttpsConnection c = null;
        DataInputStream dis = null;
        OutputStream os = null;
        int rc;
        String respBody = new String(""); // return empty string on bad things
        // TODO -- better way to handle unexpected responses
       
        try {
            System.out.println("UTIL -- opening connection");
            c= (HttpsConnection) Connector.open(url, Connector.READ);
            c.setRequestMethod(HttpConnection.GET);
            c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
            c.setRequestProperty("Cache-Control", "no-store");
            c.setRequestProperty("Connection", "close"); // not sure this is a good idea, but HTTP/1.0 might be less error-prone, some clients have trouble with chunked responses
            System.out.println("UTIL -- connection open");
           
            // Getting the response code will open the connection,
            // send the request, and read the HTTP response headers.
            // The headers are stored until requested.
            rc = c.getResponseCode();
            System.out.println("UTIL -- got response code" + rc);
           
            // Get the length and process the data
            int len = c.getHeaderFieldInt("Content-Length", 0);

            System.out.println("content-length="+len);
            dis = c.openDataInputStream();
           
            byte[] data = null;
            if (len == -1L) {
                System.out.println("UTIL -- no length, reading individual characters...");
                ByteArrayOutputStream tmp = new ByteArrayOutputStream();
                int ch;
                while( ( ch = dis.read() ) != -1 ) {
                    tmp.write( ch );
                }
                data = tmp.toByteArray();
            } else {
                System.out.println("UTIL -- got a length, reading...");
                data = new byte[len];
                dis.read(data);
            }
            respBody=new String(data);
        } catch (ClassCastException e) {
            throw new IllegalArgumentException("Not an HTTP URL");
        } finally {
            if (dis != null)
                dis.close();
            if (c != null)
                c.close();
        }
        if (rc != HttpConnection.HTTP_OK) {
            throw new OAuthServiceProviderException("HTTP response code: " + rc, rc, respBody);
        }
        return respBody;
View Full Code Here

TOP

Related Classes of javax.microedition.io.HttpsConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.