Hello All,
I am using Ms Dynamics 365 V9.2 on line
I am trying to get geolocation details from elastic Search and populate crm. I created a custom workflow which runs on create a contact entity or update some fields data and set gps details (latitude and longitude).
I am facing on an issue when i call Elastic search via httpclient. postAsync() it returns statucode Forbidden. But the same code in my console app works fine.
I call the below method in codeactivity Execute method.
Thanks for your help.
Ps:
Here is a part of my code.
public JToken LoadES(string addressCRM, string key, string url, ITracingService tracingService)
{
HttpClientHandler handler = new HttpClientHandler();
handler.UseProxy = true;
HttpClient _httpClient = new HttpClient(handler);
JToken gpsToken = null;
var bodydata = @"{'size': 1, 'query': {'match': { 'full_address': {'query':'" + addressCRM + "','fuzziness': 'AUTO'}}}}";
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault();
using (var stream = new MemoryStream())
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
using (var jsonWriter = new JsonTextWriter(streamWriter))
{
jsonSerializer.Serialize(jsonWriter, bodydata);
jsonWriter.Flush();
HttpContent body = new StreamContent(stream);
body.Headers.ContentType = new MediaTypeHeaderValue("application/json");
_httpClient.DefaultRequestHeaders.Add("Authorization", key);
// this response is not success .
var response = _httpClient.PostAsync(url, body).Result;
if (response.IsSuccessStatusCode)
{
tracingService.Trace("Success Post Async response.... ");
var result = response.Content.ReadAsStringAsync();
var res = result.Result;
if (!String.IsNullOrEmpty(res))
{
JObject jresponse = JObject.Parse(res);
gpsToken = jresponse["hits"]["hits"][0]["_source"]["gps"];
}
}
else {
tracingService.Trace("Post Async Failed.... " +response.ReasonPhrase);
}
}
return gpsToken;
}