httpcomponents-client-4 0 1tutorialpdfhttpclient-tutorial_第1頁
httpcomponents-client-4 0 1tutorialpdfhttpclient-tutorial_第2頁
httpcomponents-client-4 0 1tutorialpdfhttpclient-tutorial_第3頁
httpcomponents-client-4 0 1tutorialpdfhttpclient-tutorial_第4頁
httpcomponents-client-4 0 1tutorialpdfhttpclient-tutorial_第5頁
已閱讀5頁,還剩42頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、HttpClient TutorialOleg KalnichevskiPreface. .1. HttpClient scope .2. What HttpClient is NOT .iv iviv1.Fundamentals11.1. Request execution11.1.1. HTTP request11.1.2. HTTP response21.1.3. Working with message headers21.1.4. HTTP entity41.1.5. Ensuring release of low level resources51.1.6. Consuming e

2、ntity content51.1.7. Producing entity content61.1.8. Response handlers71.2. HTTP execution context81.3. Exception handling91.3.1. HTTP transport safety91.3.2. Idempotent methods91.3.3. Automatic exception recovery101.3.4. Request retry handler .Aborting requests .HTTP protocol interceptors .HTTP par

3、ameters .1.6.1. Parameter hierarchies .1.6.2. HTTP parameters beans .HTTP request execution parameters .1011111212131315151616161717171818181919202020222223232324252..2.Connection management ..2.3.Connection parameters .Connection persistence .HTTP connection routing .2.3.1. Ro

4、ute computation .2.3.2. Secure HTTP connections .HTTP route parameters .Socket factories ....3.Secure socket layering .SSL/TLS customization .Hostname verification ..2.8.Protocol schemes .HttpClient proxy configuration .HTTP connection managers .2.8.1. Connection operator

5、s .2.8.2. Managed connections and connection managers ...5.Simple connection manager .Pooling connection manager .Connection manager shutdown .2.9.Connection management parameters .1.2.12.Multithreaded request execution .Connection eviction policy .Connection keep alive strateg

6、y .3.HTTP state management .iiHttpClient Tutorial3.1. HTTP cookies .3.1.1. Cookie versions .3.2. Cookie specifications .3.3. HTTP cookie and state management parameters .2727282929293030303132323233333..Cookie specification registry .Choosing cookie policy .Custom cookie policy .Cooki

7、e persistence .3.8. HTTP state management and execution context .3.9. Per user / thread state management .HTTP authentication ...4.4.5.User credentials .Authentication schemes .HTTP authentication parameters .Authentication scheme registry .Credentials provider .4.6. HTTP authenticatio

8、n and execution context344.7. Preemptive authentication .4.8. NTLM Authentication .4.8.1. NTLM connection persistence .HTTP client service .3536363838393940414142424..4.HttpClient facade .HttpClient parameters .Automcatic redirect handling .HTTP client and execution context .6.Advance

9、d topics ..Custom client connections .Stateful HTTP connections .6.2.1. User token handler .6.2.2. User token and execution context .iiiPrefaceThe Hyper-Text Transfer Protocol (HTTP) is perhaps the most significant protocol used on the Internet today. Web services, network-enabled appliances

10、and the growth of network computing continue to expand the role of the HTTP protocol beyond user-driven web browsers, while increasing the numberof applications that require HTTP support.Although the package provides basic functionality for accessing resources via HTTP, it doesnt provide th

11、e full flexibility or functionality needed by many applications. HttpClient seeks to fill this void by providing an efficient, up-to-date, and feature-rich package implementing the client side ofthe most recent HTTP standards and recommendations.Designed for extension while providing robust support

12、for the base HTTP protocol, HttpClient maybe of interest to anyone building HTTP-aware client applications such as web browsers, web service clients, or systems that leverage or extend the HTTP protocol for distributed communication.1. HttpClient scopeClient-side HTTP transport library based on Http

13、Core /httpcomponents-core/index.htmlBased on classic (blocking) I/OContent agnostic2. What HttpClient is NOTHttpClient is NOT a browser. It is a client side HTTP transport library. HttpClients purpose is to transmit and receive HTTP messages. HttpClient will not attempt to cache c

