user173488
user173488

Reputation: 947

android, how to send username and password to webservice as header fields?

i am trying to create an application on android phone that takes username and password from user, encrypt the password using md5 then connect to url with these parameters.

a code to connect worked fine on iphone, but i couldn't find something like it in android:

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:myURL];

[request setHTTPMethod:@"GET"];

[request addValue:usernameField.text forHTTPHeaderField:@"UserName"];

[request addValue:MD5Pass2 forHTTPHeaderField:@"Password"];

i tried to connect via httpurlconnection used post/get Dataoutputstream, httpclient send parameters httpget/httppost, but no success.

I think I need to send the parameters as headerfield but I don't know how.

note: I compared encryption results and it was correct.

Upvotes: 0

Views: 4478

Answers (2)

Matthew Patience
Matthew Patience

Reputation: 385

I find the Apache library to be much more straightforward when it comes to HTTP.

An example of this would be as follows:

DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://www.internet.com/api"));

Normally with a GET request you would use GET parameters, ie append them to the end of the URL like so:

String url = "http://www.internet.com/api?UserName=YourUsername&Password=yourpassword" 
request.setURI(new URI(url));

But since you specified you want them as headers you could:

request.addHeader("UserName", username);
request.addHeader("Password", password);

and then:

HttpResponse response = client.execute(request);
//Parse the response from the input stream object inside the HttpResponse

Upvotes: 5

Related Questions