![]()
mport org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Scanner;
public class HCL {
public static void main(String[] args) {
try {
// doPost();
// doGet();
// doGetWithHandle();
// doGetWithabort();
clientWithInterceptor();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void doPost() throws IOException {
//Creating a HttpClient object
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//Creating a HttpGet object
HttpPost httppost = new HttpPost("http://www.baidu.com/");
//Printing the method used
System.out.println("Request Type: " + httppost.getMethod());
//Executing the Get request
HttpResponse httpresponse = httpclient.execute(httppost);
Scanner sc = new Scanner(httpresponse.getEntity().getContent());
//Printing the status line
System.out.println(httpresponse.getStatusLine());
while (sc.hasNext()) {
System.out.println(sc.nextLine());
}
} finally {
httpclient.close();
}
}
public static void doGet() throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
HttpResponse httpResponse = httpclient.execute(httpGet);
Scanner sc = new Scanner(httpResponse.getEntity().getContent());
//Printing the status line
System.out.println(httpResponse.getStatusLine());
while (sc.hasNext()) {
System.out.println(sc.nextLine());
}
}
public static void doGetWithHandle() throws IOException {
//Create an HttpClient object
CloseableHttpClient httpclient = HttpClients.createDefault();
//Create an HttpGet object
HttpGet httpget = new HttpGet("http://www.baidu.com/");
//Execute the Get request by passing the response handler object and HttpGet object
String httpresponse = httpclient.execute(httpget, new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
if(entity == null){
return "";
}else{
return EntityUtils.toString(entity);
}
}else{
return ""+status;
}
}
});
System.out.println(httpresponse);
}
public static void doGetWithabort() throws IOException {
//Creating an HttpClient object
CloseableHttpClient httpclient = HttpClients.createDefault();
//Creating an HttpGet object
HttpGet httpget = new HttpGet("http://www.kaops.com/");
//Printing the method used
System.out.println(httpget.getMethod());
//Executing the Get request
HttpResponse httpresponse = httpclient.execute(httpget);
//Printing the status line
System.out.println(httpresponse.getStatusLine());
httpget.abort();
System.out.println(httpresponse.getEntity().getContentLength());
//Executing the Get request
HttpResponse httpresponse2 = httpclient.execute(httpget);
System.out.println(httpresponse2.getStatusLine());
}
public static void clientWithInterceptor() throws IOException {
CloseableHttpClient httpclient =
HttpClients.custom().addInterceptorFirst(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext httpContext) throws HttpException, IOException {
if (request.containsHeader("sample-header")) {
System.out.println("Contains header sample-header, removing it..");
request.removeHeaders("sample-header");
}
//Printing remaining list of headers
Header[] headers = request.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i].getName());
}
}
}).build();
//Creating a request object
HttpGet httpget1 = new HttpGet("https://www.kaops.com/");
//Setting the header to it
httpget1.setHeader(new BasicHeader("sample-header","My first header"));
httpget1.setHeader(new BasicHeader("demo-header","My second header"));
httpget1.setHeader(new BasicHeader("test-header","My third header"));
//Executing the request
HttpResponse httpresponse = httpclient.execute(httpget1);
//Printing the status line
System.out.println(httpresponse.getStatusLine());
}