3
I’m performing an HTTPS get request for the following address, my initial intention is to receive the html data from the page. I followed the tutorial of Mkyong, but I get the answer code 302, I don’t know what I’m doing wrong, I added the headers necessary, but still I’m not getting a positive result. Follows 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;
}
}
Exit:
Sending 'GET' request to URL : https://portal.ufac.br/aluno/login/
Response Code : 302
Resposta:
‹
What am I doing wrong? How can I properly perform this request?
@re22 my initial idea is to recover the html code, then perform the authentication on this site, but I can’t even recover the code.
– Reiko Dev
Print the answer headers. Must have a Location header with the request address (additionally I would boot most of the request headers).
– Anthony Accioly