我是HTTP的新手。我在使用此链接下载时尝试获取文件名或位置:
http://amp.dascene.net/downmod.php?index=40871
当我通过cURL运行链接时,我得到以下内容:
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
如何在Android中获取上述信息?我试图这样做:
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();
}
但是,“位置”字段或提示文件名称的任何其他内容都不在输出列表中。这就是我得到的:
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
如何获取cURL获取的位置字段?
答案 0 :(得分:1)
HTTP状态代码302是重定向。默认情况下,HttpURLConnection将自动且透明地遵循重定向。如果您希望自己处理,可以使用setFollowRedirects(false)关闭此功能。