httpClient 4.0 重定向

今天在弄豆瓣oauth 的時候發現須要httpClient重定向的操做,找到一篇存起來以備不時只需。。
http://blog.csdn.net/fx_sky/article/details/8678839?reload (原文連接)
package com.yulore.test;

import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.ProtocolException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.RedirectStrategy;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class HttpClientURLRedirectTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		redirect02();
	}

	/**
	 * Http URL重定向
	 */
	private static void redirect02() {
		DefaultHttpClient httpclient = null;
		String url = "http://hotels.ctrip.com/hotel/hong-kong58";
		try {
			httpclient = new DefaultHttpClient();
			httpclient.setRedirectStrategy(new RedirectStrategy() {	//設置重定向處理方式

				@Override
				public boolean isRedirected(HttpRequest arg0,
						HttpResponse arg1, HttpContext arg2)
						throws ProtocolException {

					return false;
				}

				@Override
				public HttpUriRequest getRedirect(HttpRequest arg0,
						HttpResponse arg1, HttpContext arg2)
						throws ProtocolException {

					return null;
				}
			});

			// 建立httpget.
			HttpGet httpget = new HttpGet(url);
			// 執行get請求.
			HttpResponse response = httpclient.execute(httpget);

			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				// 獲取響應實體
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					// 打印響應內容長度
					System.out.println("Response content length: "
							+ entity.getContentLength());
					// 打印響應內容
					System.out.println("Response content: "
							+ EntityUtils.toString(entity));
				}
			} else if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY
					|| statusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
				
				System.out.println("當前頁面發生重定向了---");
				
				Header[] headers = response.getHeaders("Location");
				if(headers!=null && headers.length>0){
					String redirectUrl = headers[0].getValue();
					System.out.println("重定向的URL:"+redirectUrl);
					
					redirectUrl = redirectUrl.replace(" ", "%20");
					get(redirectUrl);
				}
			}

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 關閉鏈接,釋放資源
			httpclient.getConnectionManager().shutdown();
		}
	}

	/**
	 * 發送 get請求
	 */
	private static void get(String url) {

		HttpClient httpclient = new DefaultHttpClient();

		try {
			// 建立httpget.
			HttpGet httpget = new HttpGet(url);
			System.out.println("executing request " + httpget.getURI());
			// 執行get請求.
			HttpResponse response = httpclient.execute(httpget);
			
			// 獲取響應狀態
			int statusCode = response.getStatusLine().getStatusCode();
			if(statusCode==HttpStatus.SC_OK){
				// 獲取響應實體
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					// 打印響應內容長度
					System.out.println("Response content length: "
							+ entity.getContentLength());
					// 打印響應內容
					System.out.println("Response content: "
							+ EntityUtils.toString(entity));
				}
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 關閉鏈接,釋放資源
			httpclient.getConnectionManager().shutdown();
		}
	}

}
相關文章
相關標籤/搜索