gpt4 book ai didi

java - J2ME 中的 HTTP 身份验证

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:08:03 25 4
gpt4 key购买 nike

我正在尝试创建一个 J2ME 应用程序,它使用 <a href="http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/io/HttpConnection.html" rel="noreferrer noopener nofollow">HttpConnection</a> 与网络服务器通信连接器。

当我与 WebServer 对话时,我必须使用基本 HTTP 身份验证进行身份验证,这通常是这样的

<a href="http://username:password@website.com/rest/api/method" rel="noreferrer noopener nofollow">http://username:password@website.com/rest/api/method</a>

但是在 J2ME 中,当我构造一个这种形式的 url 时,它不起作用。

我还尝试添加请求属性,
hc = (HttpConnection) Connector.open(url);
hc.setRequestProperty("User", "alagu");
hc.setRequestProperty("pass", "mypassword");

但是没用。

有没有人做过基于j2me的HTTP认证?提前致谢。

最佳答案

可能是 J2ME 不支持基本身份验证,我可能错了。如果您想尝试自己在请求中设置身份验证 header ,您可能需要一个与您正在使用的 header 不同的 header 。

来自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==

因此只需创建字符串“User:Password”,对其进行 base64 编码,然后调用 setRequestProperty("Authorization", "Basic "+ encodedUserAndPass)

关于java - J2ME 中的 HTTP 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/950885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com