WebView中的NTLM身份验证

时间:2013-02-18 10:23:38

标签: android android-webview ntlm

我正在尝试加载一个会在WebView中显示图片的网址。 我需要在加载URL之前传递凭据(用户名和密码)。

此处,URL是从具有NTLM身份验证的服务器托管的。

我能够点击另一个这样的URL并获取数据。但是,我如何为Android中的WebView执行相同的操作?

2 个答案:

答案 0 :(得分:4)

您可以使用Chilkat Library进行NTLM身份验证。

  1. 下载java类(包含在下载部分的zip / rar文件中) 并将包添加到/ src文件夹中。包名是com.chilkatsoft
  2. 将库添加到/ libs文件夹。 chilkat libraries文件夹是:

    • armeabi
    • armeabi-V7A
    • MIPS
    • 86

      public class MainActivity extends Activity {
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          boolean success;
          success = http.UnlockComponent("anything");
          if (success != true) {
              return;
          }
      
          http.put_Login("<Username>");
          http.put_Password("<Password>");
          http.put_NtlmAuth(true);
          http.put_SessionLogFilename("ntlmAuthLog.txt");
          String html;
          html = http.quickGetStr("http://websitewithntlmnauthentication.com");
          //load the data to a webview from the string "html".
          webView.loadUrl(html,"","UTF-8"); }
      

      并在onCreate()

      之后添加此内容
      static {
                // Important: Make sure the name passed to loadLibrary matches the shared library
                // found in your project's libs/armeabi directory.
                //  for "libchilkat.so", pass "chilkat" to loadLibrary
                //  for "libchilkatemail.so", pass "chilkatemail" to loadLibrary
                //  etc.
                // 
                System.loadLibrary("chilkat");
      
                // Note: If the incorrect library name is passed to System.loadLibrary,
                // then you will see the following error message at application startup:
                //"The application <your-application-name> has stopped unexpectedly. Please try again."
            }
      

答案 1 :(得分:2)

问题有:&#34;我需要在加载URL之前传递凭据(用户名和密码)。&#34;但是,在加载URL之前传递凭据不是一个好习惯。

不需要任何第三方库。可以通过在WebViewClient中重写以下方法来实现:

        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                HttpAuthHandler handler, String host, String realm)
        {
            Log.d(TAG + "_onReceivedHttpAuthRequest", "host = " + host + " realm = " + realm);
//Show dialog and accept credentials from end-user. Hard-coding username and password is strict NO as it can be easlity reverse engineered.
            handler.proceed("username", "password");
        }