简体   繁体   English

在Android中使用JCIFS

[英]Using JCIFS in Android

Here is the case: I want to authenticate my Android device in code, by sending username and password to specific URL, this URL will have internal and external Access. 就是这种情况:我想通过代码验证我的Android设备,方法是将用户名和密码发送到特定的URL,此URL将具有内部和外部访问权限。 through reading JCIFS Library which main job is to connect to windows using NTML, trying to make it works in android, I inserted this code: 通过阅读JCIFS库 ,其主要工作是使用NTML连接到Windows,试图使其在android中工作,我插入了以下代码:

        new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {

            try {
                String user = "username";
                String pass = "password";
                String sharedFolder = "shared";
                String url = "smb://url/url//" + sharedFolder + "/test.txt";
                NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
                        null, user, pass);
                SmbFile sfile = new SmbFile(url, auth);
                sfile.canRead();
            } catch (Exception e) {
                e.printStackTrace();
            }




            return null;
        }
    }.execute();

this sample code I have used for only getting authorization from Windows into my Android application, I just want to return the Auth, not copying files nor comparing between files. 此示例代码仅用于从Windows获得授权进入我的Android应用程序,我只想返回Auth,而不是复制文件或在文件之间进行比较。 I have failed after reading UrlConnection and UrlStreamHandler and have tried almost all questions in StackOverFlow but I didn't find an answer. 在阅读UrlConnectionUrlStreamHandler之后,我失败了,并尝试了StackOverFlow中的几乎所有问题,但没有找到答案。

also when I apply this code, the message that comes is 当我应用此代码时,出现的消息是

02-21 04:50:02.329 3032-3046/ils.com.ntmlproject W/System.err: jcifs.smb.SmbException: Failed to connect to server
02-21 04:50:02.329 3032-3046/ils.com.ntmlproject W/System.err: java.net.UnknownHostException: jorphp01
02-21 04:50:02.329 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.UniAddress.getAllByName(UniAddress.java:315)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:864)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.connect(SmbFile.java:954)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.queryPath(SmbFile.java:1338)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.exists(SmbFile.java:1420)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.canRead(SmbFile.java:1462)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at ils.com.ntmlproject.MainActivity$1.doInBackground(MainActivity.java:51)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at ils.com.ntmlproject.MainActivity$1.doInBackground(MainActivity.java:39)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at java.lang.Thread.run(Thread.java:841)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.connect0(SmbFile.java:882)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.queryPath(SmbFile.java:1338)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.exists(SmbFile.java:1420)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at jcifs.smb.SmbFile.canRead(SmbFile.java:1462)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at ils.com.ntmlproject.MainActivity$1.doInBackground(MainActivity.java:51)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at ils.com.ntmlproject.MainActivity$1.doInBackground(MainActivity.java:39)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err:     at java.lang.Thread.run(Thread.java:841)

I don't know if the error is in http protocol ? 我不知道该错误是否在http协议中? or in the code? 还是在代码中? what I need to adjust here and what I have missed? 我需要在这里调整什么,错过什么?

The host name: jorphp01 is not resolvable from the device where the code is executed. 主机名: jorphp01无法在执行代码的设备上解析。 Below would be the possible solutions: 以下是可能的解决方案:

  1. Try connecting the device to the same network and ensure it goes through the same DNS server which can resolve this host name. 尝试将设备连接到同一网络,并确保它通过可以解析此主机名的同一DNS服务器。
  2. If the server is reachable, but DNS resolution fails, modify the DNS server configured. 如果服务器可访问,但DNS解析失败,请修改配置的DNS服务器。 Can take help of apps like https://play.google.com/store/apps/details?id=uk.co.mytechie.setDNS&hl=en 可以使用诸如https://play.google.com/store/apps/details?id=uk.co.mytechie.setDNS&hl=zh_CN的应用程序的帮助
  3. Try configuring the IP address instead of host name in the client application on the device. 尝试在设备上的客户端应用程序中配置IP地址而不是主机名。
  4. Modify the /etc/hosts file (on rooted device) to resolve the host name to the preferred IP address. 修改/ etc / hosts文件(在有根设备上),以将主机名解析为首选IP地址。 Ref: https://www.howtogeek.com/140576/how-to-edit-the-hosts-file-on-android-and-block-web-sites/ 参考: https : //www.howtogeek.com/140576/how-to-edit-the-hosts-file-on-android-and-block-web-sites/

I found what case the error and what the solution to my case. 我发现了什么情况下的错误以及我的情况下的解决方案。 first thanks for @Pavan Kumar for answer the question and gives me some hints. 首先感谢@Pavan Kumar回答了这个问题,并给了我一些提示。

  1. The server I was trying to reach is not reachable through Android, so instead I have used the IP address of server instead of its name. 我尝试访问的服务器无法通过Android访问,因此我改用了服务器的IP地址而不是其名称。
  2. I have applied the OkHTTP in network connection using jcifs library to connect to my server through this method which works fine in my case: 我已经使用jcifs库在网络连接中应用了OkHTTP通过这种方法连接到服务器,这种方法在我的情况下效果很好:

     final TextView textView = (TextView) findViewById(R.id.tvResponse); new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... params) { HttpUrl.Builder urlBuilder = HttpUrl.parse("http://10.10.6.4/erpapi/").newBuilder(); String url = urlBuilder.build().toString(); // issue the Get request String getResponse = ""; try { getResponse = doGetRequest(url); System.out.println(getResponse); } catch (IOException e) { e.printStackTrace(); getResponse = e.getMessage(); } return getResponse; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); textView.setText(s.toString().toLowerCase()); } }.execute(); 

and the function is : 函数是:

static String doGetRequest(String url) throws IOException {
        okHttpClient = new OkHttpClient.Builder()
                .authenticator(new NTMLAuthenticator("username", "password"))
                .build();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = okHttpClient.newCall(request).execute();
        return response.body().string();}

the source of this code , and have used the jcifs library for authentication. 此代码 ,并已使用jcifs库进行身份验证。

after this I could easily login in to server and sent authenticated. 在此之后,我可以轻松登录服务器并发送经过身份验证的信息。

also these are some useful resources to see from: 这些也是一些有用的资源,可从以下资源中看到:

httpclient HttpClient的

android-ntlm Android的NTLM

ntlm-authentication-in-android NTLM身份验证功能于安卓

android-smb-cifs Android的SMB,CIFS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM