Code example for HttpURLConnection

Methods: connect, getErrorStream, getInputStream, getOutputStream, getResponseCode, getResponseMessage, setDoOutput, setRequestMethod, setRequestProperty

0
	} 
 
	public static String doPost(URL url, InputStream stuffToPost, String contentType) throws IOException {
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		setUserAgent(conn);
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		if (contentType != null) {
			conn.setRequestProperty("Content-Type", contentType);
		} 
		OutputStream ostr = null;
		try { 
			ostr = conn.getOutputStream();
			copy(stuffToPost, ostr);
		} finally { 
			if (ostr != null)
				ostr.close();
		} 
 
		conn.connect();
		BufferedReader reader = null;
		try { 
			int rc = conn.getResponseCode();
			InputStream contentStream = null;
			if (rc == 400)
				contentStream = conn.getErrorStream();
			else if (rc != 200) {
				throw new IOException("code " + rc + " '" + conn.getResponseMessage() + "'");
			} else 
				contentStream = conn.getInputStream();
			reader = new BufferedReader(new InputStreamReader(contentStream), 512);
			String response = toString(reader);
			return response;
		} finally { 
			if (reader != null) {