web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Convert Java code with access key to C# code to POST JSON data

(0) ShareShare
ReportReport
Posted on by

Can anyone please help Ous in converting this code to c# to POST JSON data in CRM online.

package com.sample;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;


class RegPatient {
private final static String DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss z";
private final static String HMAC_SHA1_ALGORITHM = "HmacSHA1";

private final static String URL = "https://******.in/";
private final static String Entity = "*************";

//Live Public Key and Secret Key for nani-hospital
private final static String SECRET = "*********************";
private final static String USERNAME = "*************";

private static String sampleJson = "{\"name\":\"Test\",\"lname\":\"Patient\",\"registrationdate\":\"20181210\",\"dob\":\"19840510\",\"gender\":\"M\",\"mobile\":\"0000000000\",\"street\":\"10th cross street\",\"area\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\",\"entitykey\":\"nani-hospital\",\"title\":\"Mr\",\"age\":\"34\",\"maritalstatus\":\"Single\",\"spousename\":\"\",\"spouseage\":\"\",\"idproof\":\"Aadhaar\",\"idproofdetails\":\"1234567890123458\",\"bloodgroup\":\"A+\",\"email\":\"conatactme@xyz.com\",\"contactnumbers\":\"1234567890,0987654321\",\"ptsource\":\"flyers\",\"entitylocation\":\"location1\",\"familyid\":\"\",\"referred_by\":\"\",\"referred_bykey\":\"\",\"extphid\":\"\",\"patienttype\":\"None\",\"tag\":\"\",\"religion\":\"Hindu\",\"fileno\":\"\",\"occupation\":\"Software Engineer\"}";

private static String sampleErrorJson = "{\"name\":\"\",\"lname\":\"Patient\",\"registrationdate\":\"20181210\",\"dob\":\"19840510\",\"gender\":\"M\",\"mobile\":\"\",\"street\":\"10th cross street\",\"area\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\",\"entitykey\":\"nani-hospital\",\"title\":\"Mr\",\"age\":\"34\",\"maritalstatus\":\"Single\",\"spousename\":\"\",\"spouseage\":\"\",\"idproof\":\"Aadhaar\",\"idproofdetails\":\"1234567890123458\",\"bloodgroup\":\"A+\",\"email\":\"conatactme@xyz.com\",\"contactnumbers\":\"1234567890,0987654321\",\"ptsource\":\"flyers\",\"entitylocation\":\"location1\",\"familyid\":\"\",\"referred_by\":\"\",\"referred_bykey\":\"\",\"extphid\":\"\",\"patienttype\":\"None\",\"tag\":\"\",\"religion\":\"Hindu\",\"fileno\":\"\",\"occupation\":\"Software Engineer\"}";

public static void main(String[] args) throws HttpException, IOException, NoSuchAlgorithmException {
RegPatient client = new RegPatient();
client.regPatient(USERNAME);
}

public void regPatient(String username) throws HttpException, IOException, NoSuchAlgorithmException {
String contentToEncode = "";
String currentDate = new SimpleDateFormat(DATE_FORMAT).format(new Date());
String contentType = "application/x-www-form-urlencoded";
HttpPost post = new HttpPost(URL + "api/register/patient/" + Entity);

StringEntity params =new StringEntity(sampleJson);

post.addHeader("content-type", contentType);
post.setEntity(params);

String verb = post.getMethod();
String contentMd5 = calculateMD5(contentToEncode);
String toSign = verb + "\n" + contentMd5 + "\n"
+ contentType + "\n" + currentDate + "\n" + "\n"
+ post.getURI().getPath();

String hmac = calculateHMAC(SECRET, toSign);
System.out.println("TEST HASH "+ calculateHMAC(SECRET, "test"));

post.addHeader("authorization", "MD " + username + ":" + hmac);
post.addHeader("date", currentDate);

System.out.println("String To Sign :" + toSign);
System.out.println("hmac:" + hmac);

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);


HttpEntity entity = response.getEntity();
// Read the contents of an entity and return it as a String.
String content = EntityUtils.toString(entity);
System.out.println(content);
System.out.println("\n\n");
System.out.println("client response:" + response.getStatusLine().getStatusCode());
}

private String calculateHMAC(String secret, String data) {
try {
SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
String result = new String(Base64.encodeBase64(rawHmac));
return result;
} catch (GeneralSecurityException e) {
throw new IllegalArgumentException();
}
}

private String calculateMD5(String contentToEncode) throws NoSuchAlgorithmException {
if (contentToEncode == "")
return "";

MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(contentToEncode.getBytes());
String result = new String(Base64.encodeBase64(digest.digest()));
return result;
}
}

Thanks,

*This post is locked for comments

I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans