I am performing an HTTPS get request for the next address , my initial intention is to receive the html data from the page. I followed the Mkyong tutorial, but I get the answer 302, I do not know what I'm doing wrong, I've added the necessary headers, but I'm still not getting a positive result. Here is the code:
public class HttpUrlConnectionUFAC {
private List<String> cookies;
private HttpsURLConnection conn;
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) {
String url = "https://portal.ufac.br/aluno/login/";
HttpUrlConnectionUFAC http = new HttpUrlConnectionUFAC();
// 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 = null;
try {
page = http.getPageContent(url);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Resposta:\n"+page);
}
private String getPageContent(String url) throws Exception {
URL obj = new URL(url);
conn = (HttpsURLConnection) obj.openConnection();
// default is GET
conn.setRequestMethod("GET");
// Acts like a browser
conn.setUseCaches(false);
conn.setRequestProperty("Remote Address",
"200.129.173.7:443");
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Accept-Encoding",
"gzip, deflate, sdch");
conn.setRequestProperty("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4");
conn.setRequestProperty("Cache-Control", "max-age=0");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Host", "portal.ufac.br");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Referer", "https://portal.ufac.br/aluno/login.action?error=");
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("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"));
return response.toString();
}
public List<String> getCookies() {
return cookies;
}
public void setCookies(List<String> cookies) {
this.cookies = cookies;
}
}
Output:
Sending 'GET' request to URL : https://portal.ufac.br/aluno/login/
Response Code : 302
Resposta:
‹
What am I doing wrong? How can I correctly perform this request?