Created
October 27, 2010 04:14
-
-
Save ironcamel/648423 to your computer and use it in GitHub Desktop.
This is a comparison of making a simple http request with basic/digest authentication in perl and java. I was porting a perl app to the android platform (java) and was struck by the different amount of complexity and effort required.
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
use LWP::UserAgent; | |
my ($host, $realm, $user, $pass, $uri) = @ARGV; | |
my $agent = LWP::UserAgent->new()->credentials($host, $realm, $user, $pass); | |
print $agent->get($uri)->content(); |
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.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.impl.client.BasicCredentialsProvider; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.HttpRequest; | |
import org.apache.http.HttpResponse; | |
public class SimpleRequest { | |
public static void main(String[] args) { | |
String host = args[0]; | |
int port = Integer.parseInt(args[1]); | |
String realm = args[2]; | |
String username = args[3]; | |
String password = args[4]; | |
String uri = args[5]; | |
UsernamePasswordCredentials creds = new UsernamePasswordCredentials( | |
username, password); | |
AuthScope authScope = new AuthScope(host, port, realm); | |
BasicCredentialsProvider credProvider = new BasicCredentialsProvider(); | |
credProvider.setCredentials(authScope, creds); | |
DefaultHttpClient client = new DefaultHttpClient(); | |
client.setCredentialsProvider(credProvider); | |
HttpGet request = new HttpGet(uri); | |
try { | |
HttpResponse response = client.execute(request); | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(response.getEntity().getContent())); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
sb.append(line); | |
} | |
System.out.println(sb.toString())); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@javazquez, that is indeed very neat. I will have to look into Groovy at some point.
@heller, thanks for the code! I think I will use your HttpGetter class in the app I am working on. It's definitely better to abstract away all that crud. At least I won't have to look at it. I noticed this was your first github project. I wish you many more.