Code example for URLConnection

Methods: addRequestProperty, getContentEncoding, getContentType, getInputStream, getOutputStream, setDoOutput, x 1

0
				ArrayList<ToSyncElementsHolder> elementsToSync,
				GoogleAuthentication googleAuthentication)
				throws ClientProtocolException, IOException { 
			URL url = new URL(gaeUrl + "sync");
			URLConnection conn = url.openConnection();
			conn.addRequestProperty("Content-Type",
					"application/json; charset=UTF-8"); 
			conn.addRequestProperty("Content-Encoding", "gzip");
			conn.addRequestProperty("Cookie", googleAuthentication
					.getAuthCookie().getName() 
					+ "=" 
					+ googleAuthentication.getAuthCookie().getValue());
			conn.addRequestProperty("User-Agent",
					"Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"); 
			conn.addRequestProperty("Accept",
					"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
			conn.addRequestProperty("Accept-Language",
					"pl,en-us;q=0.7,en;q=0.3"); 
			conn.addRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
			conn.addRequestProperty("Accept-Charset", "utf-8;q=0.7,*;q=0.7");
			conn.addRequestProperty("Cache-Control", "no-cache");
			conn.addRequestProperty("Pragma", "no-cache");
			// this do post method 
			conn.setDoOutput(true);
			OutputStream outputStream = conn.getOutputStream();
			GZIPOutputStream gzipOutputStream = new GZIPOutputStream(
					outputStream);
			OutputStreamWriter out = new OutputStreamWriter(gzipOutputStream);
			new JSONSerializer().prettyPrint(true).include("*") 
					.serialize(elementsToSync, out);
			out.close();
			gzipOutputStream.close();
			InputStream inputStream = null;
			inputStream = conn.getInputStream();
			String encoding = conn.getContentEncoding();
			String charset = conn.getContentType();
			int startIndex = charset.indexOf("charset=") + 8;
			String word = charset.substring(startIndex).split(" |\"\"")[0];
			if (word == null) {
				word = "utf-8";
			}