views:

723

answers:

3

Hello,

I have problems getting the HTML code of a website by using this example code.

http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java

I have to use the above one, as I need preemptive authentication for my application in Android.

I thought the solution might be the httpget object, but I still get only errors :( c.f. HttpGet httpget = new HttpGet("/index.html");

Does anybody have an idea how to get the content of the file, which is specified in the httpget object above using the example code of the link. It definitely called, but for now I can only get status code and so on ...

Thanks 4 help

+1  A: 

When I tackled this last year, I gave up on HttpClient's native pre-emptive HTTP authentication and just rolled the header myself.

CommonsWare
A: 

Alternative 1: Please read Http Basic Authentication with Android that proposes a solution based on the HttpClient 4 official docs. I've not tested it by myself, so I'd be happy to know if it really works.

Edit: I've just tried it and it works like a charm.

Alternative 2: You can also add the "Authorization" HTTP header as proposed by @CommonsWare:

post.addHeader("Authorization", "Basic " + Base64.encode(username+":"+password));

In this case you need a Base64 encoder to encode the string containing the username and the password. You can find a lot of implementations in the Internet.

Guido
A: 

hi, I'm new on the android. I want to http authentication. I used httpClient, but ı couldnt find any way to add he username and password. Also I have to convert the password on the md5 format. Could you give any way? I research lots of forum but I coulnt find any solution way?

filiz