现在的位置: 首页 > 综合 > 正文

httpclient 登录 post

2013年10月04日 ⁄ 综合 ⁄ 共 4025字 ⁄ 字号 评论关闭
为了使apache的HttpClient更加方便易用,我写了一个继承类
总共有三个功能
1.登录网站
2.获取网站上的内容
3.post数据
本代码在
运行平台:eclipse3.2
运行环境:jre1.5
其他:
commons-codec-1.3
commons-httpclient-3.1
commons-logging-1.1.1
这可以在本空间下载
或者可以到apache上下载
虽然功能很少,功能却非常强大,基本上,可以应付网页上很多操作
代码如下 (并提供了一个例子)
  1. package pack;
  2. import org.apache.commons.httpclient.*;
  3. import org.apache.commons.httpclient.cookie.*;
  4. import org.apache.commons.httpclient.methods.*;
  5. import java.io.*;
  6. public class MyHttpClient {
  7.     private HttpClient client;
  8.     
  9.     //
  10.     // if you kown the host 
  11.     // ok
  12.     public MyHttpClient(String host)  {
  13.         client = new HttpClient();
  14.         client.getHostConfiguration().setHost(host, 80"http");
  15.     }
  16.     
  17.     //
  18.     // or you don't kown 
  19.     //
  20.     public MyHttpClient()  {
  21.         client = new HttpClient();
  22.         client.getHostConfiguration().setHost(""80"http");
  23.     }
  24.     
  25.     
  26.     //   login    user->username password->userPassword  url
  27.     //   Usually   a   successful   form-based   login   results   in   a   redicrect   to     
  28.     //   another   url   
  29.     
  30.     //usually user and password 
  31.     //sometimes id  and password
  32.     //so we can use in this mode
  33.     public String login(String user, String userName, String password,
  34.             String userPassword, String url) throws Exception
  35.     {
  36.         client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
  37.         
  38.         PostMethod post = new PostMethod(url);
  39.         NameValuePair  nuser = new NameValuePair(user, userName);
  40.         NameValuePair  npass = new NameValuePair(password, userPassword);
  41.         
  42.         return postToServer(new NameValuePair[]{nuser, npass}, post);
  43.     }
  44.     
  45.     //default email and password
  46.     public String login(String email, String password, String url)
  47.         throws Exception
  48.     {
  49.         
  50.         client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
  51.         
  52.         PostMethod post = new PostMethod(url);
  53.         NameValuePair  nuser = new NameValuePair("email", email);
  54.         NameValuePair  npass = new NameValuePair("password", password);
  55.         return postToServer(new NameValuePair[] {nuser, npass}, post);
  56.     }
  57.     
  58.     //post login msg to server
  59.     public String postToServer(NameValuePair[] nameValuePairs, PostMethod post)
  60.         throws Exception
  61.     {
  62.         String redicretURL = null;
  63.         post.setRequestBody(nameValuePairs);
  64.         
  65.         client.executeMethod(post);
  66.         post.releaseConnection();
  67.         int statusCode = post.getStatusCode();
  68.         if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
  69.                 || (statusCode == HttpStatus.SC_MOVED_PERMANENTLY)
  70.                 || (statusCode == HttpStatus.SC_SEE_OTHER)
  71.                 || (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
  72.             Header header = post.getResponseHeader("location");
  73.             if (header != null) {
  74.                 redicretURL = new String(header.getValue());
  75.             }
  76.         }
  77.         return redicretURL;
  78.     }
  79.     
  80.     //
  81.     //get html stream
  82.     //
  83.     public InputStream getContentAsStream(String url) throws Exception {
  84.         GetMethod get = new GetMethod(url);
  85.         client.executeMethod(get);
  86.         return get.getResponseBodyAsStream();
  87.     }
  88.     
  89.     //
  90.     //get html string
  91.     //
  92.     public String getContentAsString(String url) throws Exception {
  93.         GetMethod get = new GetMethod(url);
  94.         client.executeMethod(get);
  95.         return get.getResponseBodyAsString();
  96.     }
  97.     
  98. }

下面给出一个例子:

比如说登录一个网站:
  1. package pack;
  2. public class Login {
  3.     public static void main(String[] args) throws Exception {
  4.         // TODO Auto-generated method stub
  5.         //now, i try to sign in xiaonei
  6.         
  7.         MyHttpClient httpClient = new MyHttpClient();
  8.         String redicretURL = new String();
  9.         StringBuffer index = new StringBuffer();
  10.         
  11.         
  12.         redicretURL = httpClient.login("email""password""site");
  13.         if (redicretURL != null) {
  14.              index.append(httpClient.getContentAsString(redicretURL));
  15.              System.out.println(index);
  16.         } else {
  17.             System.out.println(redicretURL);
  18.         }
  19.         
  20.     }
  21. }

这样就行了,非常方便,其他功能也可以试一下,大家多多交流!

抱歉!评论已关闭.