2

我正在尝试加载一个将在WebView. 在加载 URL 之前,我需要传递凭据(用户名和密码)。

在这里,URL 由具有 NTLM 身份验证的服务器托管。

我可以点击另一个这样的 URL 并获取数据。但是我如何WebView在 Android 中为 a 做同样的事情呢?

4

2 回答 2

4

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

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

    • 阿米亚比
    • armeabi-v7a
    • mips
    • x86

      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."
            }
      
于 2013-04-18T02:12:32.270 回答
3

问题是:“我需要在加载 URL 之前传递凭据(用户名和密码)。” 但是,在加载 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");
        }
于 2015-06-30T13:06:31.980 回答