Handling HTTP 303 Redirect in HTTP
When working with IPWorks HTTP-based components, a server may return a 303 See Other response after a request is made. This indicates that the original request was processed, but the client must retrieve the result from a different URL provided by the server.
A 303 response includes a Location header that specifies the URL to request next using a GET operation. To handle this, check the response status and follow the redirect by issuing a GET request to the value of the Location header.
Http client = new Http();
try
{
client.Post("http://www.a-server.com/some-resource");
}
catch (IPWorksHttpException ex)
{
if (ex.Code == 151) // 151 means there was an error in the HTTP response.
{
if (client.StatusLine.Contains("303")) // 303 is the HTTP response error.
{
foreach(Header header in client.ParsedHeaders)
{
if(header.Field.ToLower() == "location") // Location has the URL to GET next.
{
client.Get(header.Value);
}
}
}
}
}
This ensures the client correctly retrieves the resource specified by the server after the redirect.
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.