import java.net.* ;
import java.io.* ; 

public class SimplePost {

    // This is a very simple example of how to do an HTTP Post with a single parameter.
	public static String doPost(String baseURL, String path, String name, String value) 
		throws FileNotFoundException, java.io.UnsupportedEncodingException, MalformedURLException, IOException  {
		// new URL() might throw MalformedURLException
		// new URL() might throw IOException
		URL url = new URL(baseURL + path) ;
		URLConnection urlc = url.openConnection() ;
		urlc.setDoOutput(true) ;
		urlc.setDoInput(true) ;
		urlc.setAllowUserInteraction(false) ;
		// URLConnection.getOutputStream might throw IOException
		PrintWriter server = new PrintWriter(urlc.getOutputStream()) ;
		// URLEncoder.encode might throw UnsupportedEncodingException
		String parameterString = URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8") ;
		server.print(parameterString) ;
		server.flush() ;
		server.close() ;
		InputStream in ;
		// URLConnection.getInputStream might throw FileNotFoundException
		// URLConnection.getInputStream might throw IOException
		in = urlc.getInputStream();
		// streamToString might throw IOException
		String response = streamToString(in) ;
		return response;
	}

	// This should really use 
	public static String streamToString(InputStream in) throws IOException {
		BufferedReader bis = new BufferedReader(new InputStreamReader(in));
		StringBuffer out= new StringBuffer();
		char[] inputLine = new char[2048];
		int count = bis.read(inputLine);
		while ( count > 0){
			out.append(inputLine,0,count);
			count = bis.read(inputLine);
		}
		return out.toString();
	}
}

