import
org
.
apache
.
commons
.
httpclient
.
Cookie
;
import
org
.
apache
.
commons
.
httpclient
.
HttpClient
;
import
org
.
apache
.
commons
.
httpclient
.
NameValuePair
;
import
org
.
apache
.
commons
.
httpclient
.
cookie
.
CookiePolicy
;
import
org
.
apache
.
commons
.
httpclient
.
methods
.
GetMethod
;
import
org
.
apache
.
commons
.
httpclient
.
methods
.
PostMethod
;
* @ClassName: HttpLogin
* @Description: java通过httpclient获取cookie模拟登录
public
class
HttpLogin
{
public
static
void
main
(
String
[
]
args
)
{
String loginUrl
=
"http://passport.mop.com/?targetUrl=http://hi.mop.com/?&g=1447141423230&loginCheck=UNLOGINED"
;
String dataUrl
=
"http://hi.mop.com/?"
;
HttpClient httpClient
=
new
HttpClient
(
)
;
PostMethod postMethod
=
new
PostMethod
(
loginUrl
)
;
NameValuePair
[
]
data
=
{
new
NameValuePair
(
"loginName"
,
"chzeze123"
)
,
new
NameValuePair
(
"loginPasswd"
,
"**"
)
}
;
postMethod
.
setRequestBody
(
data
)
;
try
{
httpClient
.
getParams
(
)
.
setCookiePolicy
(
CookiePolicy
.
BROWSER_COMPATIBILITY
)
;
int
statusCode
=
httpClient
.
executeMethod
(
postMethod
)
;
Cookie
[
]
cookies
=
httpClient
.
getState
(
)
.
getCookies
(
)
;
StringBuffer tmpcookies
=
new
StringBuffer
(
)
;
for
(
Cookie c
:
cookies
)
{
tmpcookies
.
append
(
c
.
toString
(
)
+
";"
)
;
System
.
out
.
println
(
"cookies = "
+
c
.
toString
(
)
)
;
if
(
statusCode
==
302
)
{
System
.
out
.
println
(
"模拟登录成功"
)
;
GetMethod getMethod
=
new
GetMethod
(
dataUrl
)
;
getMethod
.
setRequestHeader
(
"cookie"
,
tmpcookies
.
toString
(
)
)
;
postMethod
.
setRequestHeader
(
"Referer"
,
"http://passport.mop.com/"
)
;
postMethod
.
setRequestHeader
(
"User-Agent"
,
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
)
;
httpClient
.
executeMethod
(
getMethod
)
;
String text
=
getMethod
.
getResponseBodyAsString
(
)
;
System
.
out
.
println
(
text
)
;
else
{
System
.
out
.
println
(
"登录失败"
)
;
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
package Step1; import org.apache.commons.httpclient.Cookie;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.cookie.CookiePolicy;import org.apache.commons.httpclien
//
获取
request里面的
cookie
cookie
里面存值方式也是 键值对的方式
Cookie
[]
cookie
= request.get
Cookie
s();
for (int i = 0; i
Cookie
cook =
cookie
[i];
if(cook.getName().equalsIgnoreCase("eredg4.login.account")){ //
获取
键
因为代码与
Java
用apache的
HttpClient
发送Post请求大部份重复,所以就不贴整段代码了,只把不同的地方贴出来。
发送
Cookie
就必须先得到
Cookie
,所以至少发送两次请求,第一次用于得到
Cookie
,第二次在发送请求前加上
Cookie
在第一次发送Post请求前,先建立一个Default
HttpClient
对象的引用,在上文中没有建立引用,new了一个DefaultHttp
每个
Cookie
不能超过4kb
cookie
的创建:
protected void create
Cookie
(HttpServletResponse req,HttpServletResponse resp) throws IOException {
//1.创建
Cookie
对象
Cookie
cookie
= new
Cookie
("key1","value");
jsoup
Connection conn = Jsoup.connect("http://www.17sct.com/city.php?name=CHANGZHOU");
conn.method(Method.GET);
conn.followRedirects(false);
Response response = conn.execute();
1.
Cookie
概述
Cookie
是什么?
Cookie
是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递。
Cookie
包含每次用户访问站点时 Web 应用程序都可以读取的信息。
为什么需要
Cookie
? 因为HTTP协议是无状态的,对于一个浏览器发出的多次请求,WEB服务器无法区分 是不是来源于同一个浏览器。所以,需要额外的数据用于维护会话。
Cookie
正是...
1.代码是完整的导入到 myEclipse 中就可以使用
2.实现struts
cookie
记住两周 自动
登录
功能
3.访问地址:http://127.0.0.1:8080/
cookie
/index.jsp
4.系统默认有两个账户 在 UserDao 中可以查到
qq_36930839: