2

I am trying to create an application that connects to a web page that uses NTLM Authentication (not mine, so I can't change the authentication method) using a username and password and returns the page's source which I will later use to extract the information that I need.

My problem is with the authentication step. Using the HttpUrlConnection returns error 401-Unauthorized. I am using jcifs. Already add it to the build.gradle file

compile group: 'jcifs', name: 'jcifs', version: '1.3.17'

Code inspired from here:

NTLM Authentication with HttpURLConnection

The importing line just before the class declaration

import org.apache.http.impl.auth.NTLMEngineException;

Doesn't work since I am suspecting that the httpclient is deprecated

My code sample:

public class DataFetcher extends AsyncTask<String, Void, String>{
    protected String doInBackground(String... urls){
        jcifs.Config.registerSmbURLHandler();
        URL url;
        HttpURLConnection connection = null;

        String result = "" ;

        try{
            url = new URL(urls[0]);
            Log.i("URL Connected", "Done");
            connection = (HttpURLConnection) url.openConnection();
            Log.i("Connection Established","Done");

            InputStream inputStream;
            int status = connection.getResponseCode();
            if(status != HttpURLConnection.HTTP_OK){
                inputStream = connection.getErrorStream();
            }else {
                inputStream = connection.getInputStream();
            }
            Log.i("Input Stream made","Done");
            InputStreamReader reader = new InputStreamReader(inputStream);
            Log.i("Input Stream Reader","Done");


            int data = reader.read();
            while(data!=-1){
                result += (char) data;
                data = reader.read();
            }

        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }

        Log.i("Done",result);
        Log.i("Response",""+connection.getHeaderFields());
        return result;

    }
}

public void fetchData(){
    String url = "http://DOMAIN%5user:password@server/page.aspx";
    DataFetcher fetcher = new DataFetcher();
    fetcher.execute(url);
}

I checked out most (if not all) of all the links that I found however most links use HttpClient (which is deprecated), and the apache clients (which is deprecated as well)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Karim ElGhandour
  • 337
  • 3
  • 13

1 Answers1

-1

I'm actively working on the same problem, but the workaround I have in place involves targeting API 22. This obviously won't work going forward when Google starts mandating API 23 minimum in August 2018 but this might get you going now.

Jose Bueno
  • 126
  • 1
  • 9