I have write a simple RESTful web service using basic authentication.I use tips from this page secureRestWS. They have also created a video and posted on youtube with more details. It works just fine when you open it in browser. You need to write User Name and Password in authorization window.
I have also find a page with android client that call rest web service using httpclient and if the web service is without authentication it works. I am using this example androidRestWSClient. But I don`t know how to add User Name and Password in this scenario, I tried like :
client.AddParam("User Name", "myusername");
client.AddParam("Password", "mypassword");
or in header :
client.AddHeader("User Name", "myusername");
client.AddHeader("Password", "mypassword");
but nothing works. I also try to create url that tell the web service username and password like :
http://192.168.1.42/RestWS/resources/helloWorld?username=myusername&password=mypassword
I really don`t have a solution for this so if somebody have an example client I would appreciate that.
In webservice I have simple GET method
@GET
@Path("/text")
public String getText() {
return "Hello World!";
}
Do I need to use SecurityContext for username and password? Is it better to create authentication manualy like in this example :
http://aruld.info/accessing-restful-services-configured-with-ssl-using-resttemplate/
Thanks for your help
If you are using just basic http authentication, then the URL should look like this:
http://username:[email protected]/RestWS/resources/helloWorld
Well, here you go:
HttpGet httpget;
try{
httpget = new HttpGet(url);
String auth =new String(Base64.encode(( username + ":" + password).getBytes(),Base64.URL_SAFE|Base64.NO_WRAP));
httpget.addHeader("Authorization", "Basic " + auth);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With