使用Jsoup登录网站并留在网站上

时间:2016-01-25 19:40:26

标签: java android http post jsoup

我尝试在此处登录:http://www.gszi.sulinet.hu/dinaweb/diakok/belepes.jsp 有了这个:

string vUrl = "https://domain.com/groups/members/1234";
if (pageNumber > 1) vUrl += "?page=" + (pageNumber).ToString();
HttpWebRequest groupsRequest = (HttpWebRequest)WebRequest.Create(vUrl);
groupsRequest.CookieContainer = new CookieContainer();
groupsRequest.CookieContainer.Add(cookies); //recover cookies First request
groupsRequest.Method = WebRequestMethods.Http.Get;
groupsRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64)     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36";
groupsRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
groupsRequest.UseDefaultCredentials = true;
groupsRequest.AutomaticDecompression = DecompressionMethods.GZip;
groupsRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8");
groupsRequest.Headers.Add("Cache-Control", "max-age=0");

HttpWebResponse getResponse = (HttpWebResponse)groupsRequest.GetResponse();

之后,获取我必须登录的页面的html,我意识到我无法登录。有没有办法登录然后获取我现在可以访问的页面的html?任何链接,建议或帮助表示赞赏。

2 个答案:

答案 0 :(得分:5)

您对两个不同的输入标签使用相同的密钥。此外,您使用的密钥是错误的。

.data("jelszo","SOMETEXT")
.data("felnev","PASSWORD")

<强>更新

Connection.Response initial = Jsoup
        .connect("http://www.gszi.sulinet.hu/dinaweb/diakok/belepes.jsp")
        .method(Connection.Method.GET).execute();

Connection.Response login = Jsoup
        .connect("http://www.gszi.sulinet.hu/dinaweb/diakok/belepes.jsp")
        .data("jelszo","SOMETEXT")
        .data("felnev","PASSWORD")
        .cookies(initial.cookies())
        .method(Method.POST)
        .execute();

Document page = Jsoup
        .connect("ANY_PAGE_INSIDE_THE_SITE")
        .cookies(login.cookies()) //use this with any page you parse. it will log you in
        .get();

答案 1 :(得分:1)

好吧,实际上我发现我还需要一个参数(&#34; akcio&#34;)并且我将post()方法发送到错误的URL,现在我的程序正常运行,我的代码:

Connection.Response loginForm = Jsoup.connect(loginFormUrl)
         .method(Connection.Method.GET)
         .execute();

    Map<String, String> loginCookies = loginForm.cookies();

    Document document = Jsoup
        .connect(loginFormUrl)
        .data("akcio", akcio)
        .data("felnev",felnev)
        .data("jelszo",jelszo)
        .cookies(loginCookies)
        .post();

    Document document2 = Jsoup.connect(loggedInUrl)
    .cookies(loginCookies)
    .get();
    System.out.println(document2);