J2ME中的HTTP身份验证 [英] HTTP authentication in J2ME
问题描述
我正在尝试创建一个J2ME应用,它使用 HttpConnection
连接器。
I'm trying to create a J2ME app, which talks to webserver using HttpConnection
connector.
当我与WebServer通话时,我必须使用Basic HTTP auth进行身份验证,这通常类似于
When I am talking to the WebServer, I have to authenticate using Basic HTTP auth, which normally goes like
http:// username:password@website.com/rest/ api /方法
但是在J2ME中,当我构造这个表单的url时,它不起作用。
But in J2ME, when I construct a url of this form, it doesn't work.
我还尝试添加请求属性,
hc =(HttpConnection)Connector.open(url);
hc.setRequestProperty(User,alagu);
hc.setRequestProperty(pass,mypassword);
I also tried adding request property,
hc = (HttpConnection) Connector.open(url);
hc.setRequestProperty("User", "alagu");
hc.setRequestProperty("pass", "mypassword");
但不起作用。
之前有没有人做过基于j2me的HTTP身份验证?在此先感谢。
Has anyone done j2me based HTTP auth before? Thanks in advance.
推荐答案
可能是J2ME不支持基本身份验证,我可能错了。如果你想尝试在请求中自己设置身份验证标头,那么你可能需要一个不同的标题然后你正在使用的标题。
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.
来自 rfc :
要接收授权,客户端会在凭证中的base64 [7]
编码字符串中发送用单个冒号(:)字符分隔的用户ID和密码
。
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.
[...]
如果用户代理希望发送用户IDAladdin和密码
打开sesame,它将使用以下标题字段:
If the user agent wishes to send the userid "Aladdin" and password "open sesame", it would use the following header field:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
所以只需创建字符串User:Password,base64编码然后调用 setRequestProperty(授权,基本+ encodedUserAndPass)
So just create the string "User:Password", base64 encode it and then call setRequestProperty("Authorization", "Basic "+ encodedUserAndPass)
这篇关于J2ME中的HTTP身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!