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>
分享到:
相关推荐
httpclient
httpclient-4.5所需jar包,里面包含httpclient-4.5.jar等等10个必须的开发包。 1.commons-codec-1.9.jar 2.commons-logging-1.2.jar 3.fluent-hc-4.5.jar 4.httpclient-4.5.jar 5.httpclient-cache-4.5.jar 6....
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
httpClient完整封装获取网页信息、数据的代码+httpclient.jar
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....
HttpClient httpClient = new HttpClient(); // 设置 Http 连接超时为5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); /* 2 生成 GetMethod 对象并设置参数 */ GetMethod ...
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...
httpclient.jar下载 包括code.jar包
C# HttpClientHelper(HttpClient工具类) 包含 同步/异步请求 返回 string/泛型类型/Xml 及一个单例模式 的 SingleHelper 适合做爬虫
HttpClient的一个封装, HttpClient的一个封装
帮助程序员快速从Apache的HttpClient 3.x升级到HttpClient 4.x
HttpClient 4.5.3 (GA) is a maintenance release that fixes a number of defects found since 4.5.2. Please note that as of 4.4 HttpClient requires Java 1.6 or newer. Changelog: ------------------- * ...
httpClient4.3.6 HttpClient使用详解 httpClient中文帮助文档
httpclient jar包 httpclient jar包 httpclient jar包
HttpClient jar包
commons-httpclient.zip
一个使用HttpClient访问WS 的例子,包含相关jar包