需要帮助登录网站和检索信息 [英] Need help logging into website and retrieving information

查看:99
本文介绍了需要帮助登录网站和检索信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多类似的问题,但是我仍然坚持登录我的学校成绩簿(

I have read through many similar questions, but I am still stuck with logging in to my school gradebook (https://parents.mtsd.k12.nj.us/genesis/parents) to retrieve data. My networking class is shown below:

public class WebLogin {

    public String login(String username, String password, String url) throws IOException {
        URL address = new URL(url);

        HttpURLConnection connection = (HttpURLConnection) address.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("j_username", username);
        connection.setRequestProperty("j_password", password);
        connection.setRequestProperty("__submit1__","Login");

        InputStream response = connection.getInputStream();
        Document document = Jsoup.parse(response, null, "");

        //don't know what to do here!

        return null;
    }
 }

我不确定该如何处理InputStream或我是否要正确登录,因此我再次阅读并阅读了一些在线资源以尝试理解此概念,但我仍然感到困惑.任何帮助表示赞赏!

I am not sure what to do with the InputStream or if I am going about logging in correctly, and once again, I have gone through and read several online resources to try to understand this concept but I am still confused. Any help is appreciated!

更新:因此,现在我了解了基本过程,并且知道该怎么做.使用Jsoup处理连接时,我知道您可以执行以下操作:

UPDATE: So now I understand the basic process and I know what I have to do. When using Jsoup to handle the connection, I know you can do something like this:

    Connection.Response res = Jsoup.connect("---website---")
                .data("j_username", username, "j_password", password)
                .followRedirects(true)
                .method(Method.POST)
                .execute();

但是,对于如何实际发送数据(例如,使用HttpURLConnection将用户的用户名/密码发送到网站以及如何实际存储获取的cookie),我仍然有些困惑,否则,帮助已经真的很有用,我对其他一切都很好

However, I am still a little confused as to how to actually send the data (such as a user's username/password to the website with HttpURLConnection, and how to actually store the obtained cookie...otherwise, the help has been really useful and I am fine with everything else

推荐答案

示例操作:

(编辑部分中原点的说明)

(explanation on the origin in edit part)

  1. 使用用户信用[使用HttpUrlConnection]连接到网站(登录页面)
  2. 抓饼干
  3. 使用用户数据重新连接到页面
  4. [使用JSoup]解析数据
  5. 现在

(使用带有http url连接的值/密钥对):

(with use of value/key pair with http url connection):

1-2.假设我们获得了一些网址地址(登录页面" http://blabla/login.php )&我们想要登录,然后我们做什么:

1-2 . assuming we got some URL adress (login page "http://blabla/login.php") & we wanna login so what we do:

// create  & open connection 
HttpUrlConnection connection = (HttpURLConnection) new URL(adress).openConnection();

// set do output ...
connection.setDoOutput(true);

/** variable charset for encoding */
String CHARSET = "UTF-8";

// Construct the POST value/key pair  data.
String data = "login=" + URLEncoder.encode(login, CHARSET)      
        + "&password=" + URLEncoder.encode(password, CHARSET) 
        + "&remember_me=on";

byte[] dataBytes = data.getBytes(CHARSET);

// create output stram to write our creditentials 
OutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());

// write value/key data to output stream
outputStream.write(dataBytes);
outputStream.flush();

// connect to url 
connection.connect();

// now we are connected and we can do other stuff get input strem header response code etc....
int responseCode = connection.getResponseCode();

 /**
 *     here we grab cookies (how?  - in other story)
 */

connection.disconnect();

3.然后我们获得了包含用户数据的第二页( http://blabla/userdata.php ) (*如果我们尚未准备好进行重定向,**我们还可以重用连接或作为上一步的下一步进行请求)

3 . THEN WE GOT SECOND PAGE WITH USER DATA ( http://blabla/userdata.php ) (*if we allready were not redirected, ** we also can reuse connection or do request as next step to above)

//we are creating & oppening another connection to new adres as at beginning 
HttpUrlConnection connection = (HttpURLConnection) new URL(adressToUserData).openConnection();

// but we do not construct user value/kay data & don't create output stream 
// we just add obtained cookies as request property 
connection.addRequestProperty("Cookie", _cookie); 

//connecting
connection.connect();

//getting input stram 
InputStrem is = connection.getInputStream();

//parse data for example with jsoup 
Document doc = JSoup.parse(is,null"");

//show parsed result example in grid view 

  1. 要解析数据,您需要在登录后了解网页的结构(具有用户等级的部分) 检查页面,您可以使用内置于chrome浏览器中的简单工具(用鼠标右键单击并检查元素)
  1. to parse data you need to know the structure of web page after login (the part with user grades) to examine a page u can use simple tool built in chrome web browser (right click with mouse & examine element)

然后您从第3点和第9点获得了页面"作为文档你选择你需要的东西

then u got the "page" as document from point 3 & u select what u need

Elements tableWithGrades = doc.select("table>grades");

您无法从HTML中选择单个元素作为表格行(tr),单元格(td),范围,按ID或类划分的div,名称等-您喜欢的方式.您只需要学习JSoup的语法" &了解html的简单"知识:)

u cant select single element as table row(tr), cell(td), span, div by id or class, name etc from HTML - what u like to. You just need to learn 'syntax' of JSoup & got 'simple' knowledge of html:)

我不确定该如何处理InputStream或我是否要正确登录,因此我再次阅读并阅读了一些在线资源以尝试理解此概念,但我仍然感到困惑."

要清楚:

  1. 您需要明确自己的目标(目标)-您要实现的目标
  2. 然后方法方法.
  1. you need to made yourself clear objective(goals) - what you want to achieve
  2. then the way how.

您要(-目标-):

  • 从学校服务器中显示可用于登录用户的数据

所以您需要 ACT&像Web浏览器一样思考":(-way-):

so u need to ACT & "THINK" like a WEB BROWSER:(--way--):

  1. 使用用户凭据(登录)进行请求
  2. 已登录用户的抓取数据
  3. 解析数据(从数据中获取您想要的内容)
  4. 显示结果

您现在从我之前的答案中知道如何做1,2

u know now how to do 1,2 from my previous answers

您现在需要思考如何3,4-有很多方法-通往某个地方的道路很多:P您会选择走哪条路-但仍然您需要了解那些路:)

u need now think how-to 3,4 - there are many ways - many roads to one place :P it's your choice which one will u take - but still you need to be aware of those roads :)

那么收集的数据的哪一部分&您想以哪种方式呈现它?

so what part of gathered data & in what kind of way u want to present it ?

但是,关于如何实际发送数据(例如,使用HttpURLConnection将用户的用户名/密码发送到网站的信息,我仍然有些困惑"

  • 通过实施登录表单(活动),该表单将收集用户数据
  • 提供任何其他来源的数据(根据需要)
    • by implementing a login form (activity) which will gather user data
    • providing data from any other source (as u desired)
    • 在您致电之前:

      // change post string "?login=xxxx&password=zzz" to byte array *
      byte[] dataBytes = data.getBytes(CHARSET);
      

      与以下内容绑定:

      //write value/key data to output stream
      outputStream.write(dataBytes);
      outputStream.flush();
      

      或在您致电之前,如yr jsoup示例:

      or before u call as in yr jsoup example:

      res.execute();
      

      u需要设置 String login ="..."; ,passwod ="...."为什么????

      u need to set String login="...";, passwod="...." why???

      ,因为您的代码被称为顺序代码(不包括并行部分)& java使用引用

      because your code is called sequential(excluding parareel parts) & java use references

      以及如何实际存储获得的cookie ..."

      • 使用永久存储,例如:共享首选项,文件,数据库
      • 或用于会话(每个应用程序'pid = vm'生命周期)单个实例(示例应用程序类或任何其他单例)或某些 helper类/变量在您的会话期间被GC消耗
        • use persisten storage like: shared preferences, file, database
        • or for session(per app 'pid=vm' lifecycle) singleton instance(example Application class or any other singleton) or some helper class / variable which will not be consumed by GC during your session
        • ",并且写入outputStream的行为与我的Jsoup示例所做的事情相同吗?"

          在您的陈述中,jsoup使用了众所周知的 builder模式,它与我在第1-4点中编写的代码相同,但是它类似于:"关于如何执行此操作的详细信息对我来说无关紧要" ,因为您可以用斧头或铁锹挖一个洞,您会得到预期的结果.

          in your statment jsoup uses so known builder pattern which makes the same as code i wrote in point 1-4 but it's something like: "the details on how you do it are not irrelevant to me as far it is working" as you can dig a hole with an ax or a shovel - you get the expected result.

          当您发布登录信息时,为什么将其作为编码参数?"

          想象您的密码看起来像这样:$$$ -_ xxx.php?xxaasfs ??dfsdfśś%% ___////**"*-尝试使用该种类的URL进行浏览器请求 http://server.com/home.php&action=login& username = xxx& password = $$$ s-_xxx.php?xxaasfs ??dfsdfśś:)))

          imagine your password looks like this $$$s-_xxx.php?xxaasfs??dfsdfśś%%___////**"* - try to do browser request with that kinnd of url http://server.com/home.php&action=login&username=xxx&password=$$$s-_xxx.php?xxaasfs??dfsdfśś :)))

          URLEncoder.encode(password, CHARSET) 
          
          /**
          * This class is used to encode a string using the format required by
          * {@code application/x-www-form-urlencoded} MIME content type.
          *
          * <p>All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9')
          * and characters '.', '-', '*', '_' are converted into their hexadecimal value
          * prepended by '%'. For example: '#' -> %23. In addition, spaces are
          * substituted by '+'.
          */
          

          其他一些提示:

          • 当您打开流时,您应在完成后关闭它
          • 关闭流(并非总是)最好的方法"是使用try/catch的finally块
          • 因为您一直使用stram检查,如果它不为null,则会节省您的时间
          • 如果您使用jsoup检查 elements.size()> 0 element!= null ,然后执行任何涉及动作的字符串/元素
          • when u open a stream you should close it when u're done
          • to close stream 'best way' (not always) is to use finally block of try/catch
          • befor you poceed with stram check if it is not null it will save your time
          • if u use jsoup check for elements.size()>0 or element!=null before u perform any strings/element involving actions

          ps.对于您的情况,您还应该查看

          ps. for youre case you should look also at

          在JavaServer Faces Web应用程序中使用基于表单的登录

          我希望这会给您概述其中一种可能的路径.对不起错字,我一点都不懂英语; p完全没有

          I hope that this will give you an overview of one of the possible paths & sorry for typos i dont know english language ;p at all

          这篇关于需要帮助登录网站和检索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