title: Kerberos connection using HTTP Client
body: <p>I'm writing HTTP connection with Kerberos authentication. I have "HTTP/1.1 401 Unauthorized". Could you recommend me what I should check? I think there's somethink trick, but I don't see it.</p>
  
  <p>May be I should set header "WWW-Authenticate" with "Negotiate"?</p>
  
  <p>Thank a lot in advanced for any help and ideas.</p>
  
  <pre><code>public class ClientKerberosAuthentication {
  
      public static void main(String[] args) throws Exception {
  
          System.setProperty("java.security.auth.login.config", "login.conf");
          System.setProperty("java.security.krb5.conf", "krb5.conf");
          System.setProperty("sun.security.krb5.debug", "true");
          System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
  
          DefaultHttpClient httpclient = new DefaultHttpClient();
          try {
             NegotiateSchemeFactory nsf = new NegotiateSchemeFactory();
             httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);            
  
             List&lt;String&gt; authpref = new ArrayList&lt;String&gt;();
             authpref.add(AuthPolicy.BASIC);
             authpref.add(AuthPolicy.SPNEGO);
             httpclient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);            
  
  
             httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope(null, -1, AuthScope.ANY_REALM, AuthPolicy.SPNEGO), 
                    new UsernamePasswordCredentials("myuser", "mypass"));            
  
             System.out.println("----------------------------------------");
             HttpUriRequest request = new HttpGet("http://localhost:8084/web-app/webdav/213/_test.docx");
             HttpResponse response = httpclient.execute(request);
             HttpEntity entity = response.getEntity();
  
             System.out.println("----------------------------------------");
             System.out.println(response.getStatusLine());
             System.out.println("----------------------------------------");
             if (entity != null) {
                 System.out.println(EntityUtils.toString(entity));
             }
             System.out.println("----------------------------------------");
  
             // This ensures the connection gets released back to the manager
             EntityUtils.consume(entity);
  
          } finally {
             httpclient.getConnectionManager().shutdown();
          }
      }
  }
  </code></pre>