1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 package org.apache.hc.client5.http.examples.fluent; 28 29 import java.io.File; 30 31 import org.apache.hc.client5.http.fluent.Executor; 32 import org.apache.hc.client5.http.fluent.Form; 33 import org.apache.hc.client5.http.fluent.Request; 34 import org.apache.hc.core5.http.ContentType; 35 import org.apache.hc.core5.http.HttpHost; 36 import org.apache.hc.core5.http.HttpVersion; 37 import org.apache.hc.core5.util.Timeout; 38 39 /** 40 * This example demonstrates how the he HttpClient fluent API can be used to execute multiple 41 * requests within the same security context. The Executor class maintains a common context shared 42 * by all requests executed with it. The Executor is thread-safe and can be used to execute 43 * requests concurrently from multiple threads of execution. 44 */ 45 public class FluentExecutor { 46 47 public static void main(final String... args)throws Exception { 48 final Executor executor = Executor.newInstance() 49 .auth(new HttpHost("somehost"), "username", "password".toCharArray()) 50 .auth(new HttpHost("myproxy", 8080), "username", "password".toCharArray()) 51 .authPreemptive(new HttpHost("myproxy", 8080)); 52 53 // Execute a GET with timeout settings and return response content as String. 54 executor.execute(Request.get("http://somehost/") 55 .connectTimeout(Timeout.ofSeconds(1))) 56 .returnContent().asString(); 57 58 // Execute a POST with the 'expect-continue' handshake, using HTTP/1.1, 59 // containing a request body as String and return response content as byte array. 60 executor.execute(Request.post("http://somehost/do-stuff") 61 .useExpectContinue() 62 .version(HttpVersion.HTTP_1_1) 63 .bodyString("Important stuff", ContentType.DEFAULT_TEXT) 64 ).returnContent().asBytes(); 65 66 // Execute a POST with a custom header through the proxy containing a request body 67 // as an HTML form and save the result to the file 68 executor.execute(Request.post("http://somehost/some-form") 69 .addHeader("X-Custom-header", "stuff") 70 .viaProxy(new HttpHost("myproxy", 8080)) 71 .bodyForm(Form.form().add("username", "vip").add("password", "secret").build()) 72 ).saveContent(new File("result.dump")); 73 } 74 75 }