Use httpbin.org in examples

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.5.x@1694618 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2015-08-07 07:33:11 +00:00
parent bd29207ff9
commit 369b375c3b
18 changed files with 44 additions and 44 deletions

View File

@ -40,7 +40,7 @@ public class ClientAbortMethod {
public final static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet("http://www.apache.org/");
HttpGet httpget = new HttpGet("http://httpbin.org/get");
System.out.println("Executing request " + httpget.getURI());
CloseableHttpResponse response = httpclient.execute(httpget);

View File

@ -45,20 +45,20 @@ public class ClientAuthentication {
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
new AuthScope("httpbin.org", 80),
new UsernamePasswordCredentials("user", "passwd"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("http://localhost/");
HttpGet httpget = new HttpGet("http://httpbin.org/basic-auth/user/passwd");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}

View File

@ -49,7 +49,7 @@ public class ClientChunkEncodedPost {
}
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost("http://localhost/");
HttpPost httppost = new HttpPost("http://httpbin.org/post");
File file = new File(args[0]);
@ -69,7 +69,7 @@ public class ClientChunkEncodedPost {
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}

View File

@ -81,6 +81,7 @@ import org.apache.http.message.BasicLineParser;
import org.apache.http.message.LineParser;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;
/**
* This example demonstrates how to customize and configure the most common aspects
@ -221,7 +222,7 @@ public class ClientConfiguration {
.build();
try {
HttpGet httpget = new HttpGet("http://www.apache.org/");
HttpGet httpget = new HttpGet("http://httpbin.org/get");
// Request configuration can be overridden at the request level.
// They will take precedence over the one set at the client level.
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
@ -242,13 +243,9 @@ public class ClientConfiguration {
System.out.println("executing request " + httpget.getURI());
CloseableHttpResponse response = httpclient.execute(httpget, context);
try {
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(EntityUtils.toString(response.getEntity()));
System.out.println("----------------------------------------");
// Once the request has been executed the local context can

View File

@ -45,7 +45,7 @@ public class ClientConnectionRelease {
public final static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet("http://localhost/");
HttpGet httpget = new HttpGet("http://httpbin.org/get");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);

View File

@ -56,7 +56,7 @@ public class ClientCustomContext {
// Bind custom cookie store to the local context
localContext.setCookieStore(cookieStore);
HttpGet httpget = new HttpGet("http://localhost/");
HttpGet httpget = new HttpGet("http://httpbin.org/cookies");
System.out.println("Executing request " + httpget.getRequestLine());
// Pass local context as a parameter

View File

@ -73,7 +73,7 @@ public class ClientCustomPublicSuffixList {
.build();
try {
HttpGet httpget = new HttpGet("https://remotehost/");
HttpGet httpget = new HttpGet("https://httpbin.org/");
System.out.println("executing request " + httpget.getRequestLine());

View File

@ -63,9 +63,9 @@ public class ClientCustomSSL {
.build();
try {
HttpGet httpget = new HttpGet("https://localhost/");
HttpGet httpget = new HttpGet("https://httpbin.org/");
System.out.println("executing request " + httpget.getRequestLine());
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {

View File

@ -68,7 +68,7 @@ public class ClientEvictExpiredConnections {
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}

View File

@ -45,7 +45,7 @@ public class ClientExecuteProxy {
public static void main(String[] args)throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpHost target = new HttpHost("localhost", 443, "https");
HttpHost target = new HttpHost("httpbin.org", 443, "https");
HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
RequestConfig config = RequestConfig.custom()
@ -60,7 +60,7 @@ public class ClientExecuteProxy {
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}

View File

@ -67,7 +67,7 @@ public class ClientExecuteSOCKS {
HttpClientContext context = HttpClientContext.create();
context.setAttribute("socks.address", socksaddr);
HttpHost target = new HttpHost("localhost", 80, "http");
HttpHost target = new HttpHost("httpbin.org", 80, "http");
HttpGet request = new HttpGet("/");
System.out.println("Executing request " + request + " to " + target + " via SOCKS proxy " + socksaddr);

View File

@ -52,11 +52,11 @@ import org.apache.http.util.EntityUtils;
public class ClientPreemptiveBasicAuthentication {
public static void main(String[] args) throws Exception {
HttpHost target = new HttpHost("localhost", 80, "http");
HttpHost target = new HttpHost("httpbin.org", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(target.getHostName(), target.getPort()),
new UsernamePasswordCredentials("username", "password"));
new UsernamePasswordCredentials("user", "passwd"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
try {
@ -72,7 +72,7 @@ public class ClientPreemptiveBasicAuthentication {
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
HttpGet httpget = new HttpGet("/");
HttpGet httpget = new HttpGet("http://httpbin.org/hidden-basic-auth/user/passwd");
System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
for (int i = 0; i < 3; i++) {
@ -80,7 +80,7 @@ public class ClientPreemptiveBasicAuthentication {
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}

View File

@ -53,11 +53,11 @@ import org.apache.http.util.EntityUtils;
public class ClientPreemptiveDigestAuthentication {
public static void main(String[] args) throws Exception {
HttpHost target = new HttpHost("localhost", 80, "http");
HttpHost target = new HttpHost("httpbin.org", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(target.getHostName(), target.getPort()),
new UsernamePasswordCredentials("username", "password"));
new UsernamePasswordCredentials("user", "passwd"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
@ -78,7 +78,7 @@ public class ClientPreemptiveDigestAuthentication {
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
HttpGet httpget = new HttpGet("/");
HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd");
System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
for (int i = 0; i < 3; i++) {
@ -86,7 +86,7 @@ public class ClientPreemptiveDigestAuthentication {
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}

View File

@ -47,18 +47,21 @@ public class ClientProxyAuthentication {
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("username", "password"));
new AuthScope("localhost", 8888),
new UsernamePasswordCredentials("squid", "squid"));
credsProvider.setCredentials(
new AuthScope("httpbin.org", 80),
new UsernamePasswordCredentials("user", "passwd"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
try {
HttpHost target = new HttpHost("www.verisign.com", 443, "https");
HttpHost proxy = new HttpHost("localhost", 8080);
HttpHost target = new HttpHost("httpbin.org", 80, "http");
HttpHost proxy = new HttpHost("localhost", 8888);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
HttpGet httpget = new HttpGet("/");
HttpGet httpget = new HttpGet("/basic-auth/user/passwd");
httpget.setConfig(config);
System.out.println("Executing request " + httpget.getRequestLine() + " to " + target + " via " + proxy);
@ -67,7 +70,7 @@ public class ClientProxyAuthentication {
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}

View File

@ -64,7 +64,7 @@ public class ClientWithRequestFuture {
};
// Simple request ...
HttpGet request1 = new HttpGet("http://google.com");
HttpGet request1 = new HttpGet("http://httpbin.org/get");
HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1,
HttpClientContext.create(), handler);
Boolean wasItOk1 = futureTask1.get();
@ -72,7 +72,7 @@ public class ClientWithRequestFuture {
// Cancel a request
try {
HttpGet request2 = new HttpGet("http://google.com");
HttpGet request2 = new HttpGet("http://httpbin.org/get");
HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2,
HttpClientContext.create(), handler);
futureTask2.cancel(true);
@ -83,7 +83,7 @@ public class ClientWithRequestFuture {
}
// Request with a timeout
HttpGet request3 = new HttpGet("http://google.com");
HttpGet request3 = new HttpGet("http://httpbin.org/get");
HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3,
HttpClientContext.create(), handler);
Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
@ -107,7 +107,7 @@ public class ClientWithRequestFuture {
};
// Simple request with a callback
HttpGet request4 = new HttpGet("http://google.com");
HttpGet request4 = new HttpGet("http://httpbin.org/get");
// using a null HttpContext here since it is optional
// the callback will be called when the task completes, fails, or is cancelled
HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4,

View File

@ -47,7 +47,7 @@ public class ClientWithResponseHandler {
public final static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet("http://localhost/");
HttpGet httpget = new HttpGet("http://httpbin.org/");
System.out.println("Executing request " + httpget.getRequestLine());

View File

@ -45,7 +45,7 @@ public class QuickStart {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet("http://targethost/homepage");
HttpGet httpGet = new HttpGet("http://httpbin.org/get");
CloseableHttpResponse 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.
@ -64,7 +64,7 @@ public class QuickStart {
response1.close();
}
HttpPost httpPost = new HttpPost("http://targethost/login");
HttpPost httpPost = new HttpPost("http://httpbin.org/post");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));

View File

@ -23,4 +23,4 @@
# <http://www.apache.org/>.
# Disable logging for unit tests
org.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog
# org.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog