Java
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.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
CkRest rest = new CkRest();
boolean bTls = true;
boolean bAutoReconnect = true;
success = rest.Connect("example.com",443,bTls,bAutoReconnect);
if (success == false) {
System.out.println(rest.lastErrorText());
return;
}
success = rest.SendReqNoBody("GET","/api/items/123");
if (success == false) {
System.out.println(rest.lastErrorText());
return;
}
int statusCode = rest.ReadResponseHeader();
if (statusCode < 0) {
System.out.println(rest.lastErrorText());
return;
}
// Return the value of a response header field by name. Header field names are case-insensitive.
String contentType = rest.responseHdrByName("Content-Type");
if (rest.get_LastMethodSuccess() == false) {
System.out.println(rest.lastErrorText());
return;
}
System.out.println("Content-Type: " + contentType);
}
}