Calling a REST service from plugin RRS feed

  • Question

  • Hi,

    I am trying call a REST service from plugin to do some validations, following code i am using,

    HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
                webrequest.Method = WebRequestMethods.Http.Get;
                webrequest.ContentType = "application/json";
                webrequest.ContentLength = 0;            
                string result;
                using (WebResponse response = webrequest.GetResponse())
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        result = reader.ReadToEnd();

    The webservice is a public webservice, which does not need any authentication. I am getting exception saying "remote server could not be connected".  However I am able to connect to REST service using the above code from a console application from the same server(CRM app server). I am able to open the URL from the browser with in the server.

    Is there anything which is stopping me to connect the REST service from the plugin ?

    Many thanks....

    Wednesday, May 7, 2014 7:33 PM

All replies

  • There shouldn't be any problems calling a REST service within a plugin. Some things to check are:

    • Is this running in the sandbox, and if so, can you move the assembly out of the sandbox to see if it behaves differently ? Similarly, if it's not in the sandbox, try adding it in the sandbox
    • Can you access any other urls (non-REST ones) from a plugin ?
    • Does the error come quickly (within a few seconds), or does it take longer (which would indicate a timeout) ?

    One possibility could be that firewall / AV software is blocking the request. When the code runs in the plugin, it will probably run under a different identity, and in a different process, compared to a console application

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, May 8, 2014 8:25 AM
  • This is just a guess, but it's possible the request is rejected because it is detecting a cross domain/CORS script call.

    You can try adding a callback function to the parameters you send to the server in you URL (essentially invoking JSONP) and then parsing the returned value (use content type 'application/javascript' for your HttpRequest). Of course, the web service you are calling would have to support JSONP returns.

    Assuming the web service does support JSONP, (while not a complete example) this might be of some help: http://msdn.microsoft.com/en-us/library/jj819168.aspx

    Thursday, May 8, 2014 8:54 PM
  • Hi,

    This is not running in sandbox, I have also registered it in sandbox and tried. No luck.

    Tthe code takes long time when it reaches (WebResponse response = webrequest.GetResponse()) line. then it throws exception "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond".

    For a change I tried accessing google.com as url but no luck. it throws "operation timeout" exception.

    Is there something to do with firewall or security ? When I browse the webserice from my server it opens without any problem. And it is a public webservice.

    Does anyone experienced same problem ?

    Sunday, May 11, 2014 11:32 AM