初试HttpClient

[list]
需要的Jar包下载地址: [b][url]http://hc.apache.org/downloads.cgi[/url][/b]
[/list]

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值