+ Start a Discussion

Http callouts and cookies using Apex

I have an urgent issue. The goal is to update the current user's profile via Apex. Since I can't do this as myself, I make an HTTP callout and log into the Web service API as another user. In principle this should work, and my code is below.
The problem is that the sid session cookie is not recognized when I call the myApexPage page and I get a redirect back to the login Url. Are there issues around passing cookie headers using Apex? Is sid not the right cookie name? I've tried setting the cookie domain and path properties but this doesn't help.
Any help is very much appreciated.
Http h = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = h.send(req); 
string loc = res.getHeader('Location');
if (loc.indexOf('sid=') > -1)
            string sid = loc.substring(loc.indexOf('sid=')+4, loc.indexOf('je=')-1);
            req = new HttpRequest();
            res = h.send(req);
            system.debug('Response: '+res.getBody());
Why are you trying to screen scape an apex page, use an apex web service, or the regular api if you want to do something programatically.

But if I could do these readily I would.


So I create an Apex web service but how do I log in as another user and invoke it? I can redirect to an scontrol, log in and call execute but I end up with the credentials on the page and this is a public PRM. If I could call the Web Service API via Apex I'd do this, but the Soap syntax isn't documented and it could days to get this right.


If anyone can help with this I'd really appreciate it, thanks.




You have two calls to setHeader with same key. Second call will just over write the first value.
Is that what you want?


HI Dean,


Can you please share the code which you used to fix it,actually I hv the same requirement and could nt succesfully ablr to implememnt.


Hi Dean, Can you please let me know how you had done this finally.Could you please share the code.


Hi Dean,


Can you please help me?

Jia HuJia Hu
I can login and access the VF page with this method, but cannot really retrieve the data of VF page.

Does anyone know this?