Kevin Tan
Kevin Tan

Reputation: 179

Webview client not listening to shouldOverrideUrlLoading method when clicking some other hyperlink/button in the webView

I have a sharepoint site, which has ntlm authentication. in order for me to load the page, i do an authentication to the site using this.

public String LoadUrlWithNTLM(String url){
        CkHttp http = new CkHttp();
        http.put_Login("username");
        http.put_Password("password");
        http.put_NtlmAuth(true);
        http.put_SessionLogFilename("ntlmAuthLog.txt");
        String source = http.quickGetStr(url);
        return source;
    }

and load the webview with this.

public void LoadWebView(String url, String source){
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadDataWithBaseURL(url, source, "text/html", "", "");
    }

i call this in the OnCreate()

source= LoadUrlWithNTLM(url);
LoadWebView(url,source);

then i check if there is a url event click with this

webView.setWebViewClient(new WebViewClient() { 
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            String toWebView = LoadUrlWithNTLM(url);
            LoadWebView(url,source);
            return false; 
        } 
    });

at some point, i can manage through go to the Sharepoint Site with NTLM Authentication, but when i click some link, it just display "401 UNAUTHORIZED" and do not invoke the shouldOverrideUrlLoading() method on breakpoint.

Upvotes: 2

Views: 777

Answers (1)

Chilkat Software
Chilkat Software

Reputation: 1659

After authorizing, each subsequent HTTP request should include an Authorization header that contains the result of the prior authorization. If the subsequent request were sent using Chilkat HTTP, then the object would automatically send this Authorization header. However, the WebView has no knowledge of it, and it's including any Authorization header with it's request, and therefore you get the "401 Unauthorized" error.

One solution is to see if you can do NTLM authorization with WebView. I'm assuming you're using Chilkat only because this is not possible.

Another solution is to use Chilkat as you are doing, but then get the value of the Authorization header (from Chilkat) and explicitly set this header field with WebView. I don't know enough about WebView to know whether this is possible. To get the value of the Authorization header from Chilkat may require a new Chilkat feature (and I think this may be easy to do). (or it's already possible, but in a convoluted way)

Upvotes: 1

Related Questions