Sample code for 30+ languages & platforms
Java

Get a REST Response Header Value by Index

See more REST Examples

Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Rest;

public class ChilkatExample {

    public static void main(String[] args) {
        Rest rest = new Rest();
        boolean bTls = true;
        boolean bAutoReconnect = true;
        try {
            rest.connect("example.com", 443, bTls, bAutoReconnect);

            rest.sendReqNoBody("GET", "/api/items/123");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int statusCode = rest.readResponseHeader();
        if (statusCode < 0) {
            System.out.println(rest.getLastErrorText());
            return;
        }

        // Return the value of the response header at a zero-based index.  Use the same index with
        // ResponseHdrName to get the corresponding field name.
        int numHeaders = rest.getNumResponseHeaders();
        for (int i = 0; i < numHeaders; i++) {
            String value;
            try {
                value = rest.responseHdrValue(i);
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                return;
            }

            System.out.println("Header " + i + " value: " + value);
        }
    }
}