android 登录Javaeye(使用HttpURLConnection和HttpClient)

之前在网上看过好多人用java写登录网站,这次正好学习android,自己写实习一个,就拿经常登录的javaeye(现在叫iteye,已经是csdn的了)试一下。

先来分析javaeye登录界面


查看源文件

  1. <form action="/login" id="login_form" method="post">            <table border="0" cellspacing="0" cellpadding="0" class="table_1">  
  2.     <colgroup><col width="60" /><col /></colgroup>  
  3.     <tr>  
  4.       <th>账号</th>  
  5.       <td>  
  6.         <input class="input_1 required" id="user_name" name="name" placeholder="用户名或邮箱" tabindex="1" type="text" value="" />  
  7.       </td>  
  8.     </tr>  
  9.     <tr>  
  10.       <th>密码</th>  
  11.       <td>  
  12.         <input class="input_1 required" id="password" name="password" tabindex="2" type="password" value="" /></td>  
  13.     </tr>  
  14.     <tr>  
  15.       <th> </th>  
  16.       <td>  
  17.         <input id="auto" name="remember_me" tabindex="3" type="checkbox" value="1" />  
  18.         <label for="auto">下次自动登录</label>  
  19.         <a href="/users/forget">忘记密码?</a>  
  20.       </td>  
  21.     </tr>  
  22.     <tr>  
  23.       <th> </th>  
  24.       <td><input type="submit" name="button" id="button" value="登 录" class="btn_1 submit" tabindex="4" /></td>  
  25.     </tr>  
  26.   </table>  
  27. </form>  

可以分析出post地址为http://www.iteye.com/login,账号是name、密码是password。

登录以后,使用chrome的开发人员工具(其他浏览器也有相关工具,我就不一一说了)拦截获取响应消息


可以看出返回的是302地址重定向


还需要注意的是每次提交时,不能遗漏的cookie即seesion_id ,javaeye用的是_javaeye3_session

接下来是我做的android的例子,使用webview控件,它只负责内容展现,请求响应我分别使用了HttpURLConnection和HttpClient。

不说了,上代码:

主布局main.xml代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/RelativeLayout1"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/textViewInfo"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true"/>  
  14.           
  15.     <WebView  
  16.         android:id="@+id/webViewInfo"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="260dp"  
  19.         android:layout_below="@+id/textViewInfo"  
  20.         android:layout_marginTop="30dp"  
  21.         android:layout_alignParentLeft="true" />  
  22.   
  23.     <LinearLayout  
  24.         android:id="@+id/linearLayout2"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_above="@+id/linearLayout1"  
  28.         android:layout_alignParentLeft="true" >  
  29.         <TextView  
  30.             android:id="@+id/textView2"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="使用Java" />  
  34.               
  35.         <Button  
  36.             android:id="@+id/buttonJavaLogin"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.              android:layout_toRightOf="@+id/textView2"  
  40.             android:text="登录ITeye" />  
  41.       
  42.         <Button  
  43.             android:id="@+id/buttonJavaMyMessage"  
  44.             android:layout_width="wrap_content"  
  45.             android:layout_height="wrap_content"  
  46.             android:layout_toRightOf="@+id/buttonJavaLogin"  
  47.             android:text="ITeye收件箱" />  
  48.     </LinearLayout>  
  49.       
  50.     <LinearLayout  
  51.         android:id="@+id/linearLayout1"  
  52.         android:layout_width="wrap_content"  
  53.         android:layout_height="wrap_content"  
  54.         android:layout_alignParentBottom="true"  
  55.         android:layout_alignParentLeft="true" >  
  56.         <TextView  
  57.             android:id="@+id/textView1"  
  58.             android:layout_width="wrap_content"  
  59.             android:layout_height="wrap_content"  
  60.             android:text="使用Apache" />  
  61.               
  62.         <Button  
  63.             android:id="@+id/buttonApacheLogin"  
  64.             android:layout_width="wrap_content"  
  65.             android:layout_height="wrap_content"  
  66.              android:layout_toRightOf="@+id/textView1"  
  67.             android:text="登录ITeye" />  
  68.       
  69.         <Button  
  70.             android:id="@+id/buttonApacheMyMessage"  
  71.             android:layout_width="wrap_content"  
  72.             android:layout_height="wrap_content"  
  73.             android:layout_toRightOf="@+id/buttonApacheLogin"  
  74.             android:text="ITeye收件箱" />  
  75.     </LinearLayout>  
  76.   
  77. </RelativeLayout>  

