使用Java Http Client连接到Dynamic CRM

时间:2013-08-14 06:44:27

标签: java dynamics-crm-2011 httpclient office365

我一直在尝试使用以下Java代码连接到CRM,但我收到了身份验证问题。

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()));
}
}

回复包含:

Constants.TokenizedStringMsgs.GENERIC_ERROR = "<H1>Sorry, but we're having trouble           signing you in</H1><p>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#~#.</p>";
  Constants.TokenizedStringMsgs.UPN_DISAMBIGUATE_MESSAGE = "It looks like #~#MemberName_LS#~# is used with more than one account. Which account do you want to use?";

我能够通过Java代码中成功使用的凭据在线登录CRM 2011,但是当我在代码中使用相同的凭据时,我得到了上述登录问题响应。

如果我遗漏了上述代码中的任何内容,请告诉我。

0 个答案:

没有答案