Added connection release sample

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@552268 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-07-01 10:11:34 +00:00
parent 3fb3b857da
commit 70e9312721
6 changed files with 109 additions and 26 deletions

View File

@ -33,13 +33,9 @@ package org.apache.http.examples.client;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
/**
* How to abort an HTTP method before its normal completion.
@ -52,14 +48,7 @@ import org.apache.http.params.HttpProtocolParams;
public class ClientAbortMethod {
public final static void main(String[] args) throws Exception {
// Initialize default parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0");
HttpClient httpclient = new DefaultHttpClient(params);
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.apache.org/");

View File

@ -0,0 +1,101 @@
/*
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.examples.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* This examples demonstrates the recommended way of using API to make sure
* the underlying connection gets released back to the connection manager.
*
* <!-- empty lines above to avoid 'svn diff' context problems -->
* @version $Revision$
*
* @since 4.0
*/
public class ClientConnectionRelease {
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.apache.org/");
// Execute HTTP request
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println("----------------------------------------");
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to bother about connection release
if (entity != null) {
// do something useful with the response
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent()));
System.out.println(reader.readLine());
// In case of an IOException the connection will be released
// back to the connection manager automatically
// No need for ugly try-finally clause
// If a runtime exception can be thrown in the process
// of response processing make sure the HTTP request
// gets aborted to ensure connection release.
try {
// Do something that can throw a non-fatal
// unchecked exception
} catch (RuntimeException ex) {
// Abort HTTP request to ensure connection release
httpget.abort();
}
// When done with the response either close the stream
reader.close();
// Or call
entity.consumeContent();
// Either of these methods will cause connection release
}
}
}

View File

@ -33,17 +33,13 @@ package org.apache.http.examples.client;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.protocol.HttpContext;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpState;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
/**
* This examples demonstrates the use of a local HTTP context
@ -57,14 +53,8 @@ import org.apache.http.params.HttpProtocolParams;
public class ClientCustomContext {
public final static void main(String[] args) throws Exception {
// Initialize default parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0");
HttpClient httpclient = new DefaultHttpClient(params);
HttpClient httpclient = new DefaultHttpClient();
// Create a local instance of HttpState
HttpState localState = new HttpState();

View File

@ -161,7 +161,6 @@ public class ClientExecuteDirect {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0");
HttpProtocolParams.setUseExpectContinue(params, true);
defaultParameters = params;

View File

@ -170,7 +170,6 @@ public class ClientExecuteProxy {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0");
HttpProtocolParams.setUseExpectContinue(params, true);
defaultParameters = params;

View File

@ -114,6 +114,11 @@ public class DefaultHttpClient extends AbstractHttpClient {
}
public DefaultHttpClient() {
super(null, null);
}
protected HttpParams createHttpParams() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);