为了使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);
- }
- }
- }