0

I'm trying to login to a website using HttpPost. My goal is to login and get the necessary cookies that are provided by the website. So far I've been successful at "logging" in but the problem persists with redirection. After I login, I receive success message from the server but in reality, website redirects to a different url and there provides the cookies I need for the session. I've checked how the website "works" with chrome developer tools and I have the necessary parameters in order to do post.

My client at the moment isn't receiving the needed cookies, as it isn't following up with the redirect. I'd like to know what to do next basically. How can I follow up with the server redirects after logging in, in order to get the needed cookies? What classes/methods should I use? I read about htmlUnit but I'd like to avoid it, because the library/data size is huge and my application is only 0.5mb.

What I'm doing now (simplified):

Connect -> Login (httpPost) -> Success (200) -> I receive only one cookie (which isnt the one I need).

What I think I should be doing (simplified):

Connect -> Login -> Success (200) -> Follow up with the redirect -> Get the cookies (total of 5)

.

Thank you for your time and here's the code. The cookie that I'm using at the moment is the incorrect one.

DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = null;

            Log.d("LOGGING" , "Posting...");
            HttpPost httpost = new HttpPost(url);
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("user", "myUser"));
            nvps.add(new BasicNameValuePair("passwrd", "myPw"));
            nvps.add(new BasicNameValuePair("openid_identifier", ""));
            nvps.add(new BasicNameValuePair("cookielength", "-1"));
            nvps.add(new BasicNameValuePair("hash_passwrd", ""));

            try {
                httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {
                Log.d("LOGGING" , "Getting login response...");
                response = httpclient.execute(httpost);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            HttpEntity entity = response.getEntity();
            Log.d("LOGGING" , "Login form getStatus: " + response.getStatusLine());

            List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 
            Log.d("LOGGING" , "Cookie: " + cookies.toString());

            String SESSION_NAME = "PHPSESSID";
            String[] sessionValue = cookies.toString().split("value: ");
            String[] sessionValue1 = sessionValue[1].split("]");
            String realSessionValue = sessionValue1[0].toString();
            Log.d("LOGGING", "SESSION NAME:" + realSessionValue);

            /*
            Connection.Response loginForm = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    .referrer(referrer)
                    .execute();

            Document login = Jsoup.connect(url)
                    .data("user", "myUser")
                    .data("passwrd", "myPw")
                    .data("cookielength", "1440")
                    .data("openid_identifier", "")
                    .data("hash_password", "")
                    .referrer(referrer)
                    .cookie(SESSION_NAME, realSessionValue)
                    .post();
            */

            doc = Jsoup.connect(url)
                    .cookie(SESSION_NAME, realSessionValue)
                    .get();

            //String body = doc.select("body").html();
            //Log.d("LOGGING", "Body: " + body.toString());

            //Check login
            Elements bodies = doc.select("body").first().select("div#main-container-radio").first().select("div#radiorow").first().select("div.bghue").first().select("form#shoutbox-form-r");
            Log.d("LOGGING", "Login status: " + bodies.toString());
  • Is there any other way you could do this? Do you have any control over the site? Sorry, this isn't the most productive reply but it really seems like this is the hard way of doing things. – cbrulak Dec 18 '13 at 17:36
  • Thank you for your response. I do not have any control over the site and most likely wouldn't be able to receive any documentation. I'm trying to do an unofficial radio client of their stream for Android (which is working), and now I'm trying to add the functionality to use their shoutbox (necessary to login before commenting). – user2970008 Dec 18 '13 at 17:38
  • I think, with HttpClient, you have to set the default redirect strategy. There is an example in the post: http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect – CodeChimp Dec 18 '13 at 17:49
  • in that case you might be able to build a proxy that the user's pass their credentials to and the proxy does all the heavy lifting for login/etc – cbrulak Dec 18 '13 at 18:02
  • Thank you for your comments. I'll take a look at the suggestions. The problem with the redirect strategy is that I'm receiving 200 message from the server, but yet I'm not receiving the necessary cookies. When I login via their website, I'm not getting 200, but 3xx message. With Java approach, I always get 200. There's obviously something I'm not getting with the login. – user2970008 Dec 18 '13 at 18:24

1 Answers1

0

Thanks everyone. It was a problem in my own code, I was using the wrong url by mistake, as they have like 3 different ones for login. Otherwise it's running just fine.