J2ME 中的 HTTP 身份验证 [英] HTTP authentication in J2ME

查看:15
本文介绍了J2ME 中的 HTTP 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 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("用户", "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:

接收授权,客户端发送用户ID和密码,在 base64 [7] 内由单个冒号 (":") 字符分隔凭据中的编码字符串.

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"和密码芝麻开门",它将使用以下标题字段:

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("Authorization", "Basic"+ encryptedUserAndPass)

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

这篇关于J2ME 中的 HTTP 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