2

I have problem when trying to login to Moody's website using Jsoup. I had no problems with other sites, but the way I login in other cases doesn't work for Moodys.

This is my code:

Response initialResponse = Jsoup.connect("https://www.moodys.com/login.aspx")
                                .execute();

Response loginResponse = Jsoup.connect("https://www.moodys.com/login.aspx")
                    .cookies(initialResponse.cookies())
                    .data("MdcUserName", "username")
                    .data("MdcPassword", "password")
                    .method(Method.POST).execute();

doc = Jsoup.connect("any other moody's page")
                    .cookies(loginResponse.cookies())
                    .timeout(3000000).get();

System.out.printl(doc.html());

But this does not work. What am I doing wrong? Thank you.

  • How is it not working? – Reimeus Jun 02 '14 at 13:11
  • I get back login page instead of the page I'm asking for (for example with some search results). – Alexey 3699106 Jun 02 '14 at 13:14
  • @user432 should I surf through all html code and also input all hidden inputs (for example for any '' I should add '.data("name", "value")' to my connect()? If so, I tried to do smth like this. But for some hidden fields there is no "name" field. Should I ignore them? Or should I somehow input their id's? – Alexey 3699106 Jun 02 '14 at 13:40
  • @Alexey3699106 Nevermind, just took a look at their site. They send the POST only with `username`, `password` and a field `IsRememberMe`. So the final post looks something like that: `UserName=&Password=&IsRememberMe=false`. The POST is sent to `https://www.moodys.com/identity/login` instead of `https://www.moodys.com/login.aspx`, so try changing that. – user432 Jun 02 '14 at 13:45

1 Answers1

0

Basically, your code was failing in the urls and post parameters.

Here is a working example:

public static void main(String[] args) {
        try
        {
            Response initialResponse;

            initialResponse = Jsoup.connect("https://www.moodys.com/")
                    .execute();

            Response loginResponse = Jsoup
                    .connect("https://www.moodys.com/identity/login")
                    .cookies(initialResponse.cookies())
                    .data("UserName", "--")
                    .data("Password", "--")
                    .data("IsRememberMe", "false")
                                    .method(Method.POST)
                    .execute();
                    //example of internal moody's page.
            Document doc = Jsoup
                    .connect(
                            "https://www.moodys.com/newsandevents/events_/-/-/2/0/elq")
                    .cookies(loginResponse.cookies()).timeout(3000000).get();

                    //simple div selection example
            System.out
                    .println(doc
                            .select("#ctl00_ctl00_MdcWebPartManager_EventsListWebPart_ctl00_ctl00_NoRecordTipsLabel"));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Output:

<span id="ctl00_ctl00_MdcWebPartManager_EventsListWebPart_ctl00_ctl00_NoRecordTipsLabel">You have not registered for any upcoming event(s).</span>
rpax
  • 4,468
  • 7
  • 33
  • 57
  • thanx a lot for answer. But it doesn't work for me when I'm trying some other page that requires registration (for example https://www.moodys.com/page/search.aspx?cy=global&kw=shell&searchfrom=GS&spk=qs&tb=1). And https://www.moodys.com/newsandevents/events_/-/-/2/0/elq works for both logged in and logged out users. – Alexey 3699106 Jun 03 '14 at 05:42
  • And can you please tell me, why are you using .data("UserName", "--") and .data("Password", "--") instead of .data("MdcUserName", "--") and .data("MdcPassword", "--")? I tried to change that, but I've got this exception: org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/json; charset=utf-8, URL=https://www.moodys.com/identity/login at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178) at Test.main(Test.java:35) – Alexey 3699106 Jun 03 '14 at 05:44
  • Can you provide the page you want to load? Im not familiar with moodys – rpax Jun 03 '14 at 06:37
  • Yes, sure. For example, this page `https://www.moodys.com/page/search.aspx?cy=global&kw=shell&searchfrom=GS&spk=qs&tb=1`(or any other search page, searching works only for logged in users). I would be very grateful if you could help me. I've lost already a huge amount of time trying to solve this problem. – Alexey 3699106 Jun 03 '14 at 07:16