How to send request and get response from server when working with Rest API.
Best Answer
Http http1=new Http();
HttpRequest req1=new HttpRequest();
req1.setendpoint('URL');
req1.Setheader('String','String');
req1.setmethod('POST'); //you can also SET method `GET` with Get there is no need of req1.setbody()
req1.setbody(string);
HttpResponse res1;
res1 = http1.send(req1);
String str=res1.getbody();
You are doing nothing wrong.
The HTTP request object provided by Salesforce does not handle 302 redirects like your browser does.
You can try handling redirects by running the callout again on the redirected site, as shown in this answer, or you can use a tool to find out what the ultimate endpoint is, like redirect detective.
As long as you are able to hit this ultimate end-point site in your browser, you should be able to query it from the Salesforce HTTP object. Make your HTTP object query it, and it should all work.
Best Answer