Java
Chilkat2 Java
Get a REST Response Header by Name
See more REST Examples
Demonstrates Rest.ResponseHdrByName, which returns the value of a response header field by name. Header field names are case-insensitive.
Background. Response headers are available after ReadResponseHeader has read the status line and headers. Lookup by name is convenient when a specific header, such as Content-Type, is needed.
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;
}
// Return the value of a response header field by name. Header field names are case-insensitive.
String contentType = rest.responseHdrByName("Content-Type");
if (contentType == null) {
System.out.println(rest.getLastErrorText());
return;
}
System.out.println("Content-Type: " + contentType);
}
}