Created
February 8, 2011 12:37
-
-
Save dogeared/816380 to your computer and use it in GitHub Desktop.
Replacement code in NodeService
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
public String post(Service serviceName, Map<String, String> params) throws IOException { | |
StringBuffer sb = new StringBuffer(); | |
for (String key:params.keySet()) { | |
String value = params.get(key); | |
sb.append(key+"="+value+"&"); | |
} | |
String urlParameters = sb.substring(0,sb.length()-1); | |
URL url; | |
HttpURLConnection connection = null; | |
StringBuffer response = new StringBuffer(); | |
url = new URL(config.getProperty(serviceName.value)); | |
connection = (HttpURLConnection)url.openConnection(); | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); | |
connection.setRequestProperty("Content-Language", "en-US"); | |
connection.setUseCaches (false); | |
connection.setDoInput(true); | |
connection.setDoOutput(true); | |
//Send request | |
DataOutputStream wr = new DataOutputStream (connection.getOutputStream()); | |
wr.writeBytes(urlParameters); | |
wr.flush(); | |
wr.close(); | |
//Get Response | |
InputStream is = connection.getInputStream(); | |
BufferedReader rd = new BufferedReader(new InputStreamReader(is)); | |
String line; | |
while((line = rd.readLine()) != null) { | |
response.append(line); | |
response.append('\r'); | |
} | |
rd.close(); | |
if(connection != null) { | |
connection.disconnect(); | |
} | |
return response.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment