diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientAuthentication.java b/module-client/src/examples/org/apache/http/examples/client/ClientAuthentication.java new file mode 100644 index 000000000..2f00c85f1 --- /dev/null +++ b/module-client/src/examples/org/apache/http/examples/client/ClientAuthentication.java @@ -0,0 +1,70 @@ +/* + * $Header$ + * $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 + * . + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +package org.apache.http.examples.client; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; + +/** + * A simple example that uses HttpClient to execute an HTTP request against + * a target site that requires user authentication. + */ +public class ClientAuthentication { + + public static void main(String[] args) throws Exception { + DefaultHttpClient httpclient = new DefaultHttpClient(); + + httpclient.getState().setCredentials( + new AuthScope("localhost", 443), + new UsernamePasswordCredentials("username", "password")); + + HttpGet httpget = new HttpGet("https://localhost/protected"); + + System.out.println("executing request" + httpget.getRequestLine()); + HttpResponse response = httpclient.execute(httpget); + HttpEntity entity = response.getEntity(); + + System.out.println("----------------------------------------"); + System.out.println(response.getStatusLine()); + if (entity != null) { + System.out.println("Response content length: " + entity.getContentLength()); + System.out.println("Chunked?: " + entity.isChunked()); + } + if (entity != null) { + entity.consumeContent(); + } + } +} diff --git a/module-client/src/main/java/org/apache/http/client/HttpClient.java b/module-client/src/main/java/org/apache/http/client/HttpClient.java index 89c5e0069..7aab1a88e 100644 --- a/module-client/src/main/java/org/apache/http/client/HttpClient.java +++ b/module-client/src/main/java/org/apache/http/client/HttpClient.java @@ -91,9 +91,8 @@ public interface HttpClient { ; /** - * Executes a request using the {@link #getContext default context}. - * Same as {@link #execute(HttpUriRequest,HttpContext) - * client.execute(request, client.getContext())}, + * Executes a request using the {@link #getDefaultContext() default context}. + * * see there for details. * * @param request the request to execute @@ -142,8 +141,7 @@ public interface HttpClient { * {@link #getContext default context} * * @return the response to the request. See - * {@link #execute(HttpUriRequest,HttpContext) - * execute(target,request,context)} + * {@link #execute(HttpUriRequest,HttpContext)} * for details. * * @throws HttpException in case of a problem @@ -154,5 +152,22 @@ public interface HttpClient { throws HttpException, IOException ; + /** + * Executes a request along the given route using the + * {@link #getDefaultContext() default context}. + + * @param roureq the request to execute along with the route + * + * @return the response to the request. See + * {@link #execute(HttpUriRequest,HttpContext)} + * for details. + * + * @throws HttpException in case of a problem + * @throws IOException in case of an IO problem + *
timeout exceptions? + */ + HttpResponse execute(RoutedRequest roureq) + throws HttpException, IOException + ; } // interface HttpClient diff --git a/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java b/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java index 36e3ad025..f374acd32 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java +++ b/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java @@ -349,6 +349,8 @@ public abstract class AbstractHttpClient /** * Maps to {@link #execute(HttpUriRequest,HttpContext) * execute(request, context)}. + * The route is computed by {@link #determineRoute determineRoute}. + * This method uses {@link #getDefaultContext() default context}. * * @param request the request to execute * @@ -404,6 +406,13 @@ public abstract class AbstractHttpClient return execute(roureq, context); } + + public HttpResponse execute(RoutedRequest roureq) + throws HttpException, IOException { + return execute(roureq, null); + } + + // non-javadoc, see interface HttpClient public final HttpResponse execute(RoutedRequest roureq, HttpContext context) throws HttpException, IOException {