These are the response headers I get when I try to fetch the headers
But when I try to get all headers through my code it does not show the location header.
Here is my code
JavaScript
x
try {
URL obj = new URL("https://app.armorcode.com/oauth2/authorization/okta-apptesting?email=manan.girdhar@armorcode.io");
URLConnection conn = obj.openConnection();
Map<String, List<String>> map = conn.getHeaderFields();
System.out.println("Printing Response Header...n");
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " ,Value : " + entry.getValue());
}
} catch (IOException e) {
e.printStackTrace();
}
Can someone please tell me why the location header is missing
Advertisement
Answer
First of all try to get content, which you get from URLConnection
, using (for example) following code:
JavaScript
String content = IOUtils.toString(conn.getInputStream(), StandardCharsets.UTF_8);
I think you get content for third GET on your screen (with status 200).
You must prevent automatic redirects in HTTP Connection.
Try this:
JavaScript
public static void main(String[] args) throws Exception {
final URL obj = new URL("https://app.armorcode.com/oauth2/authorization/okta-apptesting?email=manan.girdhar@armorcode.io");
final URLConnection conn = obj.openConnection();
// prevent automatic redirects
final HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setInstanceFollowRedirects(false);
conn.connect();
// content your for debug
String content = IOUtils.toString(conn.getInputStream(), StandardCharsets.UTF_8);
final Map<String, List<String>> map = conn.getHeaderFields();
System.out.println("Printing Response Header...n");
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " ,Value : " + entry.getValue());
}
}