Dear Wiki user, You have subscribed to a wiki page or wiki category on "Httpcomponents Wiki" for change notification.
The following page has been changed by QuintinBeukes: http://wiki.apache.org/HttpComponents/GeneralHttpCoreIntroduction The comment on the change is: Added line numbering support ------------------------------------------------------------------------------ our basic connection manager. Here is the code to setup the static members of the class. - {{{ + {{{#!java numbers=off private static final HttpParams params; private static final BasicHttpProcessor httpProcessor; @@ -134, +134 @@ property names, and the !ExecutionContext constants are simply String objects creating an abstraction over these property names. - {{{ + {{{#!java numbers=off private final HttpContext context = new BasicHttpContext(null); }}} @@ -142, +142 @@ Now we can start setting up the particulars for the request itself. This is the constructor we are going to use: - {{{ + {{{#!java numbers=off private final HttpRequest request; private final DefaultHttpClientConnection connection; @@ -174, +174 @@ thread safety will be built into it for now. Later when we make it more advanced enough attention will be given to make it production ready. - {{{ + {{{#!java numbers=off // create the request request = new BasicHttpRequest("GET", url.toString(), HttpVersion.HTTP_1_1); context.setAttribute(ExecutionContext.HTTP_REQUEST, request); @@ -188, +188 @@ As seen the !ExecutionContext.HTTP_REQUEST attribute (http.request) takes a !HttpRequest object as it's value. - {{{ + {{{#!java numbers=off // set the host to send as a host header HttpHost requestHost = new HttpHost(url.getHost(), url.getPort()); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, requestHost); @@ -206, +206 @@ Again we set the !ExecutionContext.HTTP_TARGET_HOST context attribute (http.target_host), which takes an !HttpHost object as value. - {{{ + {{{#!java numbers=off // fetch a connection from the connection manager connection = connectionManager.getConnection(url); context.setAttribute(ExecutionContext.HTTP_CONNECTION, connection); @@ -221, +221 @@ create a URL object from the String representation. == Executing the request and receiving the response == - {{{ + {{{#!java numbers=off private HttpResponse response; private InputStream inputStream; @@ -268, +268 @@ Three of these are used to consume the whole stream and store it in either a byte array or a String. This is an example of their use: - {{{ + {{{#!java numbers=off String charset = EntityUtils.getContentCharSet(response.getEntity()); byte[] data = EntityUtils.toByteArray(response.getEntity()); String data = EntityUtils.toString(response.getEntity()); @@ -289, +289 @@ completes. For instance if a null pointer exception occurs because you didn't execute the request, at least the connection will be close/released. - {{{ + {{{#!java numbers=off public void close() { try @@ -329, +329 @@ Here they are, they are pretty self explanatory, though we will be discussing the Header class. - {{{ + {{{#!java numbers=off public Header[] getHeader(String name) throws HttpException { return response.getHeaders(name); @@ -403, +403 @@ methods, as long as you supply a java.net.Socket object to !HttpCore's connection wrapper. - {{{ + {{{#!java numbers=off public class ConnectionManager { // max requests per connection @@ -505, +505 @@ There are better ways to implement this limit, but for now this is sufficient. When the limit has been reached on the server, a !NoHttpResponseException will be thrown - when you execute the request. + when you execute the request. If you receive such an exception, try reducing the + MAX_PERSISTENT_REQUESTS constant. This isn't ideal, but sufficient for focusing on demonstrating the basic usage of !HttpCore. Later we will build around these weaknesses, making it a stronger, more @@ -528, +529 @@ If an exception occurs (connection failure, invalid URL, etc.) during the creation/execution of the request, we again catch and display it. - {{{ + {{{#!java numbers=off public static void main(String[] args) { if (args.length < 1) --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
