-
-
Save ironcamel/648423 to your computer and use it in GitHub Desktop.
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); | |
} | |
} | |
} |
@egonw, the credentials method expects $host to be of the form domain[:port], e.g., "foo.com:8080".
Crappy apache libs are crappy. You're so clever.
@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.
@ironcamel You can't take the high road after starting a "Java sucks" dogpile.
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' )
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
@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.
This is comparing HTTP client libraries... and your Perl code does not have the host port defined...