Reputation: 91
i am developing an android app wherein i need to GET HTTP headers of a website over my android activity, below cited link where i found a code but not getting all the headers.
http://www.mkyong.com/java/how-to-get-http-response-header-in-java/
i need results like shown in the below image
(source: osxdaily.com)
please support.
Upvotes: 0
Views: 6757
Reputation: 3080
Check out the java sample code that should work with android as well:
Method-1
URL url = new URL("http://www.google.com:80");
URLConnection conn = url.openConnection();
Map<String, List<String>> headerFields = conn.getHeaderFields();
Set<String> headerFieldsSet = headerFields.keySet();
Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();
while (hearerFieldsIter.hasNext()) {
String headerFieldKey = hearerFieldsIter.next();
List<String> headerFieldValue = headerFields.get(headerFieldKey);
StringBuilder sb = new StringBuilder();
for (String value : headerFieldValue) {
sb.append(value);
sb.append("");
}
System.out.println(headerFieldKey + "=" + sb.toString());
}
Method-2 (Ref: Get headers from HttpClient in android)
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("http://mkyong.com");
HttpResponse response = client.execute(request);
//get all headers
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
System.out.println("Key : " + header.getName()
+ " ,Value : " + header.getValue());
}
//get header by 'key'
String server = response.getFirstHeader("Server").getValue();
Hope it will help you..
Upvotes: 3
Reputation: 16739
Try this:
HttpResponse
response ; // http response
Get entity from the response
final HttpEntity entity = response.getEntity();
if(entity == null) {
return;
}
Get the Header object from the entity. Refer this
final Header header = entity.getContentEncoding();
if (header == null) {
return;
}
Get each header element Refer this
for (HeaderElement h : header.getElements()) {
if (h.getName().equalsIgnoreCase(HEADER_PARAMETER_VALUE)) {
h.getValue();
return;
}
}
Upvotes: 0
Reputation: 3219
Depending on http post or get you can use getAllHeaders or getHeaders method. Here is an example for http post.
HTTP POST method :
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
first method :
public Header[] getAllHeaders ()
Added in API level 1
Returns all the headers of this message. Headers are orderd in the sequence they will be sent over a connection.
Returns
all the headers of this message
second method description :
public Header[] getHeaders (String name)
Added in API level 1
Returns all the headers with a specified name of this message. Header values are ignored. Headers are orderd in the sequence they will be sent over a connection.
Parameters
name the name of the headers to return.
Returns
the headers whose name property equals name.
Upvotes: 0
Reputation: 5068
let say you have
HttpResponse response ;
then do.(for specific header)
Header[] headers = response.getHeaders("Server");
if (headers.length > 0) {
String value = headers[0].getValue();
and to retrieve all headers
Header[] headers = response.getAllHeaders();
for(int i =0 ; i<headers.length;i++)
{
System.out.println("header : "+i+" - > "+headers[i]);
}
Upvotes: 1