HttpResponse code 302
Asked Answered
A

1

6

I am using simulator BB 8900. I am trying to connect to url and get response code 302.What does it mean? Here is my code snippet:

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
.....
connection = (HttpConnection)Connector.open(url);
responseCode = connection.getResponseCode();
Attractant answered 26/4, 2012 at 8:30 Comment(4)
check this w3.org/Protocols/rfc2616/rfc2616-sec10.htmlJusticiar
You forgot to ask a question. This is perfectly normal, by the way. Decide if you want to process the response or not, and if so, follow the redirect or do whatever is appropriate in your situation. (One of my pet peeves, by the way, is people who implement defective HTTP servers or clients and skip huge portions of the protocol. Someone else always winds up getting blamed for their omission because their code "works with everyone else's stuff".)Carpal
httpcats.herokuapp.com/302Noyes
The data requested actually resides under a different URL, however, the redirection may be altered on occasion (when making links to these kinds of document, the browser should default to using the Udi of the redirection document, but have the option of linking to the final document) as for "Forward". The response format is the same as for Moved .Sibyl
S
14

An HTTP 302 is a 'temporary redirect'. You need to handle it.

As per the standard, if you get a 302 response, the response will contain a 'Location' header field with the redirect:

Client request:
GET /index.html HTTP/1.1
Host: www.example.com

Server response:
HTTP/1.1 302 Found
Location: http://www.redirected-address.example.com/

You need to extract the new URL from the response. (Use getHeaderField("Location") to do this). Then execute the same method on the new URL you got.

Two other points:

  1. Since this is a 'temporary' redirect, you cannot store this new URL. You should keep using the old one, and if it returns a 302, then use whatever URL is in 'Location'.

  2. If you are not executing a GET or HEAD, you shouldn't do the redirect automatically. Instead ask for user intervention. The RFC requires this.

Simulate answered 26/4, 2012 at 8:46 Comment(1)
re 2) this really is about safe/non-safe, not GET/HEAD in particular. Also note that HTTPbis changed the UI requirement (see trac.tools.ietf.org/wg/httpbis/trac/ticket/238)Eddyede

© 2022 - 2024 — McMap. All rights reserved.