Last active
August 17, 2017 15:03
-
-
Save abelevtsov/2ac3e001505dc8eb8a955281124e1b98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 domain = "DOMAIN"; | |
String username = "LOGIN"; | |
String password = "PASSWORD"; | |
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 | |
// 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 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' " + | |
"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" + | |
"</b:value>" + | |
"</a:KeyValuePairOfstringanyType>" + | |
"</a:Parameters>" + | |
"<a:RequestId i:nil='true'/>" + | |
"<a:RequestName>" + actionName + "</a:RequestName>" + | |
"</request>" + | |
"</Execute>" + | |
"</soap:Body>" + | |
"</soap:Envelope>"; | |
jcifs.Config.registerSmbURLHandler(); | |
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(reqXML.length())); | |
conn.setDoOutput(true); | |
conn.setDoInput(true); | |
StringBuilder response = new StringBuilder(); | |
try { | |
OutputStream reqStream = conn.getOutputStream(); | |
reqStream.write(reqXML.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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment