httpclient 登录 post
来源:互联网 发布:幸运星 山本宽 知乎 编辑:程序博客网 时间:2024/06/02 14:35
为了使apache的HttpClient更加方便易用,我写了一个继承类
下面给出一个例子:这样就行了,非常方便,其他功能也可以试一下,大家多多交流!
总共有三个功能
1.登录网站
2.获取网站上的内容
3.post数据
本代码在
运行平台:eclipse3.2
运行环境:jre1.5
其他:
commons-codec-1.3
commons-httpclient-3.1
commons-logging-1.1.1
这可以在本空间下载
或者可以到apache上下载
虽然功能很少,功能却非常强大,基本上,可以应付网页上很多操作
代码如下 (并提供了一个例子)
- package pack;
- import org.apache.commons.httpclient.*;
- import org.apache.commons.httpclient.cookie.*;
- import org.apache.commons.httpclient.methods.*;
- import java.io.*;
- public class MyHttpClient {
- private HttpClient client;
- //
- // if you kown the host
- // ok
- public MyHttpClient(String host) {
- client = new HttpClient();
- client.getHostConfiguration().setHost(host, 80, "http");
- }
- //
- // or you don't kown
- //
- public MyHttpClient() {
- client = new HttpClient();
- client.getHostConfiguration().setHost("", 80, "http");
- }
- // login user->username password->userPassword url
- // Usually a successful form-based login results in a redicrect to
- // another url
- //usually user and password
- //sometimes id and password
- //so we can use in this mode
- public String login(String user, String userName, String password,
- String userPassword, String url) throws Exception
- {
- client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
- PostMethod post = new PostMethod(url);
- NameValuePair nuser = new NameValuePair(user, userName);
- NameValuePair npass = new NameValuePair(password, userPassword);
- return postToServer(new NameValuePair[]{nuser, npass}, post);
- }
- //default email and password
- public String login(String email, String password, String url)
- throws Exception
- {
- client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
- PostMethod post = new PostMethod(url);
- NameValuePair nuser = new NameValuePair("email", email);
- NameValuePair npass = new NameValuePair("password", password);
- return postToServer(new NameValuePair[] {nuser, npass}, post);
- }
- //post login msg to server
- public String postToServer(NameValuePair[] nameValuePairs, PostMethod post)
- throws Exception
- {
- String redicretURL = null;
- post.setRequestBody(nameValuePairs);
- client.executeMethod(post);
- post.releaseConnection();
- int statusCode = post.getStatusCode();
- if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
- || (statusCode == HttpStatus.SC_MOVED_PERMANENTLY)
- || (statusCode == HttpStatus.SC_SEE_OTHER)
- || (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
- Header header = post.getResponseHeader("location");
- if (header != null) {
- redicretURL = new String(header.getValue());
- }
- }
- return redicretURL;
- }
- //
- //get html stream
- //
- public InputStream getContentAsStream(String url) throws Exception {
- GetMethod get = new GetMethod(url);
- client.executeMethod(get);
- return get.getResponseBodyAsStream();
- }
- //
- //get html string
- //
- public String getContentAsString(String url) throws Exception {
- GetMethod get = new GetMethod(url);
- client.executeMethod(get);
- return get.getResponseBodyAsString();
- }
- }
比如说登录一个网站:
- package pack;
- public class Login {
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- //now, i try to sign in xiaonei
- MyHttpClient httpClient = new MyHttpClient();
- String redicretURL = new String();
- StringBuffer index = new StringBuffer();
- redicretURL = httpClient.login("email", "password", "site");
- if (redicretURL != null) {
- index.append(httpClient.getContentAsString(redicretURL));
- System.out.println(index);
- } else {
- System.out.println(redicretURL);
- }
- }
- }
- httpclient 登录 post
- 用Httpclient来Post表单实现登录
- httpclient post请求,需要登录的地址
- HttpClient模拟Post实现登录辽大教务处
- httpclient post
- httpclient post
- httpclient post
- HTTPCLIENT.POST
- HttpClient--HttpClient的Post请求
- get、post、httpclient-get、httpclient-post
- C# 中使用System.Net.Http.HttpClient 模拟登录博客园 (GET/POST)
- C# 中使用System.Net.Http.HttpClient 模拟登录博客园 (GET/POST)
- 【实战】(二)android模拟qq登录(apache的httpClient发送get和post请求)
- java HttpClient Post实例
- HttpClient中的POST方法
- Httpclient发送POST请求
- HttpClient接口Post方式
- httpClient发送post请求
- sizeof语法
- CodeIgniter 入门
- Windows DDK中经典的宏——CONTAINING_RECORD
- 编程大师论道:PHP的魅力和不足何
- 重装了JBUILDER,以前能运行的系统又不行了的问题及解决
- httpclient 登录 post
- 字符串的应用
- Workstation(lanmanworkstation)服务启动慢的解决方法
- 令世界目瞪口呆的"中国超级工程一览"
- gridview 排序时候显示箭头
- 红旗Linux+Oracle10g
- Solaris2.4 多线程编程指南1--线程基础
- 微软新数据库向Oracle宣战
- 输入框动态判断数据库中是否有重复(根据企业名称)