---
title: "android httpclient 302自动跳转"
author: "phenom"
date: "2012-07-26T11:03:43Z"
tags: []
categories: ["移动开发"]
slug: "android-httpclient-302"
draft: false
lastmod: "2012-07-26T11:03:43Z"
id: 1607683
url: "https://phenom.iteye.com/blog/1607683"
---

在eoe发了一篇文章，没有人解答：http://www.eoeandroid.com/thread-186939-1-1.html
相同的代码在pc上使用是正常的，我需要获取location,，所以使用post方法提交数据而后不会自动跳转到结果页面，但在android却自动跳转了。找了半天没有答案，后来经过提点，知道覆盖一个方法就可以了。
DefaultHttpClient defaultHttpClient=(DefaultHttpClient) client;
             defaultHttpClient.setRedirectHandler(new RedirectHandler() {
                                 
                                 @Override
                                 public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
                                         Log.d("isRedirectRequested.context:"+context.toString());
                                         return false;
                                 }
                                 
                                 @Override
                                 public URI getLocationURI(HttpResponse response, HttpContext context)
                                                 throws ProtocolException {
                                       
                                         return null;
                                 }
                         });就可以了，不会自动跳转了
接下来依旧可以获取header，然后读取里面的location:
isRedirectRequested.context:org.apache.http.protocol.DefaultedHttpContext@406010d8
header:Server val:nginx/1.2.0
header:Date val:Thu, 26 Jul 2012 02:54:54 GMT
header:Content-Length val:0
header:Connection val:keep-alive
header:Pragma val:No-cache
header:Cache-Control val:no-cache
header:Expires val:Thu, 01 Jan 1970 00:00:00 GMT
header:Api-Server-IP val:10.75.25.115
header:Location val: