使用Java连接到Microsoft Dynamics CRM内部部署Web服务吗?

使用Java连接到Microsoft Dynamics CRM内部部署Web服务吗?,第1张

使用Java连接到Microsoft Dynamics CRM内部部署Web服务吗?

内部版本的Microsoft Dynamics CRM应用程序使用Active Directory身份验证。尽管我从未尝试过从Java引用Microsoft
Dynamics CRM Web服务,但是我确信它是可行的,因为它们是标准的Web服务,因此可以像其他任何Web服务一样通过SOAP从Java进行引用。

public class TestCRM {private static String endpointURL = "http://server:port/MSCrmServices/2007/CrmService.asmx";  private static String userName = "username";  private static String password = "password";  private static String host = "server";  private static int portport = port;//To make sure you are using the correct domain open ie and try to reach the service. The same domain you entered there is needed here  private static String domain = "DOMAIN";private static String orgName = "THIS_IS_REQUIRED"; //this does the work....public static void main(String[] args) {    CrmServiceStub stub;      try {          stub = new CrmServiceStub(endpointURL);          setOptions(stub._getServiceClient().getOptions());        RetrieveMultipledocument rmd = RetrieveMultipledocument.Factory.newInstance();          RetrieveMultiple rm = RetrieveMultiple.Factory.newInstance();        Queryexpression query = Queryexpression.Factory.newInstance();          query.setColumnSet(AllColumns.Factory.newInstance());          query.setEntityName(EntityName.######.toString());          //query.setFilter...        rm.setQuery(query);          rmd.setRetrieveMultiple(rm);        //Now this is required. Without it all i got was 401s errors          CrmAuthenticationTokendocument catd = CrmAuthenticationTokendocument.Factory.newInstance();          CrmAuthenticationToken token = CrmAuthenticationToken.Factory.newInstance();          token.setAuthenticationType(0);  token.setOrganizationName(orgName);          catd.setCrmAuthenticationToken(token);        boolean fetchNext = true;          while(fetchNext){   RetrieveMultipleResponsedocument rmrd = stub.RetrieveMultiple(rmd,  catd, null, null);   RetrieveMultipleResponse rmr = rmrd.getRetrieveMultipleResponse();   BusinessEntityCollection bec = rmr.getRetrieveMultipleResult(); String pagingcookie = bec.getPagingcookie();   fetchNext = bec.getMoreRecords(); ArrayOfBusinessEntity aobe = bec.getBusinessEntities();   BusinessEntity[] myEntitiesAtLast = aobe.getBusinessEntityArray(); for(int i=0; i<myEntitiesAtLast.length; i++){       //cast to whatever you asked for...       ### myEntity = (###) myEntitiesAtLast[i];   }          }      }       catch (Exception e) {          e.printStackTrace();      }  }private static void setOptions(Options options){      HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();    List authSchemes = new ArrayList();      authSchemes.add(HttpTransportProperties.Authenticator.NTLM);       auth.setAuthSchemes(authSchemes);    auth.setUsername(userName);      auth.setPassword(password);      auth.setHost(host);      auth.setPort(port);      auth.setDomain(domain);      auth.setPreemptiveAuthentication(false); //it doesnt matter...      options.setProperty(HTTPConstants.AUTHENTICATE, auth);      options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, "true"); //i think this is good.. not required though  }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5141012.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存