Java tutorial
/** * Client tests for Timestreams2 * Author Jesse Blum (JMB) * Produced for the Relate Project, Horizon Digital Economy Institute, University of Nottingham This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Random; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import static org.junit.Assert.*; import org.apache.commons.codec.binary.Hex; import org.junit.Test; /** * Unit tests * * @author pszjmb * @todo test additional parameters of /measurement_container/[id] */ public class TimestreamsTests { // BASEURL should be the Wordpress Timestreams API being tested private final String BASEURL = "http://192.168.56.101/wordpress/wp-content/plugins/timestreams/2"; private String fail = null; // PUBKEY and PRIKEY values should be present in the Timestreams API Key // list private final String PUBKEY = "c21fa479e5"; private final String PRIKEY = "95245d48761fe8a77dc6c6bec82e09f3"; /** * Performs HTTP get for a given URL * * @param url * is the URL to get * @return a String with the contents of the get */ private Map<String, List<String>> doGet(URL url) { try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); Map<String, List<String>> responseHeaderFields = conn.getHeaderFields(); System.out.println(responseHeaderFields); if (responseHeaderFields.get(null).get(0).equals("HTTP/1.1 200 OK")) { InputStreamReader in = new InputStreamReader((InputStream) conn.getContent()); BufferedReader buff = new BufferedReader(in); String line; do { line = buff.readLine(); System.out.println(line + "\n"); } while (line != null); } return responseHeaderFields; } catch (IOException e) { fail = e.getLocalizedMessage(); return null; } } /** * Performs HTTP post for a given URL * * @param url * is the URL to get * @param params * is a URL encoded string in the form x=y&a=b... * @return a String with the contents of the get */ private Map<String, List<String>> doPost(URL url, String params) { HttpURLConnection connection; try { connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length)); connection.setDoInput(true); connection.setDoOutput(true); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(params); wr.flush(); wr.close(); Map<String, List<String>> responseHeaderFields = connection.getHeaderFields(); System.out.println(responseHeaderFields); if (responseHeaderFields.get(null).get(0).equals("HTTP/1.1 200 OK")) { InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); System.out.println(response); } return responseHeaderFields; } catch (IOException e1) { fail("Post " + url + " failed: " + e1.getLocalizedMessage()); } return null; } /** * Performs HTTP post for a given URL * * @param url * is the URL to get * @param params * is a URL encoded string in the form x=y&a=b... * @return a String with the contents of the get */ private Map<String, List<String>> doPut(URL url, String params) { HttpURLConnection connection; try { connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length)); connection.setDoInput(true); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(params); out.close(); Map<String, List<String>> responseHeaderFields = connection.getHeaderFields(); System.out.println(responseHeaderFields); if (responseHeaderFields.get(null).get(0).equals("HTTP/1.1 200 OK")) { InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); System.out.println(response); } return responseHeaderFields; } catch (IOException e1) { fail("Post " + url + " failed: " + e1.getLocalizedMessage()); } return null; } /** * Fail a get test if not 200 OK * * @param url */ private void getTest(URL url) { System.out.println(url); Map<String, List<String>> get = doGet(url); if (null == get) { fail("Get " + url + " failed: " + fail); } else if (!get.get(null).get(0).equals("HTTP/1.1 200 OK")) { fail(get.get(null).get(0)); } } /** * Fail a get test if 200 OK This is used to test missing parameters in * requests * * @param url */ private void getFailureTest(URL url) { System.out.println(url); Map<String, List<String>> get = doGet(url); if (null == get) { fail("Get " + url + " failed: " + fail); } else if (get.get(null).get(0).equals("HTTP/1.1 200 OK")) { fail(get.get(null).get(0)); } } /** * Fail a post test if not 200 OK * * @param url */ private void postTest(URL url, String params) { System.out.println(url); Map<String, List<String>> post = doPost(url, params); if (null == post) { fail("Post " + url + " failed: " + fail); } else if (!post.get(null).get(0).equals("HTTP/1.1 200 OK")) { fail(post.get(null).get(0)); } } /** * Fail a get test if 200 OK This is used to test missing parameters in * requests * * @param url */ private void postFailureTest(URL url, String params) { System.out.println(url); Map<String, List<String>> post = doPost(url, params); if (null == post) { fail("Post " + url + " failed: " + fail); } else if (post.get(null).get(0).equals("HTTP/1.1 200 OK")) { fail(post.get(null).get(0)); } } /** * Fail a post test if not 200 OK * * @param url */ private void putTest(URL url, String params) { System.out.println(url); Map<String, List<String>> put = doPut(url, params); if (null == put) { fail("Post " + url + " failed: " + fail); } else if (!put.get(null).get(0).equals("HTTP/1.1 200 OK")) { fail(put.get(null).get(0)); } } /** * Fail a get test if 200 OK This is used to test missing parameters in * requests * * @param url */ private void putFailureTest(URL url, String params) { System.out.println(url); Map<String, List<String>> put = doPut(url, params); if (null == put) { fail("Post " + url + " failed: " + fail); } else if (put.get(null).get(0).equals("HTTP/1.1 200 OK")) { fail(put.get(null).get(0)); } } /** * Generate an HMAC Based on example: * http://stackoverflow.com/questions/6312544 * /hmac-sha1-how-to-do-it-properly-in-java * * @param value * is a String to hash * @param key * is the private key to hash with# * @param type * is the Mac format to use such as HmacSHA256 * @return The hmac */ private String hmacString(String value, String key, String type) { try { byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, type); // Get a Mac instance and initialize with the signing key Mac mac = Mac.getInstance(type); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String return new String(hexBytes, "UTF-8"); } catch (Exception e) { fail = e.getLocalizedMessage(); return null; } } /** * Tests calling the API documentation page */ @Test public void testGetDocumentation() { try { URL url = new URL(BASEURL); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling the API time */ @Test public void testGetTime() { try { URL url = new URL(BASEURL + "/time"); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Returns a HMAC for given parameters * * @param params * are all the parameters to hash * @return the HMAC as a String */ private String getSecurityString(List<String> params) { java.util.Collections.sort(params); String toHash = ""; ListIterator<String> it = params.listIterator(); while (it.hasNext()) { toHash += it.next() + "&"; } System.out.println(toHash); return hmacString(toHash, PRIKEY, "HmacSHA256"); } /** * Creates a HMAC for given parameters and returns a URL with security * parameters * * @param url * is the URL to add the security parameters to * @param params * are all the parameters to hash * @param now * is a UTC timestamp * @return the URL with security parameters */ private String getSecurityString(String url, List<String> params, String now) { java.util.Collections.sort(params); String toHash = ""; ListIterator<String> it = params.listIterator(); while (it.hasNext()) { toHash += it.next(); } String hmac = hmacString(toHash, PRIKEY, "HmacSHA256"); String urlStr = url + "?pubkey=" + PUBKEY + "&now=" + now; urlStr += "&hmac=" + hmac; return urlStr; } /** * Tests calling /metadata with valid parameters */ @Test public void testGetMetadataLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); String urlStr = getSecurityString(BASEURL + "/metadata", params, now); URL url = new URL(urlStr); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /metadata with missing security parameters */ @Test public void testGetMetadataMissingSecurity() { try { URL url = new URL(BASEURL + "/metadata"); getFailureTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /metadata?tsid=[id] with correct parameters for the URL * being tested against. */ @Test public void testGetMetadataIdLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String id = "1"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(id); String urlStr = getSecurityString(BASEURL + "/metadata", params, now); URL url = new URL(urlStr + "&tsid=" + id); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /metadata/id with valid parameters for the URL being tested * against. * Note: id should be an existing measurement container table name */ @Test public void testGetMetadataContainerLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String id = "wp_1_ts_temperature_1"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); String urlStr = getSecurityString(BASEURL + "/metadata/" + id, params, now); URL url = new URL(urlStr); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /measurement_containers with valid parameters for the URL * being tested against. */ @Test public void testGetMCLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); String urlStr = getSecurityString(BASEURL + "/measurement_containers", params, now); URL url = new URL(urlStr); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /measurement_container/[id] with valid parameters for the * URL being tested against. Note that id should be a valid measurement * container table name */ @Test public void testGetMCIdLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String id = "wp_1_ts_Pressure_25"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); String urlStr = getSecurityString(BASEURL + "/measurement_container/" + id, params, now); URL url = new URL(urlStr); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /measurement_container/[id]?action=first with valid * parameters for the URL being tested against. Note that id should be a * valid measurement container table name */ @Test public void testGetMCIdFirstLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String id = "wp_1_ts_Pressure_25"; String action = "first"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(action); String urlStr = getSecurityString(BASEURL + "/measurement_container/" + id, params, now); URL url = new URL(urlStr + "&action=" + action); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /measurement_container/[id]?action=latest with valid * parameters for the URL being tested against. Note that id should be a * valid measurement container table name */ @Test public void testGetMCIdLatestLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String id = "wp_1_ts_Pressure_25"; String action = "latest"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(action); String urlStr = getSecurityString(BASEURL + "/measurement_container/" + id, params, now); URL url = new URL(urlStr + "&action=" + action); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /measurement_container/[id]?action=count with valid * parameters for the URL being tested against. Note that id should be a * valid measurement container table name */ @Test public void testGetMCIdCountLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String id = "wp_1_ts_Pressure_25"; String action = "count"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(action); String urlStr = getSecurityString(BASEURL + "/measurement_container/" + id, params, now); URL url = new URL(urlStr + "&action=" + action); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /context with valid parameters for the URL being tested * against. */ @Test public void testGetContextLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); String urlStr = getSecurityString(BASEURL + "/context", params, now); URL url = new URL(urlStr); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /context with valid parameters for the URL being tested * against. */ @Test public void testGetTimestreamsLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); String urlStr = getSecurityString(BASEURL + "/timestreams", params, now); URL url = new URL(urlStr); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /timestream/id/[id] with valid parameters for the URL being * tested against. Note that id should be a valid timestream id */ @Test public void testGetTimestreamsIdLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String id = "1"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); String urlStr = getSecurityString(BASEURL + "/timestream/id/" + id, params, now); URL url = new URL(urlStr); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /timestream/name/[name] with valid parameters for the URL * being tested against. Note that id should be a valid mc table name */ @Test public void testGetTimestreamsNameLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String id = "wp_1_ts_temperature_23"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); String urlStr = getSecurityString(BASEURL + "/timestream/name/" + id, params, now); URL url = new URL(urlStr); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests calling /timestream/head/[id] with valid parameters for the URL * being tested against. Note that id should be a valid head id */ @Test public void testGetTimestreamsHeadIdLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String id = "1"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); String urlStr = getSecurityString(BASEURL + "/timestream/head/" + id, params, now); URL url = new URL(urlStr); getTest(url); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } } /** * Tests posting to /measurement_container with valid parameters */ @Test public void testPostMCLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String name = "testPostMCLegit" + new Random().nextInt(); String measuretype = "testPostMCLegit"; String unit = "unit=text/x-data-test"; String device = "testDev"; String datatype = "DECIMAL(5,2)"; String siteid = "1"; String blogid = "1"; String userid = "1"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(name); params.add(measuretype); params.add(unit); params.add(device); params.add(datatype); params.add(siteid); params.add(blogid); params.add(userid); String hmac = getSecurityString(params); String paramsstr = "name=" + URLEncoder.encode(name, "UTF-8") + "&measuretype=" + URLEncoder.encode(measuretype, "UTF-8") + "&unit=" + URLEncoder.encode(unit, "UTF-8") + "&device=" + URLEncoder.encode(device, "UTF-8") + "&datatype=" + URLEncoder.encode(datatype, "UTF-8") + "&siteid=" + URLEncoder.encode(siteid, "UTF-8") + "&blogid=" + URLEncoder.encode(blogid, "UTF-8") + "&userid=" + URLEncoder.encode(userid, "UTF-8") + "&pubkey=" + URLEncoder.encode(PUBKEY, "UTF-8") + "&now=" + URLEncoder.encode(now, "UTF-8") + "&hmac=" + URLEncoder.encode(hmac, "UTF-8"); // System.out.println("paramsstr: " + paramsstr); URL url = new URL(BASEURL + "/measurement_container"); postTest(url, paramsstr); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } catch (UnsupportedEncodingException e) { fail(e.getLocalizedMessage()); } } /** * Tests posting to /measurement/:name with valid parameters * Note that :name should exist in the database being tested */ @Test public void testPostMeasurementLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String name = "wp_1_ts_testPostMCLegit_60"; String value = "1.0"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(value); String hmac = getSecurityString(params); String paramsstr = "value=" + URLEncoder.encode(value, "UTF-8") + "&pubkey=" + URLEncoder.encode(PUBKEY, "UTF-8") + "&now=" + URLEncoder.encode(now, "UTF-8") + "&hmac=" + URLEncoder.encode(hmac, "UTF-8"); ; // System.out.println("paramsstr: " + paramsstr); URL url = new URL(BASEURL + "/measurement/" + name); postTest(url, paramsstr); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } catch (UnsupportedEncodingException e) { fail(e.getLocalizedMessage()); } } /** * Tests posting to /measurements/:name with valid parameters * Note that :name should exist in the database being tested */ @Test public void testPostMeasurementsLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String name = "wp_1_ts_testPostMCLegit_60"; String measurements = "{\"measurements\":[{\"v\":1,\"t\":" + "\"2012-11-09 12:10:23\"}," + "{\"v\":2,\"t\":\"2012-07-21 17:10:23\"}]}"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(name); params.add(measurements); String hmac = getSecurityString(params); String paramsstr = "name=" + URLEncoder.encode(name, "UTF-8") + "&measurements=" + URLEncoder.encode(measurements, "UTF-8") + "&pubkey=" + URLEncoder.encode(PUBKEY, "UTF-8") + "&now=" + URLEncoder.encode(now, "UTF-8") + "&hmac=" + URLEncoder.encode(hmac, "UTF-8"); ; // System.out.println("paramsstr: " + paramsstr); URL url = new URL(BASEURL + "/measurements/" + name); postTest(url, paramsstr); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } catch (UnsupportedEncodingException e) { fail(e.getLocalizedMessage()); } } /** * Tests posting to /context with valid parameters */ @Test public void testPostContextLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String type = "place"; String value = "Nottingham"; String start = "2012-11-12 10:10:23"; String end = "2012-11-12 10:20:23"; String user = "1"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(type); params.add(value); params.add(start); params.add(end); params.add(user); String hmac = getSecurityString(params); String paramsstr = "type=" + URLEncoder.encode(type, "UTF-8") + "&value=" + URLEncoder.encode(value, "UTF-8") + "&start=" + URLEncoder.encode(start, "UTF-8") + "&end=" + URLEncoder.encode(end, "UTF-8") + "&user=" + URLEncoder.encode(user, "UTF-8") + "&pubkey=" + URLEncoder.encode(PUBKEY, "UTF-8") + "&now=" + URLEncoder.encode(now, "UTF-8") + "&hmac=" + URLEncoder.encode(hmac, "UTF-8"); ; // System.out.println("paramsstr: " + paramsstr); URL url = new URL(BASEURL + "/context"); postTest(url, paramsstr); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } catch (UnsupportedEncodingException e) { fail(e.getLocalizedMessage()); } } /** * Tests posting to /context with valid parameters */ @Test public void testPutContextLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String value = "Nottingham"; String end = "2011-11-12 10:20:21"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(value); params.add(end); String hmac = getSecurityString(params); String paramsstr = "&value=" + URLEncoder.encode(value, "UTF-8") + "&end=" + URLEncoder.encode(end, "UTF-8") + "&pubkey=" + URLEncoder.encode(PUBKEY, "UTF-8") + "&now=" + URLEncoder.encode(now, "UTF-8") + "&hmac=" + URLEncoder.encode(hmac, "UTF-8"); ; //System.out.println("paramsstr: " + paramsstr); URL url = new URL(BASEURL + "/context"); putTest(url, paramsstr); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } catch (UnsupportedEncodingException e) { fail(e.getLocalizedMessage()); } } /** * Tests posting to /metadata/heartbeat/:name with valid parameters * Note that the name should be a table in the database */ @Test public void testPutHeartbeatLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String ip = "192.168.56.101"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(ip); String hmac = getSecurityString(params); String paramsstr = "&ip=" + URLEncoder.encode(ip, "UTF-8") + "&pubkey=" + URLEncoder.encode(PUBKEY, "UTF-8") + "&now=" + URLEncoder.encode(now, "UTF-8") + "&hmac=" + URLEncoder.encode(hmac, "UTF-8"); //System.out.println("paramsstr: " + paramsstr); URL url = new URL(BASEURL + "/metadata/heartbeat/wp_1_ts_testPostMCLegit_60"); putTest(url, paramsstr); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } catch (UnsupportedEncodingException e) { fail(e.getLocalizedMessage()); } } /** * Tests posting to /timestream/head/:id with valid parameters * Note that the id should be an existing playhead */ @Test public void testPutPlayheadLegit() { try { long time = new Date().getTime(); String now = Long.toString(time).substring(0, 10); String curtime = "1352315402"; String start = "1352315400"; String end = "1352315403"; String rate = "3"; List<String> params = new ArrayList<String>(); params.add(PUBKEY); params.add(now); params.add(curtime); params.add(start); params.add(end); params.add(rate); String hmac = getSecurityString(params); String paramsstr = "&curtime=" + URLEncoder.encode(curtime, "UTF-8") + "&start=" + URLEncoder.encode(start, "UTF-8") + "&end=" + URLEncoder.encode(end, "UTF-8") + "&rate=" + URLEncoder.encode(rate, "UTF-8") + "&pubkey=" + URLEncoder.encode(PUBKEY, "UTF-8") + "&now=" + URLEncoder.encode(now, "UTF-8") + "&hmac=" + URLEncoder.encode(hmac, "UTF-8"); ; //System.out.println("paramsstr: " + paramsstr); URL url = new URL(BASEURL + "/timestream/head/1"); putTest(url, paramsstr); } catch (MalformedURLException e) { fail(e.getLocalizedMessage()); } catch (UnsupportedEncodingException e) { fail(e.getLocalizedMessage()); } } }