发送数据需要看到从网站上隐藏的内容 [英] Sending data needed to see hidden content from website

查看:204
本文介绍了发送数据需要看到从网站上隐藏的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我试图让应用涉及到具体的网站。我需要访问到具有登录后可见的内容页面。如果我的理解,低于code表明,首先,我需要连接到URL1避免违约炫魅所以在这里我不能发送数据(登录,密码)。我需要看到URL3内容,但在这里我也不能发送数据,因为没有登录名和密码字段。它们在URL2。我尝试过其他解决方案,从这个网站,但我收到这是唯一为大家可见的内容。任何人可以帮助?

Recently I am trying to make app concerns a specific website. I need to have access to page which has visible content after logging. If I understand, below code shows that firstly I need to connect to url1 to avoid default mainpage so here I cannot send data (login, password). I need to see a content from url3, but here I cannot send data also because there is no login and password field. They are in url2. I tried other solutions from this website, but I receive only content that is visible for everyone. Can anybody help?

private class Parser extends AsyncTask<Void, Void, Void> {
    String h;
    String url1 = "http://www.klt.net.pl/";
    String url2 = "http://www.klt.net.pl/index.php?a=logowanie";
    String url3 = "http://www.klt.net.pl/index.php?a=przedmecz1&b=2&d=2038";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(MainActivity.this);
        pd.setTitle("Parser");
        pd.setMessage("Loading...");
        pd.setIndeterminate(false);
        pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Connection.Response response = Jsoup.connect(url1)
            .method(Connection.Method.GET)
            .timeout(50000)
            .followRedirects(true)
            .execute();
        Document document = Jsoup.connect(url2)
            .cookies(response.cookies())
            .get();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        pd.dismiss();
    }
}

编辑:

@Override
protected Void doInBackground(Void... params) {
    try {
        Connection.Response response = Jsoup.connect(url1)
        .method(Connection.Method.GET)
        .timeout(50000)
        .followRedirects(true)
        .execute();

        Connection.Response loginRes = Jsoup.connect(url2)
        .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36")
        .data("login", getlog2,
        "haslo", getpass2)
        .cookies(response.cookies()) 
        .method(Method.POST)
        .execute();

        Map<String, String> cookies = new Map<String, String>();
        cookies.addAll(loginRes.cookies());

        Connection.Response otherRes = Jsoup.connect(url3)
        .cookies(cookies)
        .method(Method.POST)
        .execute();

        d3 = Jsoup.connect(url3)
        .cookies(otherRes.cookies())
        .get();

我的更新code。这可以吗?我有错误的地图(不能instatiate的类型,不能分辨型)。

My updated code. Is that ok? I have error in Map (cannot instatiate the type and cannot resolved type).

推荐答案

要登录,你需要知道什么数据发表(ID,密码,会话cookie等...),并在 URL地址您需要上传即可。

To login you need to know what data to POST (id, password, session cookie and etc...), and the url address you need to POST to.

此信息通常包含的所有登录表单中,我将解释这样做低于此所需的步骤:

This information is generally all contained in the login form, I'll explain the steps required to do this below:

第1步:在 ID 密码,您需要输入登录应该是一个形式的投入。所以,简单地右键单击您在 ID类型的区域,然后选择检查元素(假设你使用的是Chrome)。在那里,你将能够检查输入和窗体属性。

Step 1: The ID and password that you need to enter to login should be inputs of a form. So simply right-click the area where you type in your ID and select Inspect Element (assuming you are on Chrome). There you will be able to inspect the property of the inputs and the form.

第2步:密切调查的形式和保持所有输入域(包括隐藏字段)的记录。你需要知道所有字段的名称。您还需要知道,如果表单请求在 GET POST 行动取得表单值。

Step 2: Closely investigate the form and keep record of ALL INPUT FIELDS (including hidden fields). You need to know the name and value of all fields. You also need to know if the form request is made in GET or POST and the action value for the form.

第3步:现在让我们来最有趣的部分。使用以下code片段让你的请求到服务器并获取所需内容。

Step 3: Now let's get to the fun part. Use the following code snippet to make your request to the server and retrieve the desired content.

Connection.Response loginRes = Jsoup.connect(loginUrl)
                               .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
                               .data("login", yourID
                                     "haslo", yourPassword)
                               .cookies(response.cookies()) //this is the same cookie you used for url2!
                               .method(Method.POST)
                               .execute();


  • loginUrl 是请求的地址,而你的情况是http://www.klt.net.pl/index.php A = logowanie

  • 的userAgent 告诉服务器你的浏览器的详细信息。

  • 数据是你把你的名字和放大器;表格中的所有输入字段的值对。

  • 饼干是你把你的饼干,你需要检查,如果你的请求需要通过服务器来接受的Cookie,这可以在网络标签下选中饼干一节。在你的情况,它使用了相同的cookie为URL2。

  • 指定你的请求方法。

    • loginUrl is the request address, which in your case would be "http://www.klt.net.pl/index.php?a=logowanie".
    • userAgent tells the server your browser details.
    • data is where you put your name&value pairs of all input fields in the form.
    • cookies is where you put your cookies, you need to check if your request requires cookies to be accepted by the server, this can be checked in the network tab under "cookies" section. In your case, its the same cookie used for url2.
    • method specifies your request method.
    • 检索到的 loginRes 对象将包含所有你需要的信息,在HTML,饼干和一切。

      The retrieved loginRes object will contain all the info you need, the html, cookies and everything.

      您已经成功登录后,请确保您在Map对象cookie的值存储象下面这样:

      After you've successfully logged in, make sure you store the cookie value in a Map object like below:

      Map<String, String> cookies;
      cookies.putAll(loginRes.cookies());
      

      并确保通过这饼干在今后所有申请饼干参数,象下面这样:

      And then make sure to pass this cookies to the cookies parameter in all future requests, like below:

      Connection.Response otherRes = Jsoup.connect(otherUrl).cookies(cookies)....
      

      这将确保您的登录会话保持与服务器知道您的身份验证的用户。

      This will ensure that your login session is maintained and the server knows you are authenticated user.

      ----------------更新------------

      ----------------update------------

      从doInBackground任务的开头声明地图的cookie。然后存储的所有Cookie 您尽请求后。所以:

      Declare Map cookie from the beginning of the doInBackground task. Then store ALL COOKIES after you make every request. So:

      cookies = response.cookies();
      cookies.putAll(loginRes.cookies();
      cookies.putAll(otherRes.cookies();
      

      这篇关于发送数据需要看到从网站上隐藏的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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