Dave992
Dave992

Reputation: 51

Log In to Website

I am making an app that needs to login to a website. To do so I make a HttpClient and perform a HttpPost to the url. This is working as far as I know. The StatusCodes give back 200. After this I perform a HttpGet to the restricted url (with the same client). However, I always get back a Forbidden Exception or the log in screen back in my LogCat. From searching on the web I found that it is sometimes needed to first perform a HttpGet to the site before the HttpPost, and also to attach a CookieStore. Done both and still no succes.. Somebody here that can help me with this?

        HttpClient client = new DefaultHttpClient();

        HttpGet initiate = new HttpGet(url);            
        HttpPost post = new HttpPost(url);
        HttpGet page = new HttpGet(restrictedurl);
        HttpContext localContext = new BasicHttpContext();

        CookieStore cookieStore = new BasicCookieStore();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

                HttpResponse response = client.execute(initiate, localContext);
                Log.i(Tag, " " + response.getStatusLine().getStatusCode());
                response.getEntity().consumeContent();

                response = client.execute(post, localContext);
                Log.i(Tag, " " + response.getStatusLine().getStatusCode());             
                response.getEntity().consumeContent();                                      

                ResponseHandler<String> handler = new BasicResponseHandler();
                String htmlpage = client.execute(page, handler, localContext);
                Log.i(Tag, " " + response.getStatusLine().getStatusCode());
                Log.i(Tag, htmlpage);
                response.getEntity().consumeContent();  

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

Here is my LogCat:

    06-06 13:38:47.033: I/LogTag    (27839):  StatusCode HttpGet: 200
    06-06 13:38:47.197: I/LogTag    (27839):  StatusCode HttpPost: 200
    06-06 13:38:47.361: W/System.err(27839): org.apache.http.client.HttpResponseException: Forbidden
    06-06 13:38:47.377: W/System.err(27839):    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
    06-06 13:38:47.384: W/System.err(27839):    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
    06-06 13:38:47.384: W/System.err(27839):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
    06-06 13:38:47.384: W/System.err(27839):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
    06-06 13:38:47.384: W/System.err(27839):    at com.dakro.wiebetaaltwat.MainActivity$AsyncPost.doInBackground(MainActivity.java:136)
    06-06 13:38:47.384: W/System.err(27839):    at com.dakro.wiebetaaltwat.MainActivity$AsyncPost.doInBackground(MainActivity.java:1)
    06-06 13:38:47.392: W/System.err(27839):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
    06-06 13:38:47.392: W/System.err(27839):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    06-06 13:38:47.392: W/System.err(27839):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    06-06 13:38:47.392: W/System.err(27839):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
    06-06 13:38:47.392: W/System.err(27839):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    06-06 13:38:47.400: W/System.err(27839):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    06-06 13:38:47.400: W/System.err(27839):    at java.lang.Thread.run(Thread.java:856)

Upvotes: 4

Views: 410

Answers (3)

Dave992
Dave992

Reputation: 51

Sweet succes! Although I could POST to the URL and get an 200 StatusCode back it wasn't the "complete" url. Had to add index.php to it. Also the form data was not complete. Needed to add two more parameters.

                params.add(new BasicNameValuePair("action", "login"));
                params.add(new BasicNameValuePair("login_submit", "Inloggen"));

Found this out by inspecting the site for the form data it posted to the site. Used the build in Developer Tools of Chrome(Network Tab). Thanks for the responses!

Upvotes: 1

Amit kumar
Amit kumar

Reputation: 1605

you can use this condition

 StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();
                if (statusCode == 200) {

                } else {

                }

Upvotes: 1

psx
psx

Reputation: 4048

The forbidden error normally indicates a permissions issue on the directory/file you are accessing on the web server.

Check the owner and group permissions. Also check the write/read/execute flags on the file.

Can you navigate directly to the url via a webbrowser and see if it is showing a forbidden message as well?

Upvotes: 1

Related Questions