爲了使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;
-
-
-
-
- public MyHttpClient(String host) {
- client = new HttpClient();
- client.getHostConfiguration().setHost(host, 80, "http");
- }
-
-
-
-
- public MyHttpClient() {
- client = new HttpClient();
- client.getHostConfiguration().setHost("", 80, "http");
- }
-
-
-
-
-
-
-
-
-
- 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);
- }
-
-
- 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);
- }
-
-
- 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;
- }
-
-
-
-
- public InputStream getContentAsStream(String url) throws Exception {
- GetMethod get = new GetMethod(url);
- client.executeMethod(get);
- return get.getResponseBodyAsStream();
- }
-
-
-
-
- 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 {
-
-
-
- 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);
- }
-
- }
- }
這樣就行了,非常方便,其他功能也可以試一下,大家多多交流!