Created
November 1, 2010 15:32
-
-
Save tomash/658360 to your computer and use it in GitHub Desktop.
OpenKeyVal simple getting and setting in plainest Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
import java.io.*; | |
public class HelloOpenKeyVal { | |
static String base_url = "http://openkeyval.org/"; | |
public static void main(String[] args) throws Exception { | |
//System.out.println(get("cowboytomash10")); | |
//System.out.println(post("cowboytomash10", "what the fuck, man")); | |
} | |
public static String get(String key) throws Exception { | |
String returned = ""; | |
URL okv = new URL(base_url + key); | |
URLConnection conn = okv.openConnection(); | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader( | |
conn.getInputStream())); | |
String inputLine; | |
while ((inputLine = in.readLine()) != null) | |
returned += inputLine; | |
in.close(); | |
return returned; | |
} | |
public static String post(String key, String value) throws Exception { | |
String returned = ""; | |
// Construct data | |
String data = URLEncoder.encode("data", "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8"); | |
System.out.println(data); | |
// Send data | |
URL url = new URL(base_url + key); | |
URLConnection conn = url.openConnection(); | |
conn.setDoOutput(true); | |
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); | |
wr.write(data); | |
wr.flush(); | |
// Get the response | |
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
String inputLine; | |
while ((inputLine = rd.readLine()) != null) | |
returned += inputLine; | |
wr.close(); | |
rd.close(); | |
return returned; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment