使用httpclient完成简单web登陆demo
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
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.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class DemoLogin2 {
private static String userName = "xxx";
private static String password = "xxx";
private static String LoginURL = "http://xxx.xxx.xxx/loginAjax.do";
private HttpResponse response;
private DefaultHttpClient httpclient = new DefaultHttpClient();
private String redirectLocation ;
private void login() {
HttpPost httpost = new HttpPost(LoginURL);
// All the parameters post to the web site
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("passport", userName));
nvps.add(new BasicNameValuePair("password", password));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps));
response = httpclient.execute(httpost);
} catch (Exception e) {
e.printStackTrace();
} finally {
httpost.abort();
}
}
private String getRedirectLocation() {
Header locationHeader = response.getFirstHeader("Location");
if (locationHeader == null) {
return null;
}
System.out.println(locationHeader.getValue().toString());
return locationHeader.getValue();
}
private String getText(String redirectLocation) {
HttpGet httpget = new HttpGet(redirectLocation);
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = "";
try {
responseBody = httpclient.execute(httpget, responseHandler);
} catch (Exception e) {
e.printStackTrace();
responseBody = null;
} finally {
httpget.abort();
httpclient.getConnectionManager().shutdown();
}
return responseBody;
}
public static void main(String[] args) {
DemoLogin2 demo = new DemoLogin2();
demo.login();
demo.redirectLocation = demo.getRedirectLocation();
System.out.println(demo.getText(demo.redirectLocation));
}
}
分享到:
相关推荐
httpclient Demo 案例 含jar 解决Post传参数字符原样的问题:如:1.0;解决被处理后为‘1’的问题
httpclient4.3的demo工程,包含jar包,可直接运行
httpclient Demo 可以看看
HttpClient_Demo HttpClient_Demo
这是一个android的HttpClient简单demo,实现了post与get两种方式,包括完整的客户端和服务器端程序,数据库采用的是MySql数据库,数据库部分请用户自己完成,都是最简单的,我已经测试通过,这是我一天的成果,参考...
利用 android httpclient 编写http协议的例子代码,可以参考添加post,get ,put delete 等功能。工程师文件可以直接编译运行。
网上关于HttpClient资料很多,但很多都是有代码无jar,也是找了一下午,特供资料,与君互勉。
NULL 博文链接:https://antlove.iteye.com/blog/1815470
本demo有两个例子test是作为服务端开启的,httpclient是客户端调用的,模拟服务端的test在启动后是没有界面的,要加上id才能看到json数据,httpclient的HttpClient33.java是测试类,最后的结果是接受到一个对象。...
基于springboot+httpClient无缝整合在一起的demo例子(资源分享)!
msdn上的demo,httpclient找了半天才找到的,看看吧对你有帮助的。。
jersery RestFull web service工程 应用了httpClient demo以及日志操作源码和java 动态反射机制 整个工程用来对外提供一个简化的ceph云存储服务器的restful api 同时工程作为 学习jersery 以及简单的统一日志管理 ...
HttpClient抓取网页Demo,HttpClient 的入门示例,配合blog文章的附件
最新版httpclient自动登录人人代码,内含有代码,httpclient4.3包,只需要修改账户和密码,就可以登陆
httpclient4之百度模拟登陆,回复与58同城自动登陆
压缩包内包括 httpclient 4.3的jar包和完整的demo程序。想学httpclient 4的赶紧下啊。
httpclient模拟登陆,请大家快过来赶紧下载哦。
httpclient模拟登陆代码以及相关的jar包
Java,通过 httpclient 获取 cookie 模拟登录 ,登录后携带cookie发起下一个请求
Jsoup+httpclient模拟登陆和抓取页面.pdf