有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Jsoup登录穆迪

我在尝试使用Jsoup登录穆迪网站时遇到问题。我对其他网站没有问题,但我在其他情况下的登录方式对Moodys不起作用

这是我的代码:

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());

但这是行不通的。我做错了什么?多谢各位


共 (1) 个答案

  1. # 1 楼答案

    基本上,您的代码在URL和post参数中失败

    以下是一个工作示例:

    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();
            }
        }
    

    输出:

    <span id="ctl00_ctl00_MdcWebPartManager_EventsListWebPart_ctl00_ctl00_NoRecordTipsLabel">You have not registered for any upcoming event(s).</span>