package action;
import com.opensymphony.xwork2.interceptor.ExceptionHolder;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
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.util.EntityUtils;
import org.apache.log4j.Logger;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class UserAction {
private Logger logger = Logger.getLogger(this.getClass());
private String signin_email;
private String signin_password;
public String getSignin_email() {
return signin_email;
}
public void setSignin_email(String signin_email) {
this.signin_email = signin_email;
}
public String getSignin_password() {
return signin_password;
}
public void setSignin_password(String signin_password) {
this.signin_password = signin_password;
}
//用户登录
public String signin() {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://meiwei.me/signin");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
EntityUtils.consume(entity);
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
HttpPost httppost = new HttpPost("http://meiwei.me/signin"); //用户登录
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("signin_email", getSignin_email()));
nvps.add(new BasicNameValuePair("signin_password", getSignin_password()));
httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
response = httpclient.execute(httppost);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
EntityUtils.consume(entity);
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
/*String sessionID = response.getFirstHeader("Set-Cookie").getValue();
httppost.addHeader("Cookie", sessionID);
new DefaultHttpClient().execute(httppost);*/ //绑定session 其实ClientCookie可以重新获得cookie属性
String url = ""; //登录后页面跳转
Header locationHeader = response.getFirstHeader("Location");
System.out.println(locationHeader.getValue());
if (locationHeader != null) {
url = locationHeader.getValue(); // 得到跳转href
HttpGet httpget1 = new HttpGet("http://meiwei.me" + url);
response = httpclient.execute(httpget1);
// 登陆成功
}
entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// 显示结果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
HttpGet httpget1 = new HttpGet("http://meiwei.me/setting"); //设置个人信息页面
response = httpclient.execute(httpget1);
entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// 显示结果
reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
return "tologinsuccess";
} catch (RuntimeException e) {
ExceptionHolder exceptionHolder = new ExceptionHolder(e);
logger.error(exceptionHolder.getExceptionStack());
} catch (Exception e) {
ExceptionHolder exceptionHolder = new ExceptionHolder(e);
logger.error(exceptionHolder.getExceptionStack());
}
return "toerror";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java 作为客户端发送http post请求登录网站首页。
共69个文件
jar:33个
xml:19个
jsp:6个
5星 · 超过95%的资源 需积分: 41 120 下载量 161 浏览量
2012-08-27
13:00:19
上传
评论 1
收藏 11.63MB RAR 举报
温馨提示
尝试的一个网站,发现它的登录页面是会自动跳转的,再请求的话则会退回登录页面很是苦恼。提示: HTTP/1.1 302 Found
资源推荐
资源详情
资源评论
收起资源包目录
clientlogin.rar (69个子文件)
clientlogin
clientlogin.iml 1KB
src
action
UserAction.java 5KB
Test1.java 4KB
log4j.properties 253B
struts.xml 1KB
.idea
uiDesigner.xml 9KB
misc.xml 3KB
vcs.xml 283B
.name 11B
copyright
profiles_settings.xml 115B
modules.xml 273B
encodings.xml 171B
compiler.xml 669B
workspace.xml 39KB
libraries
hibernate.xml 1KB
struts.xml 1024B
jsp.xml 316B
common.xml 2KB
artifacts
clientlogin_war_exploded.xml 848B
inspectionProfiles
ant.xml 170B
out
production
clientlogin
action
UserAction.class 5KB
Test1.class 5KB
log4j.properties 253B
struts.xml 1KB
test
clientlogin
artifacts
clientlogin_war_exploded
WEB-INF
web.xml 869B
classes
action
UserAction.class 5KB
Test1.class 5KB
log4j.properties 253B
struts.xml 1KB
lib
commons-collections-3.1.jar 546KB
proxool-0.9.1.jar 196KB
dom4j-1.6.1.jar 307KB
javassist-3.12.0.GA.jar 618KB
proxool-cglib.jar 327KB
asm-tree-3.1.jar 21KB
log4j-1.2.16.jar 470KB
ognl-3.0.1.jar 224KB
commons-beanutils-1.8.3.jar 227KB
quartz-all-1.8.0.jar 449KB
mail.jar 451KB
httpclient-4.2.1.jar 417KB
commons-io-2.0.1.jar 156KB
slf4j-nop-1.6.1.jar 5KB
slf4j-api-1.6.1.jar 25KB
mysql-connector-java-5.1.16-bin.jar 768KB
commons-logging-1.1.1.jar 59KB
httpcore-4.2.1.jar 218KB
javax.servlet.jar 68KB
freemarker-2.3.16.jar 860KB
javax.servlet.jsp.jar 77KB
commons-codec-1.6.jar 227KB
commons-fileupload-1.2.2.jar 58KB
hibernate-jpa-2.0-api-1.0.1.Final.jar 100KB
xwork-core-2.2.3.jar 602KB
commons-lang-2.5.jar 273KB
jta-1.1.jar 11KB
asm-commons-3.1.jar 32KB
commons-email-1.2.jar 32KB
antlr-2.7.6.jar 433KB
gson-1.7.1.jar 170KB
struts2-core-2.2.3.jar 756KB
hibernate3.jar 3.95MB
error1
error.jsp 151B
user
login.jsp 147B
file
index.jsp 394B
web
WEB-INF
web.xml 869B
error1
error.jsp 151B
user
login.jsp 147B
index.jsp 394B
共 69 条
- 1
lele892207980
- 粉丝: 6
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页