HTTP authentication in J2ME

前端 未结 3 1301
北海茫月
北海茫月 2020-12-18 01:55

I\'m trying to create a J2ME app, which talks to webserver using

相关标签:
3条回答
  • 2020-12-18 02:21

    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)

    0 讨论(0)
  • 2020-12-18 02:41

    Incredible, it works like a charm:

    String url = "hppt://www.example.com";
    HttpConnection hc = (HttpConnection) Connector.open(url);
    hc.setRequestProperty("Authorization", "Basic "+ BasicAuth.encode("user", "password"));
    
    0 讨论(0)
  • 2020-12-18 02:45

    I used Bouncy Castle cryptographic library's Base64 Encoder/Decoder. It is very powerful in overcoming lots of limitations set by Java ME/J2ME API. It's open-source and it works for both Java ME and Android.

    0 讨论(0)
提交回复
热议问题