SparkyNZ
SparkyNZ

Reputation: 6676

Get Filename or Location from HTTP Response when Downloading

I'm new to HTTP. I am trying to get a filename or location when downloading using this link:

http://amp.dascene.net/downmod.php?index=40871

When I run the link through cURL, I get the following:

curl --head http://amp.dascene.net/downmod.php?index=40871
HTTP/1.1 302 Found
Date: Wed, 17 Feb 2016 07:26:31 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze19
Location: http://amp.dascene.net/modules/L/Lizardking/XM.Actual%20Reality.gz
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
Connection: close

How do I get the above information in Android? I'm trying to do it like this:

  URL url = new URL( "http://amp.dascene.net/downmod.php?index=40871" );

  HttpURLConnection connInfo = (HttpURLConnection) url.openConnection();
  connInfo.setRequestMethod("GET");
  connInfo.setAllowUserInteraction(false);
  connInfo.setDoInput(true);
  connInfo.setDoOutput(true);
  connInfo.connect();

  Map<String, List<String>> sFields = connInfo.getHeaderFields();

  Set<Map.Entry<String, List<String>>> entrySet = sFields.entrySet();

  for( Map.Entry<String, List<String>> entry : entrySet )
  {
    String headerName = entry.getKey();
    System.out.println( "Header Name:" + headerName );
    List<String> headerValues = entry.getValue();

    for( String value : headerValues )
    {
      System.out.print( "Header value:" + value );
    }
    System.out.println();
  }

However, the Location field, or anything else that hints to the name of the file isn't in the output list. This is what I get:

02-17 20:28:48.812: I/System.out(7742): Header Name:null
02-17 20:28:48.812: I/System.out(7742): Header value:HTTP/1.1 200 OK
02-17 20:28:48.812: I/System.out(7742): Header Name:Accept-Ranges
02-17 20:28:48.812: I/System.out(7742): Header value:bytes
02-17 20:28:48.812: I/System.out(7742): Header Name:Age
02-17 20:28:48.812: I/System.out(7742): Header value:0
02-17 20:28:48.812: I/System.out(7742): Header Name:Connection
02-17 20:28:48.812: I/System.out(7742): Header value:Keep-Alive
02-17 20:28:48.812: I/System.out(7742): Header Name:Content-Length
02-17 20:28:48.812: I/System.out(7742): Header value:227805
02-17 20:28:48.812: I/System.out(7742): Header Name:Content-Type
02-17 20:28:48.812: I/System.out(7742): Header value:application/x-gzip
02-17 20:28:48.812: I/System.out(7742): Header Name:Date
02-17 20:28:48.812: I/System.out(7742): Header value:Wed, 17 Feb 2016 07:31:12 GMT
02-17 20:28:48.812: I/System.out(7742): Header Name:ETag
02-17 20:28:48.812: I/System.out(7742): Header value:"b80fd3-379dd-3d45429fe3940"
02-17 20:28:48.812: I/System.out(7742): Header Name:Last-Modified
02-17 20:28:48.812: I/System.out(7742): Header value:Fri, 27 Feb 2004 11:54:37 GMT
02-17 20:28:48.812: I/System.out(7742): Header Name:Server
02-17 20:28:48.812: I/System.out(7742): Header value:Apache/2.2.16 (Debian)
02-17 20:28:48.812: I/System.out(7742): Header Name:X-Android-Received-Millis
02-17 20:28:48.812: I/System.out(7742): Header value:1455694128815
02-17 20:28:48.812: I/System.out(7742): Header Name:X-Android-Response-Source
02-17 20:28:48.812: I/System.out(7742): Header value:NETWORK 200
02-17 20:28:48.812: I/System.out(7742): Header Name:X-Android-Selected-Transport
02-17 20:28:48.812: I/System.out(7742): Header value:http/1.1
02-17 20:28:48.812: I/System.out(7742): Header Name:X-Android-Sent-Millis
02-17 20:28:48.812: I/System.out(7742): Header value:1455694128376

How can I get the Location field that cURL gets?

Upvotes: 0

Views: 1029

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317497

An HTTP status code 302 is a redirect. HttpURLConnection will follow redirects automatically and transparently by default. You can turn this off with setFollowRedirects(false) if you wish to handle this yourself.

Upvotes: 1

Related Questions