Thursday, June 30, 2022

URLConnection : getHeaderFieldKey : Get the nth header field

public String getHeaderFieldKey(int n)

Returns the key for the nth header field. The request method is header zero and has a null key.

import java.net.URL;
import java.net.URLConnection;

public class Main {

    public static void main(String args[]) throws Exception {
        String resource = "https://self-learning-java-tutorial.blogspot.com";

        /* Construct URL object */
        URL url = new URL(resource);

        /* Open URLConnection to this URL */
        URLConnection conn = url.openConnection();

        for (int i = 1;; i++) {
            String headerField = conn.getHeaderFieldKey(i);
            if (headerField == null) {
                break;
            }
            System.out.println(headerField + " : " + conn.getHeaderField(i));
        }
    }
}

Sample Output
Content-Type : text/html; charset=UTF-8
Expires : Tue, 19 May 2015 15:30:48 GMT
Date : Tue, 19 May 2015 15:30:48 GMT
Cache-Control : private, max-age=0
Last-Modified : Mon, 18 May 2015 10:26:27 GMT
X-Content-Type-Options : nosniff
X-XSS-Protection : 1; mode=block
Server : GSE
Alternate-Protocol : 80:quic,p=1
Accept-Ranges : none
Vary : Accept-Encoding
Transfer-Encoding : chunked


Note
public String getHeaderField(int n)
Returns the value for the nth header field.

No comments:

Post a Comment

Difference Between Socket and Server Socket in Java

Two essential Java classes-Socket and ServerSocket-have different functions when it comes to creating networked applications. These classes ...