function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kkkkkk 

How to write HttpRequest and Response for Servlet?

Hi all,

 

I need help on how to write a http request with servlet.

Here is the situation:

I have to call this url:

     http://x.x.x.x/dev_back/CustomerServlet?action=0&user=john

From the url, I will get all customer data in JSON Object, which I have to parse later and save it into Customer object in Salesforce.

 

Since I'm new in backend programming and webservices call, could anyone help me on how to get the response from those url in apex code?

Here is the code that I have until now (sorry, if I'm maybe completely wrong on this):

 

String url = 'http://x.x.x.x/dev_back/CustomerServlet?action=0&user=john';

Http h = new Http();

// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint 
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');

// Send the request, and return a response 
    
HttpResponse res = h.send(req);

 But with the code above, I'm not be able to get the response. When I take a look on res.getBody() message, I saw that the url is being cut off, it takes only "http://x.x.x.x/dev_back/CustomerServlet?" as the string url.

 

How can I solve this?

 

Thanks!

Jia HuJia Hu
There is an example,
Sample: Parsing a JSON String and Deserializing It into Objects
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_jsonparser.htm