android 测试重定向

package irdc.EX08_01;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class EX08_01 extends Activity
{
  /*ㄢButtonン,籔TextViewン*/
  private Button mButton1;
  private TextView mTextView1;
  
  
/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    mButton1 =(Button) findViewById(R.id.myButton1);
    mTextView1 = (TextView) findViewById(R.id.myTextView1);
    
    mButton1.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {
       try{
       HttpClient httpClient = new HttpClient();
   String url =
"http://60.29.239.99/rdown/96-all/2009-07/14/583/372583252109275244.mp3?at=ymd6";
  
//url = "http://192.168.2.229:8088/YichaMarket/";
   Log.v(
"lunzi", "-------------url="+url);
   PostMethod postMethod = new PostMethod(url);
   Log.v(
"lunzi", "-------------1=");
   int statusCode = httpClient.executeMethod(postMethod);
  
   Log.v(
"lunzi", "-------------2=url="+postMethod.getURI());
  
   if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
   || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  
// 从头中取出转向的地址
   Header locationHeader = postMethod.getResponseHeader(
"location");
   String location = null;
   if (locationHeader != null) {
   location = locationHeader.getValue();
   Log.v(
"lunzi", "The page was redirected to:" + location);
   } else {
   Log.v(
"lunzi", "Location field value is null.");
   }
   return;
   }
  
   Header locationHeader = postMethod.getResponseHeader(
"location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
Log.v(
"lunzi", "The page was redirected to:" + location);
}

   mTextView1.setText(
"POST Response statusCode: "+statusCode+" url:"+locationHeader);
        }
        catch (Exception e)
        {  
          mTextView1.setText(e.getMessage().toString());
          e.printStackTrace();  
        }  
        
      }
    });
  }
}

lunzi   2011-06-24 16:16:42 评论:1   阅读:1086   引用:0
302 @2012-11-06 18:27:07  evil
为什么我用httpwatch获得的结果是302重定向的,但是在android中确是200。

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog