[SalesForce] HTTP Post issue – unable to redirect when authenticated

We are experiencing an issue with our HTTP Post method. We have the need to POST some information to an external server. We have created some code in visualforce to do this for us but it seems to only be functioning when we are not authenticated. What I mean by this is that unless this visualforce code is posted on a public facing webpage – the post does not function correctly.

Here is the visualforce code that we have:

<form action="https://myservice.com/myservice"
    method="post" target="_blank">
        <input type="hidden" name="productId" value="2129422189481977244397508903997509"></input> 
        <input type="hidden" name="param1" value="1234567891"></input>      
        <input type="hidden" name="param2" value="1234567891"></input>         
        <input type="hidden" name="param3" value="120"></input> 
        <input type="hidden" name="param4" value="120"></input> 
        <input type="hidden" name="param5" value="true"></input> 
        <input type="submit" value="Submit"></input>
</form>

Again this code works from any public facing page as well as when it is hosted on an entirely separate site, other than Salesforce.

I have added the URL into my Remote Site Settings as well.

Thanks in advance for the help.

Edit 1:

I know it is not working because when it is hosted on a public salesforce page or hosted on a non salesforce server I am correctly redirected to the webpage that is returned from the post. When I am authenticated – it simply stays on the page I am at, doing nothing.

Edit 2:

Wrote a script to connect to the server using the HTTP class and am receiving a 302 Redirect error when attempting to connect to the server under an authenticated user. The exact same code works as intended when run underneath a non-authenticated user (Site Guest User).

Edit 3:

I have tried using the Location header to post against the URL that is passed back – but posting against that new URL returns the exact same status code. 302. When I run this same code under a non authenticated user (public facing web page) – I am given the 200 OK response and, if posted through a form on the visualforce page – forwards the user to the correct webpage.

 if(res.getStatusCode() >= 300 && res.getStatusCode() <= 307){

        String location = res.getHeader('Location');
        system.debug(location);
        if(location != null){
            req = new HttpRequest();
            req.setEndpoint(location);
            req.setMethod('POST');
            req.setHeader('Content-Type', 'text/xml');
            req.setHeader('Content-Length', String.valueOf(body.length()));
            req.setBody(body);
            res = http.send(req);
            system.debug('Status Code: ' + res.getStatusCode());
            system.debug('2nd location: ' + res.getHeader('Location'));
            //System.assertEquals(200, res.getStatusCode());
        }
    }

Best Answer

Turns out if you embed a form tag inside of an apex:form tag - it modifies the values of it (including the URL you have inside of the action attribute). This was causing the page to attempt to post against the salesforce server itself instead of my external site.