sdk/src/com/socialize/facebook/Util.java
public static String openUrl(String url, String method, Bundle params)
          throws MalformedURLException, IOException {
        // random string as boundary for multi-part http post
        String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
        String endLine = "\r\n";

        OutputStream os;

        if (method.equals("GET")) {
            url = url + "?" + encodeUrl(params);
        }
        Util.logd("Facebook-Util", method + " URL: " + url);
        HttpURLConnection conn =
            (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("User-Agent", System.getProperties().
                getProperty("http.agent") + " FacebookAndroidSDK");
        if (!method.equals("GET")) {
            Bundle dataparams = new Bundle();
            for (String key : params.keySet()) {
                Object parameter = params.get(key);
                if (parameter instanceof byte[]) {
                    dataparams.putByteArray(key, (byte[])parameter);
                }
            }

            // use method override
            if (!params.containsKey("method")) {
                params.putString("method", method);
            }

            if (params.containsKey("access_token")) {
                String decoded_token =
                    URLDecoder.decode(params.getString("access_token"));
                params.putString("access_token", decoded_token);
            }

            conn.setRequestMethod("POST");
            conn.setRequestProperty(
                    "Content-Type",
                    "multipart/form-data;boundary="+strBoundary);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.connect();
            os = new BufferedOutputStream(conn.getOutputStream());

            os.write(("--" + strBoundary +endLine).getBytes());
            os.write((encodePostBody(params, strBoundary)).getBytes());
            os.write((endLine + "--" + strBoundary + endLine).getBytes());

            if (!dataparams.isEmpty()) {

                for (String key: dataparams.keySet()){
                    os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());
                    os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());
                    os.write(dataparams.getByteArray(key));
                    os.write((endLine + "--" + strBoundary + endLine).getBytes());

                }
            }
            os.flush();
        }

        String response = "";
        try {
            response = read(conn.getInputStream());
        } catch (FileNotFoundException e) {
            // Error Stream contains JSON that we can parse to a FB error
            response = read(conn.getErrorStream());
        }
        return response;
    }

    private static String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }

    public static void clearCookies(Context context) {
        // Edge case: an illegal state exception is thrown if an instance of
        // CookieSyncManager has not be created.  CookieSyncManager is normally
        // created by a WebKit view, but this might happen if you start the
        // app, restore saved state, and click logout before running a UI
        // dialog in a WebView -- in which case the app crashes
        @SuppressWarnings("unused")
        CookieSyncManager cookieSyncMngr =
            CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    }
public static String openUrl(String url, String method, Bundle params)
          throws MalformedURLException, IOException {
        // random string as boundary for multi-part http post
        String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
        String endLine = "\r\n";

        OutputStream os;

        if (method.equals("GET")) {
            url = url + "?" + encodeUrl(params);
        }
        Util.logd("Facebook-Util", method + " URL: " + url);
        HttpURLConnection conn =
            (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("User-Agent", System.getProperties().
                getProperty("http.agent") + " FacebookAndroidSDK");
        if (!method.equals("GET")) {
            Bundle dataparams = new Bundle();
            for (String key : params.keySet()) {
                Object parameter = params.get(key);
                if (parameter instanceof byte[]) {
                    dataparams.putByteArray(key, (byte[])parameter);
                }
            }

            // use method override
            if (!params.containsKey("method")) {
                params.putString("method", method);
            }

            if (params.containsKey("access_token")) {
                String decoded_token =
                    URLDecoder.decode(params.getString("access_token"));
                params.putString("access_token", decoded_token);
            }

            conn.setRequestMethod("POST");
            conn.setRequestProperty(
                    "Content-Type",
                    "multipart/form-data;boundary="+strBoundary);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.connect();
            os = new BufferedOutputStream(conn.getOutputStream());

            os.write(("--" + strBoundary +endLine).getBytes());
            os.write((encodePostBody(params, strBoundary)).getBytes());
            os.write((endLine + "--" + strBoundary + endLine).getBytes());

            if (!dataparams.isEmpty()) {

                for (String key: dataparams.keySet()){
                    os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());
                    os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());
                    os.write(dataparams.getByteArray(key));
                    os.write((endLine + "--" + strBoundary + endLine).getBytes());

                }
            }
            os.flush();
        }

        String response = "";
        try {
            response = read(conn.getInputStream());
        } catch (FileNotFoundException e) {
            // Error Stream contains JSON that we can parse to a FB error
            response = read(conn.getErrorStream());
        }
        return response;
    }

    private static String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }

    public static void clearCookies(Context context) {
        // Edge case: an illegal state exception is thrown if an instance of
        // CookieSyncManager has not be created.  CookieSyncManager is normally
        // created by a WebKit view, but this might happen if you start the
        // app, restore saved state, and click logout before running a UI
        // dialog in a WebView -- in which case the app crashes
        @SuppressWarnings("unused")
        CookieSyncManager cookieSyncMngr =
            CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    }
