Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

httpget request with auth

Tags:

java

android

http

I tried to make http get request with code:

String username = "test\\v100";
String host = "1.2.3.4";
String password = "pass";

HttpClient client = new DefaultHttpClient();

AuthScope as = new AuthScope(host, 90);
UsernamePasswordCredentials upc = new UsernamePasswordCredentials(username, password);

((AbstractHttpClient) client).getCredentialsProvider().setCredentials(as, upc);

BasicHttpContext localContext = new BasicHttpContext();

BasicScheme basicAuth = new BasicScheme();
localContext.setAttribute("preemptive-auth", basicAuth);

HttpHost targetHost = new HttpHost(host, 90, "http");

HttpGet httpget = new HttpGet("/");

HttpResponse response = client.execute(targetHost, httpget, localContext);

But get an exception "java.net.SocketException: Permission denied" with the last line.

Android-2.2 with eclipse IDE.

Curl request in the host system

curl -u test\v100:pass "http://1.2.3.4:90"

works fine.

How can i make http request in the right way?

Thanks!

like image 960
user1312837 Avatar asked Apr 04 '12 12:04

user1312837


1 Answers

Have you added the internet permission in your manifest file?

If not, add this line in your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" /> 
like image 55
Bart Avatar answered Sep 20 '22 09:09

Bart