登录对话框布局login.xml代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/linearLayout1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content" >  
  11.   
  12.         <TextView  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="账号: " />  
  16.   
  17.         <EditText  
  18.             android:id="@+id/editTextUsername"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_weight="1" />  
  22.   
  23.     </LinearLayout>  
  24.   
  25.     <LinearLayout  
  26.         android:id="@+id/linearLayout2"  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content" >  
  29.   
  30.         <TextView  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="密码: " />  
  34.   
  35.         <EditText  
  36.             android:id="@+id/exitTextPassword"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_weight="1"  
  40.             android:inputType="textPassword" />  
  41.   
  42.     </LinearLayout>  
  43.   
  44. </LinearLayout>  

AndroidManifest.xml不要忘了加如下代码:

  1. <uses-permission android:name="android.permission.INTERNET"/>  

主程序代码如下:

  1. package com.zhang.test08_16;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.BufferedReader;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.InputStreamReader;  
  9. import java.io.OutputStream;  
  10. import java.io.OutputStreamWriter;  
  11. import java.io.UnsupportedEncodingException;  
  12. import java.io.Writer;  
  13. import java.net.HttpURLConnection;  
  14. import java.net.MalformedURLException;  
  15. import java.net.ProtocolException;  
  16. import java.net.URL;  
  17. import java.util.ArrayList;  
  18. import java.util.List;  
  19.   
  20. import org.apache.http.HttpEntity;  
  21. import org.apache.http.HttpResponse;  
  22. import org.apache.http.HttpStatus;  
  23. import org.apache.http.NameValuePair;  
  24. import org.apache.http.client.ClientProtocolException;  
  25. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  26. import org.apache.http.client.methods.HttpGet;  
  27. import org.apache.http.client.methods.HttpPost;  
  28. import org.apache.http.client.methods.HttpUriRequest;  
  29. import org.apache.http.impl.client.DefaultHttpClient;  
  30. import org.apache.http.message.BasicNameValuePair;  
  31. import org.apache.http.params.CoreProtocolPNames;  
  32. import org.apache.http.params.HttpParams;  
  33. import org.apache.http.protocol.HTTP;  
  34. import org.apache.http.util.EntityUtils;  
  35.   
  36. import android.app.Activity;  
  37. import android.app.AlertDialog;  
  38. import android.content.DialogInterface;  
  39. import android.os.Bundle;  
  40. import android.view.LayoutInflater;  
  41. import android.view.View;  
  42. import android.webkit.WebView;  
  43. import android.widget.Button;  
  44. import android.widget.EditText;  
  45. import android.widget.TextView;  
  46.   
  47. public class Test08_16Activity extends Activity {  
  48.       
  49.     private TextView textViewInfo;  
  50.     private WebView webViewInfo;  
  51.     private Button buttonJavaLogin;  
  52.     private Button buttonJavaMyMessage;  
  53.     private Button buttonApaceLogin;  
  54.     private Button buttonApacheMyMessage;  
  55.       
  56.     private View loginView;  
  57.     private AlertDialog loginDialog;  
  58.     private EditText editTextUserName;  
  59.     private EditText editTextPassword;  
  60.       
  61.     private String clientType;  
  62.     private DefaultHttpClient client;  
  63.     private String cookie;  
  64.       
  65.     private static final String LOGIN_URL = "http://www.iteye.com/login";  
  66.     private static final String MESSAGE_URL = "http://app.iteye.com/messages";  
  67.       
  68.     @Override  
  69.     public void onCreate(Bundle savedInstanceState) {  
  70.         super.onCreate(savedInstanceState);  
  71.         setContentView(R.layout.main);  
  72.           
  73.         textViewInfo = (TextView)findViewById(R.id.textViewInfo);  
  74.         webViewInfo = (WebView)findViewById(R.id.webViewInfo);  
  75.         buttonJavaLogin = (Button)findViewById(R.id.buttonJavaLogin);  
  76.         buttonJavaMyMessage = (Button)findViewById(R.id.buttonJavaMyMessage);  
  77.         buttonApaceLogin = (Button)findViewById(R.id.buttonApacheLogin);  
  78.         buttonApacheMyMessage = (Button)findViewById(R.id.buttonApacheMyMessage);  
  79.           
  80.         LayoutInflater inflater = LayoutInflater.from(this);  
  81.         loginView = inflater.inflate(R.layout.login, null);  
  82.         editTextUserName = (EditText) loginView.findViewById(R.id.editTextUsername);  
  83.         editTextPassword = (EditText) loginView.findViewById(R.id.exitTextPassword);  
  84.         loginDialog = getLoginDialog();  
  85.           
  86.         buttonJavaLogin.setOnClickListener(new View.OnClickListener() {  
  87.               
  88.             @Override  
  89.             public void onClick(View v) {  
  90.                 clientType = "java";  
  91.                 loginDialog.show();  
  92.             }  
  93.         });  
  94.         buttonJavaMyMessage.setOnClickListener(new View.OnClickListener() {  
  95.               
  96.             @Override  
  97.             public void onClick(View v) {  
  98.                 showMyMessageJava();  
  99.             }  
  100.         });  
  101.           
  102.         buttonApaceLogin.setOnClickListener(new View.OnClickListener() {  
  103.               
  104.             @Override  
  105.             public void onClick(View v) {  
  106.                 clientType = "apache";  
  107.                 loginDialog.show();  
  108.             }  
  109.         });  
  110.         buttonApacheMyMessage.setOnClickListener(new View.OnClickListener() {  
  111.               
  112.             @Override  
  113.             public void onClick(View v) {  
  114.                 showMyMessageApache();  
  115.             }  
  116.         });  
  117.     }  
  118.   
  119.     @Override  
  120.     protected void onResume() {  
  121.         client = new DefaultHttpClient();//client.getCookieStore().getCookies()  session_id  
  122.         HttpParams httpParams = client.getParams();  
  123.         httpParams.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);  
  124.           
  125.         super.onResume();  
  126.     }  
  127.       
  128.     @Override  
  129.     protected void onPause() {  
  130.         client.getConnectionManager().shutdown();  
  131.           
  132.         super.onPause();  
  133.     }  
  134.       
  135.     private AlertDialog getLoginDialog() {  
  136.         return new AlertDialog.Builder(Test08_16Activity.this)  
  137.             .setTitle("登录")  
  138.             .setView(loginView)  
  139.             .setPositiveButton("登录"new DialogInterface.OnClickListener() {  
  140.   
  141.                 @Override  
  142.                 public void onClick(DialogInterface dialog, int which) {  
  143.                     if (clientType.equals("apache")) {  
  144.                         loginApache();  
  145.                     } else if(clientType.equals("java")) {  
  146.                         loginJava();  
  147.                     }  
  148.                 }  
  149.             })  
  150.             .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  151.                   
  152.                 @Override  
  153.                 public void onClick(DialogInterface dialog, int which) {  
  154.                 }  
  155.             })  
  156.             .create();  
  157.     }  
  158.   
  159.     private void loginJava() {  
  160.         String username = editTextUserName.getText().toString();  
  161.         String password = editTextPassword.getText().toString();  
  162.           
  163.         URL url = null;  
  164.         try {  
  165.             url = new URL(LOGIN_URL);  
  166.         } catch (MalformedURLException e) {  
  167.         }  
  168.           
  169.         HttpURLConnection urlConnection = null;  
  170.         try {  
  171.             urlConnection = (HttpURLConnection) url.openConnection();  
  172.         } catch (IOException e) {  
  173.             textViewInfo.setText(e.getMessage());  
  174.             return;  
  175.         }  
  176.         try {  
  177.             urlConnection.setRequestMethod("POST");  
  178.         } catch (ProtocolException e) {  
  179.         }  
  180.         urlConnection.setDoOutput(true);  
  181.         urlConnection.setRequestProperty("Content-Type""application/x-www-form-urlencoded");  
  182.         urlConnection.setRequestProperty("Connection""keep-alive");  
  183.         urlConnection.setInstanceFollowRedirects(false);//  
  184.           
  185.         OutputStream out = null;  
  186.         try {  
  187.             out = new BufferedOutputStream(urlConnection.getOutputStream());//请求  
  188.         } catch (IOException e) {  
  189.             urlConnection.disconnect();  
  190.             textViewInfo.setText(e.getMessage());  
  191.             return;  
  192.         }  
  193.           
  194.         Writer writer = null;  
  195.         try {  
  196.             writer = new OutputStreamWriter(out,"UTF-8");  
  197.         } catch (UnsupportedEncodingException e1) {  
  198.         }  
  199.         try {  
  200.             writer.write("name="+username +"&password="+password);  
  201.         } catch (IOException e) {  
  202.             urlConnection.disconnect();  
  203.             textViewInfo.setText(e.getMessage());  
  204.             return;  
  205.         } finally{  
  206.             try {  
  207.                 writer.flush();  
  208.                 writer.close();  
  209.             } catch (IOException e) {  
  210.             }  
  211.         }  
  212.           
  213.         getResponseJava(urlConnection);  
  214.     }  
  215.   
  216.     private void showMyMessageJava() {  
  217.         URL url = null;  
  218.         try {  
  219.             url = new URL(MESSAGE_URL);  
  220.         } catch (MalformedURLException e) {  
  221.         }  
  222.           
  223.         HttpURLConnection urlConnection = null;  
  224.         try {  
  225.             urlConnection = (HttpURLConnection) url.openConnection();  
  226.         } catch (IOException e) {  
  227.             textViewInfo.setText(e.getMessage());  
  228.             return;  
  229.         }  
  230.         //method  The default value is "GET"  
  231.           
  232.         getResponseJava(urlConnection);  
  233.     }  
  234.       
  235.     //共用HttpClient  
  236.     private void loginApache() {  
  237.         String username = editTextUserName.getText().toString();  
  238.         String password = editTextPassword.getText().toString();  
  239.           
  240.         List<NameValuePair> params = new ArrayList<NameValuePair>(1);  
  241.         params.add(new BasicNameValuePair("name", username));  
  242.         params.add(new BasicNameValuePair("password", password));  
  243.         HttpEntity formEntity = null;  
  244.         try {  
  245.             formEntity = new UrlEncodedFormEntity(params,HTTP.UTF_8);  
  246.         } catch (UnsupportedEncodingException e) {  
  247.         }  
  248.           
  249.         HttpPost request = new HttpPost(LOGIN_URL);  
  250.         request.setEntity(formEntity);  
  251.           
  252.         getResponseApache(request);  
  253.     }  
  254.       
  255.     private void showMyMessageApache() {  
  256.         HttpGet request = new HttpGet(MESSAGE_URL);  
  257.         getResponseApache(request);  
  258.     }  
  259.       
  260.     private void getResponseJava(HttpURLConnection urlConnection) {  
  261.         if(cookie != null) {//session_id  
  262.             urlConnection.setRequestProperty("Cookie", cookie);  
  263.         }  
  264.           
  265.         InputStream in = null;  
  266.         try {  
  267.             in = new BufferedInputStream(urlConnection.getInputStream());//响应  
  268.         } catch (IOException e) {  
  269.             urlConnection.disconnect();           
  270.             textViewInfo.setText(e.getMessage());  
  271.             return;  
  272.         }  
  273.           
  274.         cookie = urlConnection.getHeaderField("Set-Cookie");//session_id  
  275.         int responseCode = -1;  
  276.         try {  
  277.             responseCode = urlConnection.getResponseCode();  
  278.         } catch (IOException e) {  
  279.             urlConnection.disconnect();           
  280.             textViewInfo.setText(e.getMessage());  
  281.             return;  
  282.         }  
  283.         if(responseCode == 301 || responseCode == 302 || responseCode == 307) {//重定向  
  284.             String location = urlConnection.getHeaderField("location");  
  285.             URL url = null;  
  286.             try {  
  287.                 url = new URL(location);  
  288.             } catch (MalformedURLException e) {  
  289.             }  
  290.               
  291.             HttpURLConnection urlConnectionRedirect = null;  
  292.             try {  
  293.                 urlConnectionRedirect = (HttpURLConnection) url.openConnection();  
  294.             } catch (IOException e) {  
  295.                 textViewInfo.setText(e.getMessage());  
  296.                 return;  
  297.             }  
  298.               
  299.             getResponseJava(urlConnectionRedirect);  
  300.             return;  
  301.         }  
  302.           
  303.         BufferedReader reader = null;  
  304.         try {  
  305.             reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));  
  306.         } catch (UnsupportedEncodingException e1) {  
  307.         }  
  308.         StringBuilder result = new StringBuilder();  
  309.         String tmp = null;  
  310.         try {  
  311.             while((tmp = reader.readLine()) != null){  
  312.                 result.append(tmp);  
  313.             }  
  314.         } catch (IOException e) {  
  315.             textViewInfo.setText(e.getMessage());  
  316.             return;  
  317.         } finally {  
  318.             try {  
  319.                 reader.close();  
  320.                 urlConnection.disconnect();  
  321.             } catch (IOException e) {  
  322.             }  
  323.         }  
  324.         webViewInfo.loadDataWithBaseURL(null, result.toString(), "text/html""UTF-8"null);  
  325.     }  
  326.       
  327.     private void getResponseApache(HttpUriRequest request) {  
  328.         HttpResponse response = null;  
  329.         try {  
  330.             response = client.execute(request);//重定向 RedirectStrategy execute while (!done)   
  331.         } catch (ClientProtocolException e) {  
  332.             textViewInfo.setText(e.getMessage());  
  333.         } catch (IOException e) {  
  334.             textViewInfo.setText(e.getMessage());  
  335.         }  
  336.           
  337.         if (response == null) {  
  338.             return;  
  339.         }  
  340.           
  341.         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {  
  342.             textViewInfo.setText("error response" + response.getStatusLine().toString());  
  343.             return;  
  344.         }   
  345.           
  346.         String result = null;  
  347.         try {  
  348.             result = EntityUtils.toString(response.getEntity(),"UTF-8");  
  349.         } catch (Exception e) {  
  350.             textViewInfo.setText("error response" + response.getStatusLine().toString());  
  351.             return;  
  352.         }   
  353.         webViewInfo.loadDataWithBaseURL(null, result, "text/html""UTF-8"null);  
  354.     }  
  355. }  

