Skip to content

Instantly share code, notes, and snippets.

@ironcamel
Created October 27, 2010 04:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ironcamel/648423 to your computer and use it in GitHub Desktop.
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.
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();
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);
}
}
}
@leto
Copy link

leto commented Oct 27, 2010

That can be golfed a bit:

use LWP::UserAgent;
my ($host, $realm, $user, $pass) = @argv;
my $agent = LWP::UserAgent->new->credentials($host, $realm, $user, $pass);
print $agent->get($uri)->content;

@rbuels
Copy link

rbuels commented Oct 27, 2010

oh this is lovely

@ironcamel
Copy link
Author

Thanks @leto. I applied your suggestion.

@egonw
Copy link

egonw commented Oct 27, 2010

This is comparing HTTP client libraries... and your Perl code does not have the host port defined...

@ironcamel
Copy link
Author

@egonw, the credentials method expects $host to be of the form domain[:port], e.g., "foo.com:8080".

@heller
Copy link

heller commented Oct 27, 2010

Crappy apache libs are crappy. You're so clever.

@ironcamel
Copy link
Author

@heller, a more constructive comment would be to point out a nicer library and maybe short code example. My impression was that apache libs are the gold standard for doing web stuff in java. I was motivated to post this as I was developing a mobile app for Android and the apache libs are what the Android API provide. So they must not be that crappy.

@heller
Copy link

heller commented Oct 28, 2010

@ironcamel You can't take the high road after starting a "Java sucks" dogpile.

@javazquez
Copy link

Here is a quick groovy 1.7.4 Basic Auth Example
@grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0' )

def authSite = new groovyx.net.http.HTTPBuilder( 'http://10.110.201.115/~juanvazquez/basicAuth/' )
authSite.auth.basic 'test', 'this'
println authSite.get( path:'testAuth.html' )

@heller
Copy link

heller commented Oct 28, 2010

By way of reconciliation, I made a couple small utilities which are hopefully helpful to you (or at least mildly entertaining). Cheers!

http://github.com/heller/Java-Utils/blob/master/src/com/whatevermatt/net/CliHttpGet.java

@ironcamel
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment