HTTPCLIENT-1201: Quick start doc improvements
Contributed by miles zarathustra <albert.bradley at thomsonreuters.com> git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1354386 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
413c41202b
commit
0b02fc11fd
|
@ -52,7 +52,7 @@ HttpClient Overview
|
|||
|
||||
[[1]] {{{./tutorial/html/index.html}HttpClient Tutorial}} - gives a detailed examination of the
|
||||
HttpClient API, which was written in close accordance with the (sometimes not very intuitive)
|
||||
HTTP specification/standard. A copy is also shipped with the release.
|
||||
HTTP specification/standard. A copy is also shipped with the release.
|
||||
{{{./tutorial/pdf/httpclient-tutorial.pdf}A PDF version}} is also available
|
||||
|
||||
[[1]] {{{./examples.html}HttpClient Examples}} - a set of examples demonstrating some of
|
||||
|
|
|
@ -52,50 +52,58 @@ HttpClient Quick Start
|
|||
|
||||
[]
|
||||
|
||||
[[2]] Quite often the easiest way to get started is with a short example. This example
|
||||
shows how to execute HTTP GET and POST requests using HttpClient native API.
|
||||
[[2]] The below code fragment illustrates the execution of HTTP GET and POST requests using the HttpClient native API.
|
||||
|
||||
-------------
|
||||
|
||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||
HttpGet httpget = new HttpGet("http://targethost/homepage");
|
||||
HttpGet httpGet = new HttpGet("http://targethost/homepage");
|
||||
|
||||
HttpResponse response1 = httpclient.execute(httpget);
|
||||
HttpResponse response1 = httpclient.execute(httpGet);
|
||||
|
||||
// The underlying HTTP connection is still held by the response object
|
||||
// to allow the response content to be streamed directly from the network socket.
|
||||
// In order to ensure correct deallocation of system resources
|
||||
// the user MUST either fully consume the response content or abort request
|
||||
// execution by calling HttpGet#releaseConnection().
|
||||
|
||||
try {
|
||||
System.out.println(response1.getStatusLine());
|
||||
HttpEntity entity1 = response1.getEntity();
|
||||
// do something useful with the response body
|
||||
// and ensure it is fully consumed
|
||||
EntityUtils.consume(entity1);
|
||||
} finally {
|
||||
httpget.releaseConnection();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// Handle exception
|
||||
}
|
||||
finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
|
||||
HttpPost httpost = new HttpPost("http://targethost/login");
|
||||
HttpPost httpPost = new HttpPost("http://targethost/login");
|
||||
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
|
||||
nvps.add(new BasicNameValuePair("username", "vip"));
|
||||
nvps.add(new BasicNameValuePair("password", "secret"));
|
||||
httpost.setEntity(new UrlEncodedFormEntity(nvps));
|
||||
HttpResponse response2 = httpclient.execute(httpost);
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
|
||||
HttpResponse response2 = httpclient.execute(httpPost);
|
||||
|
||||
try {
|
||||
System.out.println(response2.getStatusLine());
|
||||
HttpEntity entity2 = response2.getEntity();
|
||||
// do something useful with the response body
|
||||
// and ensure it is fully consumed
|
||||
EntityUtils.consume(entity2);
|
||||
} finally {
|
||||
httpost.releaseConnection();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// Handle exception
|
||||
finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
|
||||
-------------
|
||||
|
||||
[[3]] The same requests can be executed using a simpler, albeit less felxible, fluent API.
|
||||
[[3]] The same requests can be executed using a simpler, albeit less flexible, fluent API.
|
||||
|
||||
-------------
|
||||
|
||||
|
|
Loading…
Reference in New Issue