看看运行效果:

主界面:


点击 使用java登录iteye,弹出登录对话框:


输入javaeye的账号和密码,点击登录(有点慢,耐心等待),结果如下:


登录成功了,点击 iteye收件箱(相当于在浏览器里点击收件箱),验证是同一session


成功,代码没有问题。

使用Apache的方式结果一样,我就不截图了。

做这个例子,我是想了解http协议,httpclient内部实现原理。写的过程中参照如下:

seesion问题:http://hi.baidu.com/%CE%A4%D7%D4%C9%FD/blog/item/e2ce0004f52f2c61030881fa.html

http://helin.iteye.com/blog/257115

302重定向:http://blog.csdn.net/hegch/article/details/1891146

http 417 http://blog.yes2.me/archives/915

webview乱码:http://hongyang321.iteye.com/blog/1021564

接下来分析一下代码:

先分析java部分,代码和我上篇博客node.js+android http请求响应基本很像,关键的不同

1.解决session的代码:

  1. private String cookie;  
  2.   
  3. if(cookie != null) {//session_id  
  4.     urlConnection.setRequestProperty("Cookie", cookie);  
  5. }  
  6.   
  7. cookie = urlConnection.getHeaderField("Set-Cookie");//session_id  

共享cookie,获取响应前查看是否有cookie,如果有则放在请求头上,获取响应后,拿到cookie存下来。

