Witam, próbuję napisać aplikację która w pierwotnym założeniu ma się zalogować na stronie. Kod napisałem, sporo szukałem ale problem niestety cały czas występuje. Wysyłam żądanie typu GET, wyodrębniam parametry które później zwracam do serwera, uzupełniam formularz i odsyłam, niestety odpowiedź jest cały czas taka sama, czyli strona logowania z błędem w kodzie który brzmi tak:
Nie wiem jak to ugryźć bo według mnie wszystko jest dobrze. Być może problem powodowany jest ciasteczkami które odsyłam ale nie wiem sam.
Proszę o pomoc co może powodować, że nie mogę się zalogować.</p>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package welplanviewer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author pawel
*/
public class HTTPURLConnectionUsos
{
private static final String GET_URL = "https://logowanie.wat.edu.pl/cas/login?service=https://usos.wat.edu.pl/kontroler.php?_action=logowaniecas/index&locale=pl";
private static final String POST_URL = "https://logowanie.wat.edu.pl/cas/login?service=https://usos.wat.edu.pl/kontroler.php?_action=logowaniecas/index&locale=pl";
private static String POST_PARAMS = "[email protected]&password=123";
private static ArrayList<String> parametrs = new ArrayList<>();
private static String lt, execution, _eventId;
private static String regex = "type=\"hidden\" name=\".*\"";
private static String [] twoCookiesNameAndValue = new String[4];
private static String ciastka;
public static void main(String[] args) throws IOException
{
sendGET();
System.out.println("GET DONE");
sendPOST();
System.out.println("POST DONE");
}
private static void sendGET() throws IOException
{
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Host", "logowanie.wat.edu.pl");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Connection","keep-alive");
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK)
{
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
Pattern pattern = Pattern.compile(regex);
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
Matcher matcher = pattern.matcher(inputLine);
if (matcher.find())
{
parametrs.add(inputLine);
}
}
ciastka = con.getHeaderField("Set-Cookie");
in.close();
// print result
System.out.println(response.toString());
lt = parametrs.get(0).replaceAll("<input type=\"hidden\" name=\"lt\" value=\"", "").replaceAll("\" />", "").trim();
execution = parametrs.get(1).replaceAll("<input type=\"hidden\" name=\"execution\" value=\"", "").replaceFirst("\" />", "").trim();
_eventId = parametrs.get(2).replaceAll(" <input type=\"hidden\" name=\"_eventId\" value=\"", "").replaceAll("\" />", "").trim();
POST_PARAMS +="&" + "It=" + lt + "&" + "execution=" + execution + "&" + "_eventId=" + _eventId + "&" + "submit=" + "ZALOGUJ";
} else
{
System.out.println("GET request not worked");
}
parametrs.clear();
}
private static void sendPOST() throws IOException
{
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
StringTokenizer tok = new StringTokenizer(ciastka, ";");
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Host", "usos.wat.edu.pl");
con.setRequestProperty("Refer", "https://logowanie.wat.edu.pl/info/pl/services.html");
con.setRequestProperty("Connection", "Keep-Alive");
while (tok.hasMoreTokens())
{
con.addRequestProperty("Cookie", tok.nextToken());
}
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK)
{
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else
{
System.out.println("POST request not worked");
}
}
}
Zdjęcia ciastek przed zalogowaniem:
<image>/home/pawel/Obrazy/Katalog bez nazwy/ciast.png</image>
<image>/home/pawel/Obrazy/Katalog bez nazwy/ciasteczka.png</image>
Zdjęcia ciasteczek i parametrów po zalogowaniu:
<image>/home/pawel/Obrazy/Katalog bez nazwy/ciastkapoazlogowaniu2.png</image>
<image>/home/pawel/Obrazy/Katalog bez nazwy/parametry2.png</image>
Postać ciastek które wysyłam, oczywiście bez tych ";".
JSESSIONID=E18A92892075FC017FE084B63B2CFCDE; Path=/cas/; Secure; HttpOnly
Proszę o pomoc.