14、ontent, execute javascript embedded in HTML pages, try to guess content type, or reformat request / redirect locationURIs, or other functionality unrelated to the HTTP transport.ivChapter 1. Fundamentals1.1. Request executionThe most essential function of HttpClient is to execute HTTP methods. Execu

15、tion of an HTTP method involves one or several HTTP request / HTTP response exchanges, usually handled internally by HttpClient. The user is expected to provide a request object to execute and HttpClient is expected totransmit the request to the target server return a corresponding response object,

16、or throw an exception if execution was unsuccessful.Quite naturally, the main entry point of the HttpClient API is the HttpClient interface that defines thecontract described above.Here is an example of request execution process in its simplest form:1.1.1. HTTP requestAll HTTP requests have a reques

17、t line consisting a method name, a request URI and a HTTP protocolversion.HttpClient supports out of the box all HTTP methods defined in the HTTP/1.1 specification: GET,HEAD, POST, PUT, DELETE, TRACE and OPTIONS. There is a special class for each method type.: HttpGet, HttpHead, HttpPost, HttpPut, H

18、ttpDelete, HttpTrace, and HttpOptions.The Request-URI is a Uniform Resource Identifier that identifies the resource upon which to apply the request. HTTP request URIs consist of a protocol scheme, host name, optional port, resource path, optional query, and optional fragment.HttpClient provides a nu

19、mber of utility methods to simplify creation and modification of request URIs.URI can be assembled programmatically:stdout 1URI uri = URIUtils.createURI(http, , -1, /search, q=httpclient&btnG=Google+Search&aq=f&oq=, null);HttpGet httpget = new HttpGet(uri); System.out.println(httpget.g

20、etURI();HttpGet httpget = newHttpGet( /search?hl=en&q=httpclient&btnG=Google+Search&aqHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(http:/localhost/); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity();if

21、 (entity != null) InputStreamtream = entity.getContent(); int l;byte tmp = new byte2048;while (l =tream.read(tmp) != -1) FundamentalsQuery string can also be generated from individual parameters:stdout 1.1.2. HTTP responseHTTP response is a message sent by the server back to the client after having

22、received and interpreteda request message. The first line of that message consists of the protocol version followed by a numeric status code and its associated textual phrase.stdout 1.1.3. Working with message headersAn HTTP message can contain a number of headers describing properties of the messag

23、e such as the content length, content type and so on. HttpClient provides methods to retrieve, add, remove and enumerate headers.2HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, OK);response.addHeader(Set-Cookie, c1=a; path=/; domain=localhost);response.addHeade

24、r(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);HTTP/1.1 200OKHTTP/1.1 200 OKHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, OK);System.out

25、.println(response.getProtocolVersion(); System.out.println(response.getStatusLine().getStatusCode(); System.out.println(response.getStatusLine().getReasonPhrase(); System.out.println(response.getStatusLine().toString();/search?q=httpclient&btnG=Google+Search&aq=f&oq=List qparams

26、= new ArrayList(); qparams.add(new BasicNameValuePair(q, httpclient); qparams.add(new BasicNameValuePair(btnG, Google Search); qparams.add(new BasicNameValuePair(aq, f); qparams.add(new BasicNameValuePair(oq, null);URI uri = URIUtils.createURI(http, , -1, /search, URLEncodedUtils.forma

27、t(qparams, UTF-8), null);HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI();/search?q=httpclient&btnG=Google+Search&aq=f&oq=Fundamentalsstdout The most efficient way to obtain all headers of a given type is by using the HeaderIterator interface.stdout It also

28、 provides convenience methods to parse HTTP messages into individual header elements.stdout 3c1 = a path=/domain=localhost c2 = bpath=/ c3 = cHttpResponse 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 = newBasicHeaderElementIterator( response.headerIterator(S et-Cookie);while (it.hasNext() HeaderElement elem = it.nextElement(); System.out.println(elem.getName() + = + elem.getValue(); NameValuePair params =

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論