public static String openUrl ( String url , String method , Bundle params ) throws MalformedURLException , IOException { // random string as boundary for multi-part http post String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f" ; String endLine = "\r\n" ; OutputStream os ; if ( method . equals ( "GET" ) ) { url = url + "?" + encodeUrl ( params ) ; } Util . logd ( "Facebook-Util" , method + " URL: " + url ) ; HttpURLConnection conn = ( HttpURLConnection ) new URL ( url ) . openConnection ( ) ; conn . setRequestProperty ( "User-Agent" , System . getProperties ( ) . getProperty ( "http.agent" ) + " FacebookAndroidSDK" ) ; if ( ! method . equals ( "GET" ) ) { Bundle dataparams = new Bundle ( ) ; for ( String key : params . keySet ( ) ) { Object parameter = params . get ( key ) ; if ( parameter instanceof byte [ ] ) { dataparams . putByteArray ( key , ( byte [ ] ) parameter ) ; } } // use method override if ( ! params . containsKey ( " method " ) ) { params . putString ( " method " , method ) ; } if ( params . containsKey ( "access_token" ) ) { String decoded_token = URLDecoder . decode ( params . getString ( "access_token" ) ) ; params . putString ( "access_token" , decoded_token ) ; } conn . setRequestMethod ( "POST" ) ; conn . setRequestProperty ( "Content-Type" , "multipart/form-data;boundary=" + strBoundary ) ; conn . setDoOutput ( true ) ; conn . setDoInput ( true ) ; conn . setRequestProperty ( "Connection" , "Keep-Alive" ) ; conn . connect ( ) ; os = new BufferedOutputStream ( conn . getOutputStream ( ) ) ; os . write ( ( "--" + strBoundary + endLine ) . getBytes ( ) ) ; os . write ( ( encodePostBody ( params , strBoundary ) ) . getBytes ( ) ) ; os . write ( ( endLine + "--" + strBoundary + endLine ) . getBytes ( ) ) ; if ( ! dataparams . isEmpty ( ) ) { for ( String key : dataparams . keySet ( ) ) { os . write ( ( "Content-Disposition: form-data; filename=\"" + key + "\"" + endLine ) . getBytes ( ) ) ; os . write ( ( "Content-Type: content/unknown" + endLine + endLine ) . getBytes ( ) ) ; os . write ( dataparams . getByteArray ( key ) ) ; os . write ( ( endLine + "--" + strBoundary + endLine ) . getBytes ( ) ) ; } } os . flush ( ) ; } String response = "" ; try { response = read ( conn . getInputStream ( ) ) ; } catch ( FileNotFoundException e ) { // Error Stream contains JSON that we can parse to a FB error response = read ( conn . getErrorStream ( ) ) ; } return response ; } private static String read ( InputStream in ) throws IOException { StringBuilder sb = new StringBuilder ( ) ; BufferedReader r = new BufferedReader ( new InputStreamReader ( in ) , 1000 ) ; for ( String line = r . readLine ( ) ; line != null ; line = r . readLine ( ) ) { sb . append ( line ) ; } in . close ( ) ; return sb . toString ( ) ; } public static void clearCookies ( Context context ) { // Edge case: an illegal state exception is thrown if an instance of // CookieSyncManager has not be created.  CookieSyncManager is normally // created by a WebKit view, but this might happen if you start the // app, restore saved state, and click logout before running a UI // dialog in a WebView -- in which case the app crashes @ SuppressWarnings ( "unused" ) CookieSyncManager cookieSyncMngr = CookieSyncManager . createInstance ( context ) ; CookieManager cookieManager = CookieManager . getInstance ( ) ; cookieManager . removeAllCookie ( ) ; }
Connect to an HTTP URL and return the response as a string.

Note that the HTTP method override is used on non-GET requests. (i.e.
requests are made as "POST" with method specified in the body).

@param url - the resource to open: must be a welformed URL
@param method - the HTTP method to use ("GET", "POST", etc.)
@param params - the query parameter for the URL (e.g. access_token=foo)
@return the URL contents as a String
@throws MalformedURLException - if the URL format is invalid
@throws IOException - if a network problem occurs
Connect to an HTTP URL and return the response as a string .