首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

HttpClient 4.1版本,模拟登录,总算成功了

2012-07-01 
HttpClient 4.1版本,模拟登录,终于成功了HttpClient 4.1版本,模拟登录,终于成功了。有一个感慨和大家分享:1

HttpClient 4.1版本,模拟登录,终于成功了

HttpClient 4.1版本,模拟登录,终于成功了。

有一个感慨和大家分享:

1.为什么现在有企业要求多HTTP协议,TCP/IP协议熟悉

理由是与互联网上的资源交互的时候,需要用到这些东西,而且是必须要用到的。

?

?

感觉有点只可意会不可言传。举个例子吧,你要爬取人人网上的资源,那么你必须登录以后才能

爬取页面,而我们如果不是用账号登录,那么人人网的服务器会发送过来的是登录页面,

那么我们要爬取人人网登录以后的资源,就要模拟这个登录过程,然后在程序中拦截到登录以后

人人网发送过来的资源的。

就是这个意思了。

?

下面是杭州电子科技大学的校内论文,实名注册的。外校无法访问,无法注册。如果你有幸看到这个文章

并且你是杭电的,那么你可以测序下程序,就能进行模拟登录了。

?

?

package endual.jsoup.client3;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URI;import java.net.URISyntaxException;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.*;import org.apache.http.client.utils.URIUtils;import org.apache.http.client.utils.URLEncodedUtils;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import com.sun.jndi.toolkit.url.Uri;import endual.utils.HttpUtils; /**    Endual
   2012.3.28 */public class DictSpider {             public static void main(String[] args) throws Exception {            login();        //http://passport.redhome.cc/user                            }     //    static void get(String url) throws IOException {//    //            HttpGet get = new HttpGet(url);//  //            HttpResponse response = client.execute(get);//    //            System.out.println(response.getStatusLine());//    //            HttpEntity entity = response.getEntity();//  //            dump(entity);// //        }         /**     * 执行登录过程     * @param user     * @param pwd     * @param debug     * @throws IOException     * @throws Exception      */    static void login() throws IOException, Exception {        HttpClient client = new DefaultHttpClient();        HttpPost post = new HttpPost("http://passport.redhome.cc/user/login");        post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) " +"AppleWebKit/535.11 (KHTML, like Gecko) " +"Chrome/17.0.963.83 " +"Safari/535.11");        String login_type = "username" ;        String stu_no = "name" ; //登录名字        String password = "pawd" ; //登录密码        String cookietime = "0" ;        String btn_submit = "%E7%99%BB%E5%BD%95" ;        //登录表单的信息        List<NameValuePair> qparams = new ArrayList<NameValuePair>();        qparams.add(new BasicNameValuePair("login_type", login_type));        qparams.add(new BasicNameValuePair("stu_no", stu_no));        qparams.add(new BasicNameValuePair("password", password));        qparams.add(new BasicNameValuePair("cookietime", cookietime));        qparams.add(new BasicNameValuePair("btn_submit", btn_submit));                UrlEncodedFormEntity params = new UrlEncodedFormEntity(qparams, "UTF-8");        post.setEntity(params);        //相当于按了下确定登录的按钮,也就是浏览器调转了        HttpResponse response = client.execute(post);        /**        //这里最好验证下当前浏览器有没有调转,重定向等等,但是这边没有做        **/         HttpEntity entity = response.getEntity();        System.out.println(entity.getContentType().getElements()[0]) ;        String entityMsg = EntityUtils.toString(entity) ;         System.out.println(entityMsg);                        HttpGet get = new HttpGet("http://passport.redhome.cc/user");        HttpResponse responsex = client.execute(get);        System.out.println(responsex.getStatusLine());        HttpEntity entityx = responsex.getEntity();        String entityMsgx = EntityUtils.toString(entityx) ;         System.out.println(entityMsgx);    }}
?

?

?

热点排行