title: NTLM Authentication
body: <p>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. </p>
  
  <p>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</p>
  
  <pre><code>compile group: 'jcifs', name: 'jcifs', version: '1.3.17'
  </code></pre>
  
  <p>Code inspired from here: </p>
  
  <p><a href="https://stackoverflow.com/questions/32968769/ntlm-authentication-with-httpurlconnection">NTLM Authentication with HttpURLConnection</a></p>
  
  <p>The importing line just before the class declaration</p>
  
  <pre><code>import org.apache.http.impl.auth.NTLMEngineException;
  </code></pre>
  
  <p>Doesn't work since I am suspecting that the httpclient is deprecated</p>
  
  <p>My code sample:</p>
  
  <pre><code>public class DataFetcher extends AsyncTask&lt;String, Void, String&gt;{
      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);
  }
  </code></pre>
  
  <p>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)</p>