Java tutorial
package aop.controller; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import static org.apache.http.HttpHeaders.USER_AGENT; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; 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.BasicNameValuePair; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author tony */ public class RequestController { public void sendPostReq(int id, String type) throws Exception { boolean debug = false; String url = "http://localhost:1337/"; CloseableHttpClient httpClient = HttpClients.createDefault(); try { HttpPost post = new HttpPost(url); // add header post.setHeader("User-Agent", USER_AGENT); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("id", Integer.toString(id))); urlParameters.add(new BasicNameValuePair("type", type)); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = httpClient.execute(post); if (debug) { 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()); } } finally { httpClient.close(); } } }