java - When does URL redirect happen? -
I have a URL that redirects to another, which redirects to another, and I want to get an end domain I am trying. I thought that there is a redirection when connecting, but running this code tells me otherwise:
public static zero main (string [] args {system.out.println (resolveEndDomain ( "Http: //goo.gl/ELHEjl")); } Private Static String Prospect Endendeman (String Deep Link) {HTTPTo Connection Connection httpNo; Try {httpConnection = (HttpURLConnection) New URL (deep link). OpenConnection (); //httpConnection.getResponseCode (); } Hold (exception e) {e.printStackTrace (); } Return httpConnection.getURL (). GetHost (); }
So, if I run it, it will give me just goo.gl
. But if I dismiss the line to get the response code, then it runs and gets it from the end domain, and prints www.thomann.de
. Even if it does not help using getHeaderFields ()
, getContent ()
or similar, but using connect
I am not interested in those responses, so how can I do it and solve the redirection? When does the connection occur effectively?
For your question, when the server responds with a note addressed with a 301 or 302 HTTP status code That status codes are part of the answer to the server, so you should actually be requested with that resource (usually with connect
).
As you have already received, HttpURLConnection
usually handles the part of the check for redirection status code and you will be redirected to the URL provided as part of the redirect response. Redirects (up to maximum redirection, I'm not sure where this limit is defined).
You can set the following redirector
to false
and automatically interpret the HTTP header if you are being redirected or not , But unless you receive the status code (or 4XX
- not found, denied - or 5XX
- server error-), you will not be sure that the URL
For example, you can join http://goo.gl/ELHEjl
and find that it redirects you to Confirms http://www.bit.ly/s34314313
. But until then, unless you link to that URL and get a 3xx status code, you can not ensure that is connected to http://www.bit.ly/s34314313
Having the last resource location or just redirects you again.
Comments
Post a Comment