title: Connect to Dynamic CRM using Java Http Client
body: <p>I have been trying to connect to CRM using the below Java code, but I am getting an authentication issue.</p>
  
  <pre><code>package com.raj;
  
  import java.io.IOException;
  import org.apache.commons.io.IOUtils;
  import org.apache.http.Header;
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpHost;
  import org.apache.http.HttpResponse;
  import org.apache.http.auth.AuthScope;
  import org.apache.http.auth.NTCredentials;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.protocol.BasicHttpContext;
  import org.apache.http.protocol.HttpContext;
  
  public class MSDynaOData {
  
  /**
  * @param args
  */
  public static void main(String[] args) throws ClientProtocolException, IOException {
  
  DefaultHttpClient httpclient = new DefaultHttpClient();
  
  NTCredentials creds = new NTCredentials("XXXXXXXXXX", "XXXXXXX", "", "");
  httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
  
  HttpHost target = new HttpHost("XXXXXXXXX", 80, "http");
  HttpContext localContext = new BasicHttpContext();
  
  // Execute a cheap method first. This will trigger NTLM authentication
  String url = "/XRMServices/2011/Organization.svc/Account";
  url += "?$select=Name";
  HttpGet httpget = new HttpGet(url);
  
  httpget.addHeader("Accept", "application/json");
  HttpResponse response = httpclient.execute(target, httpget, localContext);
  HttpEntity entity = response.getEntity();
  System.out.println(" Status :: " + response.getStatusLine());
  
  for (Header header : response.getAllHeaders()) {
  System.out.println(header.getName() + " : " + header.getValue());
  }
  
  System.out.println(IOUtils.toString(entity.getContent()));
  }
  }
  </code></pre>
  
  <p>Response contains :</p>
  
  <pre><code>Constants.TokenizedStringMsgs.GENERIC_ERROR = "&lt;H1&gt;Sorry, but we're having trouble           signing you in&lt;/H1&gt;&lt;p&gt;Please try again in a few minutes. If this doesn't work, you might     want to contact your admin and report the following error: #~#ErrorCode#~#.&lt;/p&gt;";
    Constants.TokenizedStringMsgs.UPN_DISAMBIGUATE_MESSAGE = "It looks like #~#MemberName_LS#~# is used with more than one account. Which account do you want to use?";
  </code></pre>
  
  <p>I am able to login to CRM 2011 online through the credentials used in Java code successfully, but when I used same credentials in the code I am getting the above login issue response.</p>
  
  <p>Please let me know if I am missing anything in the above code.</p>