0

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.

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, your answer is clear and comprehensive, i tried it, and although i thought it will work (theoretically it must) but it did not, i am still getting the same result "login failed"!
at last i found the bug, they gave me a wrong username! now it is working, thank you.
1

you can try using HttpUrlConnection with this http://developer.android.com/reference/java/net/URLConnection.html#addRequestProperty%28java.lang.String,%20java.lang.String%29

2 Comments

thank you for answering, i tried your solution but it gives the same result as before! "login failed" although i am sure that the parameters are correct!
it is working now fine, after correcting the data, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.