`
includemain
  • 浏览: 32261 次
  • 性别: Icon_minigender_1
  • 来自: 嘉兴
社区版块
存档分类
最新评论

初试HttpClient

    博客分类:
  • Java
阅读更多


package com.tan.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class TestHttpClient {
	private static final String LOGINURL = "http://localhost:8080/login.jsp";
	private static final String USERNAME = "username";
	private static final String PASSWORD = "password";
	private static final String LINE = System.getProperty("line.separator");
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		CookieStore store = getCookieStore();
		if (store != null) {
			List<Cookie> cookies = store.getCookies();
			System.out.println(cookies);
//			for (Cookie cookie: cookies) {
//				System.out.println(cookie);
//			}
		}
	}
	private static CookieStore getCookieStore() throws UnsupportedEncodingException,
			IOException, ClientProtocolException {
		CookieStore store = null;
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(LOGINURL);
		
		// 登录的参数 
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		
		// 登录的 Username
		nvps.add(new BasicNameValuePair("username", USERNAME));
		nvps.add(new BasicNameValuePair("password", PASSWORD));
		nvps.add(new BasicNameValuePair("URL", "/"));
		
		httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		
		// 执行登录
		HttpResponse response = httpClient.execute(httpPost);
		// 获取 Http响应头中的 Set-Cookie
		Header header = response.getFirstHeader("Set-Cookie");
		HeaderElement[] elements = header.getElements();
		if (elements != null) {
			for (HeaderElement element : elements) {
				System.out.println(
							"HeaderElement's name : " + element.getName() + LINE + 
							"HeaderElement's value : " + element.getValue() 
							);
				
				// 获取 Cookies 
				store = httpClient.getCookieStore();
				break;
			}
		}
		return store;
	}

}


<%@ page contentType="text/html;charset=gb18030" pageEncoding="gb18030"%>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> Login Form </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <SCRIPT LANGUAGE="JavaScript">
  <!--
	
  //-->
  </SCRIPT>
 </HEAD>

 <BODY>
  <%!
	private static boolean isEmpty(String v) {
		return v == null || v.trim().length() == 0;
	}
  %>


  <%
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	if (!isEmpty(username) && !isEmpty(password)) {
		out.println("Login successful");
	} else {
		out.println("Login failure");
	}
  %>
  <FORM METHOD="post" ACTION="login.jsp"  id="loginForm">
	<input type="text" name="username" />
	<input type="password" name="password"/>
	<input type="submit" value="submit" />
	<input type="reset" value="reset" />
  </FORM>
 </BODY>
</HTML>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics