Perhaps Location
header is returned as a part of redirect response. If so, URLConnection
handles redirect automatically by issuing the second request to the pointed resource, so you need to disable it:
((HttpURLConnection) conn).setInstanceFollowRedirects(false);
EDIT:
If you actually need a URL of the redirect target and don't want to disable redirect handling, you may call getURL()
instead (after connection is established).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…