模擬登錄 302 重定向

利用httpclient進行模擬登錄; java

發現過程當中存在302重定向的狀況,解決方法,得到地址,再次發送請求。 cookie

httpclient是同個,二次發送請求,會保存session id ,便可進行訪問。 session

public class Main {

	static String charset = "utf-8";
	static String result = "";
	private static CloseableHttpClient httpClient = HttpClients.createDefault();
	private static HttpClientContext context = new HttpClientContext();

	public static void main(String[] args) {

		String url = "http://hdu.sunnysport.org.cn/login";
		String param = "username=14031601&password=14031601";
		// System.out.println(sendURLPost(url, param));
		CloseableHttpResponse response = null;
		String content = null;
		BasicCookieStore cookieStore = new BasicCookieStore();
		/**
		 * 使用httpclient
		 */
		List<NameValuePair> nvps = new ArrayList<>();
		nvps.add(new BasicNameValuePair("username", "14031601"));
		nvps.add(new BasicNameValuePair("password", "14031601"));

		try {
			// HttpClient中的post請求包裝類
			HttpPost post = new HttpPost(url);
			// nvps是包裝請求參數的list
			if (nvps != null) {
				post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			}
			// 執行請求用execute方法,content用來幫咱們附帶上額外信息
			response = httpClient.execute(post, context);
			// 看是否重定向,並測試
			System.out.println(response.getStatusLine().getStatusCode());
			Header firstHeader = response.getFirstHeader("Location");
			System.out.println("name:" + firstHeader.getName());
			System.out.println("value:" + firstHeader.getValue());
			if (firstHeader.getValue() != null) {

				System.out.println("開始重定向=======");
				HttpPost post2 = new HttpPost(firstHeader.getValue());
				// nvps是包裝請求參數的list
				if (nvps != null) {
					post2.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
				}
				// 執行請求用execute方法,content用來幫咱們附帶上額外信息
				response = httpClient.execute(post2, context);
				
				System.out.println(response.getStatusLine().getStatusCode());
				Header firstHeader2 = response.getFirstHeader("Location");
			
			}
			// 獲得相應實體、包括響應頭以及相應內容
			HttpEntity entity = response.getEntity();

			content = EntityUtils.toString(entity);
			// 關閉輸入流
			EntityUtils.consume(entity);
			/**
			 * 重定向並不須要使用cookies,此處多餘
			 */
			List<Cookie> cookies = cookieStore.getCookies();
			if (cookies.isEmpty()) {
				System.out.println("None");
			} else {
				for (int i = 0; i < cookies.size(); i++) {
					System.out.println("- " + cookies.get(i).toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				System.out.println(content);
			}
		}
	}

}



而且貼一下過程當中使用別人的httpclient的封裝:
/**
 * Http工具類
 * 
 * @author Zhu
 * 
 */
public class HttpUtils {

	private static CloseableHttpClient httpClient = HttpClients.createDefault();
	private static HttpClientContext context = new HttpClientContext();

	private HttpUtils() {

	}

	public static String sendGet(String url) {
		CloseableHttpResponse response = null;
		String content = null;
		try {
			HttpGet get = new HttpGet(url);
			response = httpClient.execute(get, context);
			HttpEntity entity = response.getEntity();
			content = EntityUtils.toString(entity);
			EntityUtils.consume(entity);
			return content;
		} catch (Exception e) {
			e.printStackTrace();
			if (response != null) {
				try {
					response.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return content;
	}

	public static String sendPost(String url, List<NameValuePair> nvps) {
		CloseableHttpResponse response = null;
		String content = null;
		try {
			// HttpClient中的post請求包裝類
			HttpPost post = new HttpPost(url);
			// nvps是包裝請求參數的list
			if (nvps != null) {
				post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			}
			// 執行請求用execute方法,content用來幫咱們附帶上額外信息
			response = httpClient.execute(post, context);
			// 獲得相應實體、包括響應頭以及相應內容
			HttpEntity entity = response.getEntity();
			// 獲得response的內容
			content = EntityUtils.toString(entity);
			// 關閉輸入流
			EntityUtils.consume(entity);
			return content;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return content;
	}
}
相關文章
相關標籤/搜索