2.解决重定向的代码:

  1. if(responseCode == 301 || responseCode == 302 || responseCode == 307) {//重定向  
  2.     String location = urlConnection.getHeaderField("location");  
  3.     URL url = null;  
  4.     try {  
  5.         url = new URL(location);  
  6.     } catch (MalformedURLException e) {  
  7.     }  
  8.       
  9.     HttpURLConnection urlConnectionRedirect = null;  
  10.     try {  
  11.         urlConnectionRedirect = (HttpURLConnection) url.openConnection();  
  12.     } catch (IOException e) {  
  13.         textViewInfo.setText(e.getMessage());  
  14.         return;  
  15.     }  
  16.       
  17.     getResponseJava(urlConnectionRedirect);  
  18.     return;  
  19. }  
查看响应代码是否需要重定向,如果是读出响应头上的location,发出get请求,递归调用。

再来分析一下httpclient部分:

1.解决407 error

  1. HttpParams httpParams = client.getParams();  
  2. httpParams.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);  

2.解决session

  1. private DefaultHttpClient client;  

共用一个client就可以了,cookie存在这client.getCookieStore().getCookies(), 和我们使用一个浏览器,多个标签页共享session原理一样。

3.解决重定向

没有附加任何代码,httpclient内部已经实现,源码主要部分如下:

  1. class AbstractHttpClient  
  2.     public final HttpResponse execute(HttpUriRequest request)  
  3.         throws IOException, ClientProtocolException {  
  4.   
  5.         return execute(request, (HttpContext) null);  
  6.     }  
  7.     public final HttpResponse execute(HttpHost target, HttpRequest request,  
  8.                                       HttpContext context)  
  9.   
  10.      RequestDirector director = null;  
  11.      director = createClientRequestDirector    return new DefaultRequestDirector  
  12.      return director.execute(target, request, execContext);  
  13.   
  14. class DefaultRequestDirector   
  15.     public HttpResponse execute(HttpHost target, HttpRequest request,  
  16.                                 HttpContext context)  
  17.            while (!done) {  
  18.         RoutedRequest followup = handleResponse(roureq, response, context);  
  19.                 if (followup == null) {  
  20.                     done = true;  
  21.                 } else {  
  22.   
  23.     protected RoutedRequest handleResponse(RoutedRequest roureq,  
  24.                                            HttpResponse response,  
  25.                                            HttpContext context)  
  26.         if (HttpClientParams.isRedirecting(params) &&  
  27.                 this.redirectStrategy.isRedirected(request, response, context)) {  
  28.   
  29. class DefaultRedirectStrategy  
  30.     public boolean isRedirected(  
  31.             final HttpRequest request,  
  32.             final HttpResponse response,  
  33.             final HttpContext context)  
  34.   
  35.         int statusCode = response.getStatusLine().getStatusCode();  
  36.         String method = request.getRequestLine().getMethod();  
  37.         Header locationHeader = response.getFirstHeader("location");  
  38.         switch (statusCode) {  
  39.         case HttpStatus.SC_MOVED_TEMPORARILY:  
  40.             return (method.equalsIgnoreCase(HttpGet.METHOD_NAME)  
  41.                 || method.equalsIgnoreCase(HttpHead.METHOD_NAME)) && locationHeader != null;  
  42.         case HttpStatus.SC_MOVED_PERMANENTLY:  
  43.         case HttpStatus.SC_TEMPORARY_REDIRECT:  
  44.             return method.equalsIgnoreCase(HttpGet.METHOD_NAME)  
  45.                 || method.equalsIgnoreCase(HttpHead.METHOD_NAME);  
  46.         case HttpStatus.SC_SEE_OTHER:  
  47.             return true;  
  48.         default:  
  49.             return false;  
  50.         } //end of switch     

处理方法基本和我的一样,除了对象封装意外,它使用的条件循环 while (!done) ,我使用的是条件递归。

ps:使用windows7以后,根据内容找文件很蛋疼,推荐大家使用 UltraFileSearch 官网 http://www.ultrafilesearch.com/,不是广告,只是用了这么多搜索替代工具以后,觉得最好的一个,和大家分享一下

敲了半天怪累的,就说到这了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值