Java
Chilkat2 Java
Enumerate REST Response Header Names
See more REST Examples
Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.
Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given header.
Chilkat Java Downloads
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;
}
// Enumerate the response header fields by zero-based index. ResponseHdrName returns the field name
// and ResponseHdrValue returns the value at the same index.
int numHeaders = rest.getNumResponseHeaders();
for (int i = 0; i < numHeaders; i++) {
String name;
String value;
try {
name = rest.responseHdrName(i);
value = rest.responseHdrValue(i);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println(name + ": " + value);
}
}
}