使用CodeName One登录到NTLM服务器

时间:2016-06-09 09:00:43

标签: codenameone

所以我是codename1的新手, 我试图使用HTLM登录服务器, 我怎么做到这一点?

我找不到任何有助于解决此问题的资源。

2 个答案:

答案 0 :(得分:1)

在此处https://docs.oracle.com/javase/8/docs/technotes/guides/net/http-auth.html

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.Authenticator;
    import java.net.PasswordAuthentication;
    import java.net.URL;

    public class RunHttpSpnego {
        static final String kuser = "username"; // your account name
        static final String kpass = password; // retrieve password for your account 

        static class MyAuthenticator extends Authenticator {
            public PasswordAuthentication getPasswordAuthentication() {
                // I haven't checked getRequestingScheme() here, since for NTLM
                // and Negotiate, the usrname and password are all the same.
                System.err.println("Feeding username and password for " + getRequestingScheme());
                return (new PasswordAuthentication(kuser, kpass.toCharArray()));
            }
        }

        public static void main(String[] args) throws Exception {
            Authenticator.setDefault(new MyAuthenticator());
            URL url = new URL(args[0]);
            InputStream ins = url.openConnection().getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
            String str;
            while((str = reader.readLine()) != null)
                System.out.println(str);
        }
    }

答案 1 :(得分:1)

NTLM需要来自操作系统的本机支持。你在那里做的将在JavaSE上运行,模拟器正在运行,但不能在设备上运行。

要在设备上运行此功能,您需要使用原生代码NTLM Authentication with HttpURLConnection来描述如何为Android完成此操作。要实现这一点,您需要创建一个本机接口并在本机接口中调用此代码:

jcifs.Config.registerSmbURLHandler();

从那时起,其余部分应该来自Codename One。请注意,您还需要将library that's required放在native / android目录中。

围栏的iOS端应该使用此how to send NTLM request in iOS

特别是这个块:

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost: _host
port: 80
protocol: @"http"
realm: _host
authenticationMethod:NSURLAuthenticationMethodNTLM];