Making a Http GET callout to internal Salesforce URL

If you have requirement of call-out to internal Salesforce url from logged in session, Use your code as:

Http h = new Http();
HttpRequest webReq = new HttpRequest();
webReq.setMethod('GET');
webReq.setHeader('Authorization','Bearer '+UserInfo.getSessionId());
// replace endpoint url
webReq.setEndpoint('https://na12.salesforce.com/udd/Site/siteDashboard.apexp');
HttpResponse res = h.send(webReq);
System.debug(' ------ '+res.getbody());

Basically request need to be authorized. That is why you were redirected to login page. I assume that you have already added endpoint url in remote site setting of organization.


If you're just looking to get the content of the VisualForce page you can use the PageReference getContent() instance method (docs). Looking at the docs it's available for use in callouts.

You can call internal URLs as well as Salesforce APIs from within APEX but it's not easy and not supported. I haven't done this myself, but I believe setting a cookie with the current user's session id would be sufficient. Of course Salesforce doesn't give full access to headers or cookies, so you might run into issues. One example of consuming a Salesforce SOAP API is the APEX Metadata API Wrapper by @AndrewFawcett. It has loads of interesting techniques and discussion in the readme.