Войдите в систему и получение идентификатора сеанса зарегистрированного с помощью HTTPS Connection в JavaJAVA

Программисты JAVA общаются здесь
Anonymous
Войдите в систему и получение идентификатора сеанса зарегистрированного с помощью HTTPS Connection в Java

Сообщение Anonymous »

Я пытаюсь войти на веб -сайт, используя запросы HTTPS, а затем захватить идентификатор сеанса. Мне нужен этот идентификатор сеанса, чтобы удалить некоторые теги в веб -приложении. < /P>

До сих пор я могу войти в приложение. После регистрации в приложении меня направляет на домашнюю страницу. Здесь я пытался захватить идентификатор сеанса из файлов cookie с помощью HTTPS GET запрос на домашнюю страницу. Но, к сожалению, cookie не содержит идентификатор сеанса. < /p>

Когда я отправляю запрос получить запрос на домашнюю страницу после того, как его зарегистрировали I Получить cookie as:
Поля заголовков: {null = [http /1.1 200 ok], Server = [Microsoft-iis /7.0], pragma = [no-cache], Date = [wed, wed, 23 Dec 2015 04:37 :13 gm = gm = gmt] [1, 23 декабря 2015 г. 04:37 Cache-Control = [No-Cache, No Store], Si = [1], x-aspnet-version = [4.0.30319], set-cookie = [], истекает = [-1], контент-длина = [255882], X-Power-by = [asp.net], контент-тип = [Text/html; charset = utf-8]} < /p>

Здесь поле сет-cookie пусто.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 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 paramList = new ArrayList();
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 getCookies() {
return cookies;
}

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

}


Подробнее здесь: https://stackoverflow.com/questions/344 ... on-in-java

Вернуться в «JAVA»