* Added a convenience method to the HttpClient interface
* Added authentication sample git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@554082 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ba3da55d99
commit
ef0368e4fc
|
@ -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
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
* [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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
* <br/><i @@@>timeout exceptions?</i>
|
||||
*/
|
||||
HttpResponse execute(RoutedRequest roureq)
|
||||
throws HttpException, IOException
|
||||
;
|
||||
|
||||
} // interface HttpClient
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue