c# - HttpRequest POST to RESTful web service - Salesforce Apex Callout -


i have created restful web service (c#, wcf) implements following interface:

public interface itestservice {     [operationcontract]     [webinvoke(method = "post",      responseformat = webmessageformat.json,      bodystyle = webmessagebodystyle.bare,      uritemplate = "?s={astr}")]     string test(string astr); } 

where test() method returns whatever given (or default "test" string) - and timestamps when method called.

the service publicly available when enter url in browser:

http://xx.xxx.xxx.xx:41000/testservice/web/ 

it returns json "test" string (or whatever might have been entered ?s=... @ end).


i want salesforce post data web service.

my apex class looks - gets triggered when object gets inserted in salesforce:

public class webservicecallout  {     @future (callout=true)     public static void sendnotification(string name)      {         httprequest req = new httprequest();         httpresponse res = new httpresponse();         http http = new http();          req.setendpoint('http://xx.xxx.xxx.xx:41000/testservice/web/');         req.setmethod('post');         req.setheader('content-type', 'application/json');         req.setbody('');          try          {             res = http.send(req);         }          catch(system.calloutexception e)          {             system.debug('callout error: '+ e);             system.debug(res.tostring());         }     } } 

when object inserted salesforce, apex jobs section says sendnotification() method completed. service never picked post method. (note: service ip in remote site settings has been added).

is there wrong syntax?

(at stage want salesforce invoke web service - without posting it)


as example have created sample console application works fine posting service.

internal static void main(string[] args) {     uri address = new uri("http://xx.xxx.xxx.xx:41000/testservice/web/");      // create web request     httpwebrequest request = (httpwebrequest)webrequest.create(address);      // set type post     request.method = "post";     request.contenttype = "application/json";      // create data want send       var postdata = "";      // create byte array of data want send     var bytedata = utf8encoding.utf8.getbytes(postdata);      // set content length in request headers     request.contentlength = bytedata.length;      // write data     using (var stream = request.getrequeststream())     {         stream.write(bytedata, 0, bytedata.length);     }      // response       var response = (httpwebresponse)request.getresponse();      var responsestring = new streamreader(         response.getresponsestream()         ).readtoend();      console.writeline(responsestring); } 

why doesn't apex class in salesforce callout correctly?

i figured out problem. didn't specify port in remote site settings (what!!)

the above code samples should work flawlessly (at least me)


hints whoever gets stuck in future:

in setup -> security -> remote site settings

the service (remote site url) should in format - ever host it:

http://xx.xxx.xxx.xx:41000 

to test above code go to:

[your name] -> developer console -> debug -> open execute anonymous window

and copy following line:

webservicecallout.sendnotification('test'); 

and click execute.

code executes , should see execution log (and errors it).


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -