HTTP Classes

These classes expose the general HTTP request/response functionality:

The HttpRequest and HttpResponse classes support the following elements:

The following example shows an HTTP GET request made to the external server specified by the value of url that gets passed into the getContent method. This example also shows accessing the body of the returned response:

public class HttpCalloutSample {

// Pass in the endpoint to be used using the string url 
    
  public String getContent(String url) {

// Instantiate a new http object 
    
    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);
    return res.getBody();
  }
}

Before you can access external servers from an endpoint or redirect endpoint using Apex or any other feature, you must add the remote site to a list of authorized remote sites in the Salesforce user interface. To do this, log in to Salesforce and select Your Name | Setup | Security Controls | Remote Site Settings.

Note
The AJAX proxy handles redirects and authentication challenges (401/407 responses) automatically. For more information about the AJAX proxy, see AJAX Toolkit documentation.

Use the DOM Classes to parse XML content in the body of a request created by HttpRequest or a response accessed by HttpResponse.

© Copyright 2000–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.