简体   繁体   English

在java中使用Https连接登录并获取记录的会话ID

[英]Login and getting session id of the logged using Https connection in java

I am trying to login to a website using the https requests and then capture the session id.我正在尝试使用 https 请求登录网站,然后捕获会话 ID。 I need this session id inorder to delete some tags in the web application.我需要此会话 ID 才能删除 Web 应用程序中的某些标签。

Till now I am able to login to the application.到目前为止,我可以登录到该应用程序。 After being logged in the application directs me to the home page.登录后,应用程序会将我定向到主页。 This is where I tried to capture the session id from the cookies using https get request to the home page.这是我尝试使用 https get 请求从 cookie 中捕获会话 ID 到主页的地方。 But unfortunately the cookie doesnot contain session id.但不幸的是,cookie 不包含会话 ID。

When I send get request to home page after being logged in I get a cookie as : headers fields are :{null=[HTTP/1.1 200 OK], Server=[Microsoft-IIS/7.0], Pragma=[no-cache], Date=[Wed, 23 Dec 2015 04:37:13 GMT], Serv=[1], Cache-Control=[no-cache, no-store], SI=[1], X-AspNet-Version=[4.0.30319], Set-Cookie=[], Expires=[-1], Content-Length=[255882], X-Powered-By=[ASP.NET], Content-Type=[text/html;当我登录后向主页发送 get 请求时,我得到一个 cookie 为:标头字段为:{null=[HTTP/1.1 200 OK], Server=[Microsoft-IIS/7.0], Pragma=[no-cache] , Date=[Wed, 23 Dec 2015 04:37:13 GMT], Serv=[1], Cache-Control=[no-cache, no-store], SI=[1], X-AspNet-Version=[ 4.0.30319], Set-Cookie=[], Expires=[-1], Content-Length=[255882], X-Powered-By=[ASP.NET], Content-Type=[text/html; charset=utf-8]}字符集=utf-8]}

Here the Set-Cookie field is empty.这里 Set-Cookie 字段为空。

I have written the following lines of code.我已经编写了以下代码行。

package com.iso.mozart.test.ui;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HttpUrlConnectionExample {

  private List<String> cookies;
  private HttpsURLConnection conn;

  private final String USER_AGENT = "Mozilla/5.0";

  public static void main(String[] args) throws Exception {

    String url = "https://www.testpurpose.com/Login.aspx";
    String homepage = "https://www.testpurpose.com/Home.aspx";

    HttpUrlConnectionExample http = new HttpUrlConnectionExample();

    // make sure cookies is turn on
    CookieHandler.setDefault(new CookieManager());

    // 1. Send a "GET" request, so that you can extract the form's data.
    String page = http.GetPageContent(url);
    String postParams = http.getFormParams(page, "username here", "password here");

    // 2. Construct above post's content and then send a POST request for
    // authentication
    http.sendPost(url, postParams);

    // 3. success then go to homepage.

    String result = http.GetPageContent(homepage);
    System.out.println(result);
  }   

private void sendPost(String url, String postParams) throws Exception {

    URL obj = new URL(url);
    conn = (HttpsURLConnection) obj.openConnection();

    // Acts like a browser
    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Host", "www.testpurpose.com");
    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    for (String cookie : this.cookies) {
        conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
    }
    conn.setRequestProperty("Connection", "keep-alive");
    conn.setRequestProperty("Referer", url);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));

    conn.setDoOutput(true);
    conn.setDoInput(true);

    // Send post request
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(postParams);
    wr.flush();
    wr.close();

    int responseCode = conn.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + postParams);
    System.out.println("Response Code : " + responseCode);
    //  System.out.println("HEADERS:"+conn.getRequestMethod());

    BufferedReader in = 
             new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    // System.out.println(response.toString());

  }

  private String GetPageContent(String url) throws Exception {

    URL obj = new URL(url);
    conn = (HttpsURLConnection) obj.openConnection();

    // default is GET
    conn.setRequestMethod("GET");

    conn.setUseCaches(false);

    // act like a browser
    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    conn.getRequestProperties();
    conn.setDoOutput(true);
    //  System.out.println("Huhahahahah :"+conn.getHeaderFields());
    if (cookies != null) {
        for (String cookie : this.cookies) {
            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
        }
    }
    int responseCode = conn.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
     System.out.println("headers fields are :"+conn.getHeaderFields());
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    // Get the response cookies
    setCookies(conn.getHeaderFields().get("Set-Cookie"));      //This is where I have tried to capture the session id from cookie but could it doesnot contain session id.
    return response.toString();


  }

  public String getFormParams(String html, String username, String password)
        throws UnsupportedEncodingException {

    System.out.println("Extracting form's data...");

    Document doc = Jsoup.parse(html);

    // Google form id
    Element loginform = doc.getElementById("form1");
    Elements inputElements = loginform.getElementsByTag("input");
    List<String> paramList = new ArrayList<String>();
    for (Element inputElement : inputElements) {
        String key = inputElement.attr("name");
        String value = inputElement.attr("value");

        if (key.equals("txtUserID"))
            value = username;
        else if (key.equals("txtPassword"))
            value = password;
        paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
    }

    // build parameters list
    StringBuilder result = new StringBuilder();
    for (String param : paramList) {
        if (result.length() == 0) {
            result.append(param);
        } else {
            result.append("&" + param);
        }
    }
    return result.toString();
  }

  public List<String> getCookies() {
    return cookies;
  }

  public void setCookies(List<String> cookies) {
    this.cookies = cookies;
  }

}
CookieHandler.setDefault(new CookieManager());

Change above line in your code with this line.使用此行更改代码中的上一行。

CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

Let me know if that works for you.让我知道这是否适合您。

HttpSession session = request.getSession();
String sessionid = session.getId();

use this small block of code.使用这个小代码块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM