0

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?

SparkyNZ
  • 6,266
  • 7
  • 39
  • 80
  • You nowhere told HttpUrlConnection to get only the headers as you instructed curl. – greenapps Feb 17 '16 at 10:24
  • @greenapps: Can you be more specific? – SparkyNZ Feb 18 '16 at 00:23
  • Yes. But i suggest you look just at your curl command. Then you should see what i mean. – greenapps Feb 18 '16 at 10:42
  • @greenapps: I thought --head meant return the headers.. which it is. Sorry I don't understand what you mean. – SparkyNZ Feb 19 '16 at 00:19
  • Correct. Return only the headers. Now i will repeat my question: Where did you instruct HttpUrlConnection to request the same? Maybe there is no redirect if you only ask for headers was the idea. – greenapps Feb 19 '16 at 07:54
  • @greenapps I guess you are saying there is a method rrequired to get the headers? I thought getHeaderFields would do that. Sorry I may be a little slow – SparkyNZ Feb 19 '16 at 08:46
  • No. I was asking you it you told HttpUrlConnection to request only the headers from the server and not the complete page. With curl you only requested the headers. The server will not send the page then. Now for the third time: where do you tell HttpUrlConnections to request only the headers like curl does? – greenapps Feb 19 '16 at 11:57
  • 1
    To make a long story to an end: I was suggesting that in order to request only the headers you could use connInfo.setRequestMethod("HEAD");. But when i tried the setdooutput statement had to be removed and Map sFields became null. So another thing where HttpUrlConnection fails. – greenapps Feb 19 '16 at 12:58

1 Answers1

1

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Excellent. That worked. I gues cURL must take the 302 response a step further, or is the 'follow redirects' a flag that is set in the HTTP GET request? – SparkyNZ Feb 17 '16 at 07:37
  • I found this link too which helped: http://stackoverflow.com/questions/3786161/cant-get-response-header-location-using-javas-urlconnection – SparkyNZ Feb 17 '16 at 07:38
  • The client is smart enough to see the 302 and understand, per HTTP protocol, that the actual destination should be the Location header value. Don't know about cURL's behavior though, sorry. Maybe it doesn't follow on HEAD requests by default? – Doug Stevenson Feb 17 '16 at 07:40
  • Maybe I misunderstood and have this the wrong way round. setInstanceFollowsRedirects(true) would mean that my client would be smart and follow the 302 Location field on my behalf - the reason I can't see it (correct?). So by setting this flag to false, I am telling the API that I want to handle it manually, so I then get to see Location in the response? – SparkyNZ Feb 17 '16 at 07:45