0
I’m trying to get a status code[ redirect 301, Location : ] through a request. This request brings as parameter an id associated to a database stored url (in this case, a Map), with the id, I search the url and with the Httpurlconnection class I instantiated a connection. This is the problem: a new connection and not a redirect. So much so that the status I get is a status code 200.
p.s this is my first question here and I am not experienced at all, so it was bad if it is not well formulated.
the method I described is this :
@GetMapping("/geturl/{id}")
public String getUrl(@PathVariable("id") String id) throws IOException {
String url = "";
System.out.println("We are in GET url");
System.out.println("Id value " + id);
if (urlRepository.findById(id) != null) {
try {
url = urlRepository.findById(id).getUrl();
System.out.println("This is the URL ... " + url);
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
conn.setInstanceFollowRedirects(false);
conn.connect();
System.out.println("Request URL ... " + url);
boolean redirect = false;
// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
System.out.println("Response Code ... " + status);
System.out.println("Location : " + conn.getHeaderField("Location"));
if (redirect) {
// get redirect url from "location" header field
String newUrl = conn.getHeaderField("Location");
// get the cookie if need, for login
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
System.out.println("Redirect to URL : " + newUrl);
}
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
System.out.println("URL Content... \n" + html.toString());
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
return "404 Not Found";
}
Much of the code came from here : https://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/
any omission just signal.
the outputs of the system outs :
We are in GET url
Id value 08316aaa
This is the URL ... http://www.google.com
Request URL ... http://www.google.com
Response Code ... 200
Location : null
URL Content...
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="pt-BR"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="nN9LKT6NkpSrzvOI7rKOPA==">(function(){window.google={kEI:'6mNxXf7BC7rM5OUPmbaggAE',kEXPI:'0,1353746,2016,1641,781,1225,731,223,510,1065,3152,57,320,207,1017,54,1430,526,51,2,124,10,169,544,226,112,160,240,138,1132071,1197736,329531,1294,12383,4855,32692,15247,867,12163,7235,9286,363,3320,5505,2442,5942,1119,2,579,727,2431,1362,283,4040,4968,773,2249,4745,3118,6196,669,1050,1496,312,1478,7,2,489,2044,8909,5297,2016,38,918,875,1217,1364,350,1,1260,2736,3061,2,631,3240,4192,3874,2884,20,317,1118,482,420,2128,1,369,2777,520,399,992,509,776,8,109,2687,219,670,78,48,553,11,15,1278,2212,202,323,5,68,1177,7,83,244,513,324,193,1470,52,820,2620,335,449,34,108,152,52,1137,2,1509,554,606,1317,522,186,593,1182,520,361,1586,747,61,158,26,72,112,44,1102,328,1284,17,83,336,81,507,1919,1639,607,101,373,1339,729,501,557,1601,1492,134,521,252,181,79,1288,28,496,7,504,224,592,523,695,152,3,201,1227,321,52,736,369,269,221,980,2,378,284,243,310,116,958,349,677,220,77,1069,289,193,6,6,275,299,109,467,12,23,122,135,9,397,44,8,112,2,91,75,528,129,119,20,35,55,3,468,56,16,838,127,676,29,292,219,9,68,3,3,249,565,441,524,203,96,2,241,8,5896398,6000372,94,2799874,4,1572,549,333,444,1,2,80,1,900,583,9,304,1,8,1,2,2132,1,1,1,1,1,414,1,748,141,59,726,3,7,563,1,3559,2,115,27,11',authuser:0,kscs:'c9c918f0_6mNxXf7BC7rM5OUPmbaggAE',kGL:'BR',kBL:'nfid'};google.sn='webhp';google.kHL='pt-BR';google.jsfs='Ffpdje';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b};google.https=function(){return"https:"==window.location.protocol};google.ml=function(){return null};google.time=function(){return(new Date).getTime()};google.log=function(a,b,e,c,g){if(a=google.logUrl(a,b,e,c,g)){b=new Image;var d=google.lc,f=google.li;d[f]=b;b.onerror=b.onload=b.onabort=function(){delete d[f]};google.vel&&google.vel.lu&&google.vel.lu(a);b.src=a;google.li=f+1}};google.logUrl=function(a,b,e,c,g){var d="",f=google.ls||"";e||-1!=b.search("&ei=")||(d="&ei="+google.getEI(c),-1==b.search("&lei=")&&(c=google.getLEI(c))&&(d+="&lei="+c));c="";!e&&google.cshid&&-1==b.search("&cshid=")&&"slh"!=a&&(c="&cshid="+google.cshid);a=e||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+d+f+"&zx="+google.time()+c;/^http:/i.test(a)&&google.https()&&(google.ml(Error("a"),!1,{src:a,glmm:1}),a="");return a};}).call(this);(function(){google.y={};google.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1};google.lm=[];google.plm=function(a){google.lm.push.apply(google.lm,a)};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};}).call(this);google.f={};var a=window.location,b=a.href.indexOf("#");if(0<=b){var c=a.href.substring(b+1);/(^|&)q=/.test(c)&&-1==c.indexOf("#")&&a.replace("/search?"+c.replace(/(^|&)fp=[^&]*/g,"")+"&cad=h")};</script><style>#gb{font:13px/27px Arial,sans-serif;height:30px}#gbz,#gbg{position:absolute;white-space:nowrap;top:0;height:30px;z-index:1000}#gbz{left:0;padding-
....
Done
Bound.