Created
June 9, 2010 23:29
-
-
Save sandyarmstrong/432356 to your computer and use it in GitHub Desktop.
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
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