import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;


public class Main {

	private final String USER_AGENT = "Mozilla/5.0";
	 
	public static void main(String[] args) throws Exception {
 
		Main http = new Main();

 
		System.out.println("\nTesting 2 - Send Http POST request");
		http.sendPost();
 
	}

 
	// HTTP POST request
	private void sendPost() throws Exception {
 
		String url = "http://www.liste-serveur-minecraft.net/serveur.php?id=1535";
		URL obj = new URL(url);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 
		//add reuqest header
		con.setRequestMethod("POST");
		con.setRequestProperty("User-Agent", USER_AGENT);
		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
		con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//		con.setRequestProperty("Accept-Encoding", "gzip, deflate");
		con.setRequestProperty("Accept-Language", "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4");
		con.setRequestProperty("Cache-Control", "max-age=0");
		con.setRequestProperty("Connection", "keep-alive");
		con.setRequestProperty("Content-Length", "19");
		con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		//con.setRequestProperty("Cookie", "PHPSESSID=095mvmh20jhkeuhvte5rc4ogc0; _ga=GA1.2.442711901.1421964604");
		con.setRequestProperty("Host", "www.liste-serveur-minecraft.net");
		con.setRequestProperty("Origin", "http://www.liste-serveur-minecraft.net");
		con.setRequestProperty("Referer", "http://www.liste-serveur-minecraft.net/serveur.php?id=1535");
		con.setRequestProperty("User-Agent", "Mozilla/5.0");
		String urlParameters = "captchaCode=4&vote=";
 
		// Send post request
		con.setDoOutput(true);
		 
		
		DataOutputStream wr = new DataOutputStream(con.getOutputStream());
		wr.writeBytes(urlParameters);
		wr.flush();
		wr.close();

		System.out.println(con.getHeaderFields());
		int responseCode = con.getResponseCode();
		System.out.println("\nSending 'POST' request to URL : " + url);
		System.out.println("Post parameters : " + urlParameters);
		System.out.println("Response Code : " + responseCode);
 
		BufferedReader in = new BufferedReader(
		        new InputStreamReader(con.getInputStream()));
		String inputLine;
		StringBuffer response = new StringBuffer();
 
		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();
 
		//print result
		System.out.println(response.toString().replace("    ", "\n"));
 
	}
 
}
