Skip to content

Instantly share code, notes, and snippets.

@abelevtsov
Last active August 17, 2017 15:03
Show Gist options
  • Save abelevtsov/2ac3e001505dc8eb8a955281124e1b98 to your computer and use it in GitHub Desktop.
Save abelevtsov/2ac3e001505dc8eb8a955281124e1b98 to your computer and use it in GitHub Desktop.

Revisions

  1. abelevtsov revised this gist Aug 17, 2017. 1 changed file with 21 additions and 9 deletions.
    30 changes: 21 additions & 9 deletions Crm2013_OnPremise_NTLMv2_Java_Connect.java
    Original file line number Diff line number Diff line change
    @@ -7,14 +7,23 @@
    public class Main {

    public static void main(String[] args) throws IOException {
    String domain = "DOMAIN";
    String username = "LOGIN";
    String password = "PASSWORD";
    String url = "SERVER_URL";
    String scheme = "https";
    String crmUrl = "URL";
    String orgName = "ORG_NAME";
    String url = String.format("%s://%s%s%s:%s@%s/%s", scheme, domain, "%5C", username, password, crmUrl, orgName);
    String actionName = "new_CustomAction";

    // then use https you need to store certificate in cacerts before call CRM service
    System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files (x86)\\Java\\jre1.8.0_144\\lib\\security\\cacerts");
    // to avoid "PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target" exception
    // see: https://stackoverflow.com/questions/9619030/resolving-javax-net-ssl-sslhandshakeexception-sun-security-validator-validatore for details
    if (scheme == "https") {
    System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files (x86)\\Java\\jre1.8.0_144\\lib\\security\\cacerts");
    }

    String soapXML =
    String reqXML =
    "<?xml version='1.0' encoding='UTF-8'?>" +
    "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' " +
    "xmlns:ser='http://schemas.microsoft.com/xrm/2011/Contracts/Services' " +
    @@ -23,41 +32,44 @@ public static void main(String[] args) throws IOException {
    "<soap:Header/>" +
    "<soap:Body>" +
    "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
    "<request xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>" +
    "<request " +
    "xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' " +
    "xmlns:i='http://www.w3.org/2001/XMLSchema-instance' " +
    "xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>" +
    "<a:Parameters xmlns:c='http://www.w3.org/2001/XMLSchema'>" +
    "<a:KeyValuePairOfstringanyType>" +
    "<b:key>data</b:key>" +
    "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>" +
    "test_data" +
    "test" +
    "</b:value>" +
    "</a:KeyValuePairOfstringanyType>" +
    "</a:Parameters>" +
    "<a:RequestId i:nil='true'/>" +
    "<a:RequestName>new_CustomAction</a:RequestName>" +
    "<a:RequestName>" + actionName + "</a:RequestName>" +
    "</request>" +
    "</Execute>" +
    "</soap:Body>" +
    "</soap:Envelope>";

    jcifs.Config.registerSmbURLHandler();

    URL urlRequest = new URL("https://OPEN.RU%5C" + username + ":" + password + "@" + url + "/XRMServices/2011/Organization.svc/web");
    URL urlRequest = new URL(url + "/XRMServices/2011/Organization.svc/web");

    HttpURLConnection conn = (HttpURLConnection)urlRequest.openConnection();

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    conn.setRequestProperty("Accept", "application/xml, text/xml, */*");
    conn.setRequestProperty("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    conn.setRequestProperty("Content-Length", String.valueOf(soapXML.length()));
    conn.setRequestProperty("Content-Length", String.valueOf(reqXML.length()));
    conn.setDoOutput(true);
    conn.setDoInput(true);

    StringBuilder response = new StringBuilder();

    try {
    OutputStream reqStream = conn.getOutputStream();
    reqStream.write(soapXML.getBytes("UTF-8"));
    reqStream.write(reqXML.getBytes("UTF-8"));

    InputStream stream = conn.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(stream));
  2. abelevtsov created this gist Aug 16, 2017.
    77 changes: 77 additions & 0 deletions Crm2013_OnPremise_NTLMv2_Java_Connect.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    package ru.open;

    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class Main {

    public static void main(String[] args) throws IOException {
    String username = "LOGIN";
    String password = "PASSWORD";
    String url = "SERVER_URL";

    // then use https you need to store certificate in cacerts before call CRM service
    System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files (x86)\\Java\\jre1.8.0_144\\lib\\security\\cacerts");

    String soapXML =
    "<?xml version='1.0' encoding='UTF-8'?>" +
    "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' " +
    "xmlns:ser='http://schemas.microsoft.com/xrm/2011/Contracts/Services' " +
    "xmlns:con='http://schemas.microsoft.com/xrm/2011/Contracts' " +
    "xmlns:arr='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
    "<soap:Header/>" +
    "<soap:Body>" +
    "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
    "<request xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>" +
    "<a:Parameters xmlns:c='http://www.w3.org/2001/XMLSchema'>" +
    "<a:KeyValuePairOfstringanyType>" +
    "<b:key>data</b:key>" +
    "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>" +
    "test_data" +
    "</b:value>" +
    "</a:KeyValuePairOfstringanyType>" +
    "</a:Parameters>" +
    "<a:RequestId i:nil='true'/>" +
    "<a:RequestName>new_CustomAction</a:RequestName>" +
    "</request>" +
    "</Execute>" +
    "</soap:Body>" +
    "</soap:Envelope>";

    jcifs.Config.registerSmbURLHandler();

    URL urlRequest = new URL("https://OPEN.RU%5C" + username + ":" + password + "@" + url + "/XRMServices/2011/Organization.svc/web");

    HttpURLConnection conn = (HttpURLConnection)urlRequest.openConnection();

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    conn.setRequestProperty("Accept", "application/xml, text/xml, */*");
    conn.setRequestProperty("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    conn.setRequestProperty("Content-Length", String.valueOf(soapXML.length()));
    conn.setDoOutput(true);
    conn.setDoInput(true);

    StringBuilder response = new StringBuilder();

    try {
    OutputStream reqStream = conn.getOutputStream();
    reqStream.write(soapXML.getBytes("UTF-8"));

    InputStream stream = conn.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(stream));

    String str;
    while ((str = in.readLine()) != null) {
    response.append(str);
    }

    in.close();

    System.out.println(response);
    } catch(Exception err) {
    System.out.println(err);
    }
    }
    }