Created
October 30, 2017 16:26
-
-
Save prabhatkashyap/1a6ebcf843053060cf2c66e7aae8254c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
public class HttpClientExample { | |
private final String USER_AGENT = "Mozilla/5.0"; | |
public static void main(String[] args) throws Exception { | |
HttpClientExample http = new HttpClientExample(); | |
System.out.println("Testing 1 - Send Http GET request"); | |
http.sendGet(); | |
System.out.println("\nTesting 2 - Send Http POST request"); | |
http.sendPost(); | |
} | |
// HTTP GET request | |
private void sendGet() throws Exception { | |
String url = "http://www.google.com/search?q=developer"; | |
HttpClient client = new DefaultHttpClient(); | |
HttpGet request = new HttpGet(url); | |
// add request header | |
request.addHeader("User-Agent", USER_AGENT); | |
HttpResponse response = client.execute(request); | |
System.out.println("\nSending 'GET' request to URL : " + url); | |
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.toString()); | |
} | |
// HTTP POST request | |
private void sendPost() throws Exception { | |
String url = "URL"; | |
HttpClient client = new DefaultHttpClient(); | |
HttpPost post = new HttpPost(url); | |
// add header | |
post.setHeader("User-Agent", USER_AGENT); | |
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); | |
urlParameters.add(new BasicNameValuePair("name", "example")); | |
post.setEntity(new UrlEncodedFormEntity(urlParameters)); | |
HttpResponse response = client.execute(post); | |
System.out.println("\nSending 'POST' request to URL : " + url); | |
System.out.println("Post parameters : " + post.getEntity()); | |
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.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment