[httpcomments-client-4.5.2]--源碼分析(Working with message headers)

來源:互聯網
上載者:User

An HTTP message can contain a number of headers describing properties of the message such as the content length, content type and so on. HttpClient provides methods to retrieve, add, remove and enumerate headers.

一個HTTP訊息可以包含多個標題描述性質的資訊等內容的長度、內容類型等。HttpClient提供檢索,添加方法,刪除和列舉了頭。

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=localhostSet-Cookie: c2=b; path="/", c3=c; domain="localhost"2

The most efficient way to obtain all headers of a given type is 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=localhostSet-Cookie: c2=b; path="/", c3=c; domain="localhost"

It also provides convenience methods to parse HTTP messages into individual header elements.
它還提供了對HTTP報文分成單獨的標題元素的簡便方法。

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 = apath=/domain=localhostc2 = bpath=/c3 = cdomain=localhost

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.