Sending HTTP Requests using Apache HTTP Client:
Below is the sample code to test end points using Apache Http Client in Java
This is a GET Method.
package org.apache.http.examples.client; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; public class GetLibrary { public static void main(String args[]) throws Exception { String url = "http://www.google.com/search?q=httpClient"; HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); request.addHeader("User-Agent", "USER-AGENT"); HttpResponse response = client.execute(request); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } System.out.println(result); } }
Can you tell us more about this? I’d want to find out some additional information.
LikeLike
Very good article! We will be linking to this great article on our website.
Keep up the great writing.
LikeLiked by 1 person
HttpPost(url); –> Post Method, HttpDeleteWithBody(url); –> Delete Method, HttpPut(url); –> Put Method.
LikeLike