0

I have set up the Asterisk Server properly for allowing GUI interface, to check this I have tried & tested an already available Application for Android & tested the same with my browser. I am able to login & view the files. eg. http://192.168.8.x:8088/asterisk/rawman?action=getconfig&filename=users.conf

this commands shows me the user.conf file.

However the same commands does not works from my Android Application. It results in

Response: Error Message: Permission denied

my code: 1st button click: try{ new mygoogleSearch().execute(http://192.168.8.x:8088/asterisk/rawman?action=login&username=tismo&secret=tismo123); } catch(Exception e) { Log.v("Exception google search","Exception:"+e.getMessage()); }

this returns: 03-27 17:27:09.468: E/GoogleSearch(21686): Response: SuccessMessage: Authentication accepted

On 2nd Button click:

try{
            new Execute().execute("http://192.168.8.4:8088/asterisk/rawman?action=getconfig&filename=test.conf");
} catch(Exception e) {
Log.v("Exception google search","Exception:"+e.getMessage());
}

class mygoogleSearch extends AsyncTask {

protected String doInBackground(String... searchKey) {
    ;
    String cmd = searchKey[0];
    try {
        return  action(cmd);

    } catch(Exception e) {
        Log.v("Exception ",
                "Exception:"+e.getMessage());
        return "";
    }
}

private String action(String uRL) throws MalformedURLException, IOException { String newFeed= uRL; StringBuilder response = new StringBuilder();

    URL url = new URL(newFeed);
    HttpURLConnection httpconn  = (HttpURLConnection) url.openConnection();
    httpconn.setUseCaches(false);
    //httpconn.setRequestProperty("Cache", "false");
    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();
    }
    return response.toString();

}

this returns:

03-27 17:28:31.808: E/GoogleSearch(21800): Response: ErrorMessage: Permission denied

user2119228
  • 71
  • 1
  • 8
  • do you login before action: getconfig ? What is in your `/etc/asterisk/manager.conf` ? – pce Mar 27 '13 at 10:44
  • Yes I am logging in, after that I am issuing command to getconfig. I can easily login & view the configuration by using : myWebView.loadUrl("http://192.168.8.4:8088/asterisk/rawman?action=login&username=admin&secret=admin123"); butexcept for login nothing seems to be working while using HTTP APIs myWebView.loadUrl("http://192.168.8.4:8088/asterisk/rawman?action=getconfig&filename=sip.conf"); – user2119228 Mar 27 '13 at 10:52
  • new Execute().execute("http://192.168.8.4:8088/asterisk/rawman?action=login&username=tismo&secret=tismo123"); this returns: 03-27 17:27:09.468: E/GoogleSearch(21686): Response: SuccessMessage: Authentication accepted then on next button click: new Execute().execute(""http://192.168.8.4:8088/asterisk/rawman?action=getconfig&filename=sip.conf""); 03-27 17:28:31.808: E/GoogleSearch(21800): Response: ErrorMessage: Permission denied – user2119228 Mar 27 '13 at 12:02
  • do you connect to the same session? @see my example/answer – pce Mar 27 '13 at 13:49
  • No..I guess that was my mistake..i am trying to fix it now.. – user2119228 Mar 28 '13 at 04:50
  • finally this worked for me.. http://stackoverflow.com/questions/15674371/how-to-use-cookies-in-httpurlconnection-for-android – user2119228 Mar 29 '13 at 04:50

1 Answers1

0

You need a CookieManager to connect to the same session.

Quote from the asteriskbook "the definitive Guide"
"The LOGIN command authenticates credentials for the Manager interface’s HTML view. Once you are logged in, Asterisk stores a cookie on your browser (valid for the length of the httptimeout setting). This cookie is used to connect to the same session."

Update: Examples of a CookieManager Storage to mimic the Browser:
How to use Cookies with HttpUrlConnection, persist Cookies using HttpUrlConnection

Community
  • 1
  • 1
pce
  • 4,160
  • 1
  • 16
  • 24
  • Well I guess you are right..I need to use cookie...I tried few implementations...but still I need to figure the correct way to use cookies.http://stackoverflow.com/questions/15674371/how-to-use-cookies-in-httpurlconnection-for-android – user2119228 Mar 28 '13 at 04:58
  • finally this worked http://stackoverflow.com/questions/15674371/how-to-use-cookies-in-httpurlconnection-for-android – user2119228 Mar 29 '13 at 04:51