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

Connecting to the sever through apex

We can connect to an external server through HTML code which is written below:




<INPUT NAME="version" TYPE="text" VALUE="1.0"> </INPUT> 

<INPUT NAME="requestId" TYPE="text" VALUE="2010028"></INPUT>


<INPUT NAME="queryFlags" TYPE="text" VALUE="enable-strong-identification+return-ssn+return-user-name">

<INPUT type="submit" value="Send"></INPUT>




But connecting to the same server with Apex code is creating problems. We are getting :


System.HttpRequest[Endpoint=, Method=POST]

System.HttpResponse[Status=Moved Temporarily, StatusCode=302]


Following is the Apex code snippet:




Http http = new Http(); 

HttpRequest req = new HttpRequest();  

HttpResponse res = new HttpResponse(); 

req.setBody('version='+EncodingUtil.urlEncode(version, 'UTF-8')+'&requestId='+EncodingUtil.urlEncode(requestId, 'UTF-8')+'&customerId='+EncodingUtil.urlEncode(customerId, 'UTF-8')+'&queryFlags='+EncodingUtil.urlEncode(queryFlags, 'UTF-8'));           


I could not understand why this error is coming. 


please suggest.


I'd be surprised if Apex was having any effect on the server responding with a 302.


The 302 status code means that the resource you have requested has been temporarily relocated.  In practice this is a very common way of redirecting the browser to a different page and changing the request to a GET.  I'd imagine that the server carries out whatever processing is required based on the form and then redirects the browser to the results page.


The browser will automatically follow this redirect, but an HTTPRequest will simply return it to your code.  

Rahul SharmaRahul Sharma

try using above HTML in vf page instead of apex.

I also was facing similar problem in my one of the projects, but with some other 3rd-party app.