.net - temporarily - ¿Cómo verifico una respuesta 302? Solicitud web
status: 302(found) redirection-the client must take additional action to complete the request. (3)
Usando WebRequest
quiero saber si obtengo un
"302 movido temporalmente"
Respuesta en lugar de obtener automáticamente la nueva URL.
Al igual que:
HttpWebResponse response;
int code = (int) response.StatusCode;
El código debe ser
HttpStatusCode.TemporaryRedirect
Si desea detectar una respuesta de redireccionamiento, en lugar de WebRequest
cree automáticamente la WebRequest
y establezca la propiedad AllowAutoRedirect
en false
:
HttpWebRequest request = WebRequest.Create(someUrl) as HttpWebRequest;
request.AllowAutoRedirect = false;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.Redirect ||
response.StatusCode == HttpStatusCode.MovedPermanently)
{
// Do something here...
string newUrl = response.Headers["Location"];
}
VB Net Code
Function GetRealUrl(someUrl As String) As String
Dim req As HttpWebRequest = TryCast(WebRequest.Create(someUrl), HttpWebRequest)
req.AllowAutoRedirect = False
Dim response As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
If response.StatusCode = HttpStatusCode.Redirect OrElse response.StatusCode = HttpStatusCode.MovedPermanently Then
'' Do something...
Dim newUrl As String = response.Headers("Location")
getrealurl = newUrl
Else
getrealurl = someUrl
End If
End Function