Skip to content Skip to sidebar Skip to footer

How To Use Cookies In Httpurlconnection For Android?

I am trying to create an Application for Asterisk Interface, the configuration setup has been done properly as I have double checked it. It seems the reason why I can't access Conf

Solution 1:

private String action(String uRL)
            throws MalformedURLException, IOException {

        StringBuilder response = new StringBuilder();
        Log.v("Execute","execute url:"+uRL);
        URL url = new URL(uRL);
        HttpURLConnection httpconn  = (HttpURLConnection) url.openConnection();

        if(sCookie!=null && sCookie.length()>0){
            httpconn.setRequestProperty("Cookie", sCookie);
        }

        if(httpconn.getResponseCode()==HttpURLConnection.HTTP_OK) {
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(httpconn.getInputStream()),
                    8192);
            String strLine = null;
            while ((strLine = input.readLine()) != null) {
                response.append(strLine);
            }
            input.close();
        }

        String cookie = httpconn.getHeaderField("set-cookie");
        if(cookie!=null && cookie.length()>0){
            sCookie = cookie;
        }
        return response.toString();

    }

Post a Comment for "How To Use Cookies In Httpurlconnection For Android?"