An HTTP message can contain a number of headers describing properties of the message such as the content length, content t Ype and so on. HttpClient provides methods to retrieve, add, remove and enumerate headers.
An HTTP message can contain multiple headings describing the nature of the content such as the length, content type, etc. HttpClient provides search, add methods, deletes, and enumerates the headers.
HttpResponse response = new Basichttpresponse (httpversion.http_1_1,
httpstatus.sc_ok, "OK");
Response.AddHeader ("Set-cookie",
"c1=a; path=/; Domain=localhost");
Response.AddHeader ("Set-cookie",
"C2=b", path=\ "/\", C3=c; domain=\ "localhost\");
Header H1 = Response.getfirstheader ("Set-cookie");
SYSTEM.OUT.PRINTLN (H1);
Header H2 = Response.getlastheader ("Set-cookie");
System.out.println (H2);
Header[] hs = response.getheaders ("Set-cookie");
System.out.println (hs.length);
Stdout>
Set-cookie:c1=a; path=/; Domain=localhost
set-cookie:c2=b path= "/", c3=c domain= "localhost"
2
The most efficient way to obtain all headers of a given type are by using the Headeriterator interface.
HttpResponse response = new Basichttpresponse (httpversion.http_1_1,
httpstatus.sc_ok, "OK");
Response.AddHeader ("Set-cookie",
"c1=a; path=/; Domain=localhost");
Response.AddHeader ("Set-cookie",
"C2=b", path=\ "/\", C3=c; domain=\ "localhost\");
Headeriterator it = response.headeriterator ("Set-cookie");
while (It.hasnext ()) {
System.out.println (It.next ());
}
StdOut >
Set-cookie:c1=a; path=/; Domain=localhost
set-cookie:c2=b path= "/", c3=c domain= "localhost"
It also provides convenience methods to parse the HTTP messages into individual header elements.
It also provides an easy way to split HTTP messages into separate header elements.
HttpResponse response = new Basichttpresponse (httpversion.http_1_1,
httpstatus.sc_ok, "OK");
Response.AddHeader ("Set-cookie",
"c1=a; path=/; Domain=localhost");
Response.AddHeader ("Set-cookie",
"C2=b", path=\ "/\", C3=c; domain=\ "localhost\");
Headerelementiterator it = new Basicheaderelementiterator (
response.headeriterator ("Set-cookie"));
while (It.hasnext ()) {
headerelement elem = It.nextelement ();
System.out.println (Elem.getname () + "=" + Elem.getvalue ());
namevaluepair[] params = Elem.getparameters ();
for (int i = 0; i < params.length i++) {
System.out.println ("" + params[i]);
}
Stdout>
C1 = a
path=/
domain=localhost
c2 = b
path=/
c3 = C
domain=localhost