HttpClient,实现登录,请求等操作,session保持

来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-02 03:41 点击:
HttpClient,httpclient.jar包中的,我则喜欢折射成类似于dll以用的形式,知道在哪个namespace下
http://hc.apache.org/httpcomponents-client
可以从这里获取或得到些信息
实现的目标:
1.模拟登录
2.登录之后,模拟提交请求
 
关键点:
1.session状态保持
2.页面隐藏的属性获取
之前用HttpURLConnection,但未成功,瓶颈在于没有绕过session保持上,最终还是使用HttpClient,实现相对容易些
 
示例代码
(代码有删减,不见得能够跑通,需要修改)
 
/** *  */package httpclienttest;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Random;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.ResponseHandler;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.cookie.Cookie;import org.apache.http.impl.client.BasicResponseHandler;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HTTP;import com.alibaba.ccbu.parseelements.ParseTest;/** * @author forward *  */public class LoginFormDemo {     public static void DemoLoginTwo() throws Exception {        DefaultHttpClient httpclient = new DefaultHttpClient();        HttpGet httpget = new HttpGet(                http://xxxx.com/xx.htm);        HttpResponse response = httpclient.execute(httpget);        HttpEntity entity = response.getEntity();        System.out.println("Login form get: " + response.getStatusLine());        if (entity != null) {            entity.consumeContent();        }        System.out.println("Initial set of cookies:");        List<Cookie> cookies = httpclient.getCookieStore().getCookies();        if (cookies.isEmpty()) {            System.out.println("None");        } else {            for (int i = 0; i < cookies.size(); i++) {                System.out.println("- " + cookies.get(i).toString());            }        }        // action=xxx&Password=xxxx        HttpPost httpost = new HttpPost(                "http://localhost/sss");        List<NameValuePair> nvps = new ArrayList<NameValuePair>();        nvps.add(new BasicNameValuePair("LoginId", "xxxx"));        nvps.add(new BasicNameValuePair("Password", "xxxxx"));        nvps.add(new BasicNameValuePair("action", "xxxx"));        nvps.add(new BasicNameValuePair("eventSubmit_doPost", "xxxx"));        httpost.setEntity

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Android 完全退出程序
      • 原创:Android应用开发-Andorid歌词秀,含源码
      • android 屏幕保护
      • Android手机软件汉化教程---第四课 dex文件汉化
      • 众多Android 开源项目推荐,给力工作给力学习
      • Android Audio代码分析4
      • Android得到已安装的应用程序信息!
      • Android开发者指南(29) —— USB Host and Accessory
      • Android成长的幕后推手:工程师鲁宾
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1