Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

java - HTTP authentication in J2ME

I'm trying to create a J2ME app, which talks to webserver using HttpConnection connector.

When I am talking to the WebServer, I have to authenticate using Basic HTTP auth, which normally goes like

http://username:[email protected]/rest/api/method

But in J2ME, when I construct a url of this form, it doesn't work.

I also tried adding request property, hc = (HttpConnection) Connector.open(url); hc.setRequestProperty("User", "alagu"); hc.setRequestProperty("pass", "mypassword");

but didn't work.

Has anyone done j2me based HTTP auth before? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It could be J2ME has no support for basic authentication, I might be wrong. If you want to try just setting the authentication header in the request yourself you'll likely need a different header then what you're using.

From the rfc:

To receive authorization, the client sends the userid and password, separated by a single colon (":") character, within a base64 [7] encoded string in the credentials.

[...]

If the user agent wishes to send the userid "Aladdin" and password "open sesame", it would use the following header field:

 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

So just create the string "User:Password", base64 encode it and then call setRequestProperty("Authorization", "Basic "+ encodedUserAndPass)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...