Skip to content

Instantly share code, notes, and snippets.

@sandyarmstrong
Created June 9, 2010 23:29
Show Gist options
  • Save sandyarmstrong/432356 to your computer and use it in GitHub Desktop.
Save sandyarmstrong/432356 to your computer and use it in GitHub Desktop.
package com.novell.webyast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
public class RestClient {
// FIXME: JUnit this
public String getMethod(final String scheme, final String hostname, final int port,
final String resourcePath, final String user, final String pass)
throws Exception {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass.toCharArray());
}
});
HttpURLConnection c = (HttpURLConnection) new URL(scheme,
hostname, port, resourcePath).openConnection();
c.setUseCaches(false);
c.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(c
.getInputStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null)
sb.append(inputLine + "\n");
in.close();
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment