前提:
需要用到的java包
commons-httpclient-3.1.jar
commons-logging.jar
log4j-1.2.15.jar
commons-codec.jar

登陆淘宝的例子:

package com.spider.cron;import java.io.IOException;import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;public class TaoBaoLogin {private static final String LOGON_SITE = "http://www.taobao.com";private static final int LOGON_PORT = 80;private static final String TAOBAO_BASE_LOGIN_BEFORE = "http://member1.taobao.com/member/login.jhtml?f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F";private static final String TAOBAO_BASE_LOGIN = "http://login.taobao.com/member/login.jhtml";public static void main(String args[]) throws HttpException, IOException {String taobaoUser="woshigoojje@163.com";String taobaoPwd="3DES_2_000000000000000000000000000000_61F0B8BE021BBBDD020919017B6816F5";String taobaoTid="XOR_1_000000000000000000000000000000_63584054400B0F717B750370";HttpClient client = new HttpClient();client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);String _tb_token_Value="";Cookie[] cookies = client.getState().getCookies();        String responseString = processGet(client,null,TAOBAO_BASE_LOGIN_BEFORE,cookies,true,true);   responseString=responseString.substring(responseString.indexOf("_tb_token_")+"_tb_token_".length());responseString=responseString.substring(responseString.indexOf("_tb_token_")+"_tb_token_".length());_tb_token_Value=responseString.substring(responseString.indexOf("value=")+"value='".length(),responseString.indexOf(">")-1);PostMethod post = new PostMethod(TAOBAO_BASE_LOGIN);NameValuePair[] params= new NameValuePair[] {             new NameValuePair("_oooo_", ""),new NameValuePair("_tb_token_", _tb_token_Value),new NameValuePair("abtest", ""),new NameValuePair("action", "Authenticator"),new NameValuePair("actionForStable", "enable_post_user_action"),new NameValuePair("CtrlVersion", "1,0,0,7"),new NameValuePair("done", ""),new NameValuePair("event_submit_do_login", "anything"),new NameValuePair("from", ""),new NameValuePair("loginType", "4"),new NameValuePair("mcheck", ""),new NameValuePair("mi_uid", ""),new NameValuePair("pstrong", ""),new NameValuePair("support", "000001"),new NameValuePair("tid", taobaoTid),new NameValuePair("TPL_password", taobaoPwd),new NameValuePair("TPL_redirect_url", ""),new NameValuePair("TPL_redirect_url", ""),new NameValuePair("TPL_redirect_url", ""),new NameValuePair("TPL_redirect_url", ""),new NameValuePair("TPL_redirect_url", ""),new NameValuePair("TPL_redirect_url", ""),new NameValuePair("TPL_username", taobaoUser),new NameValuePair("yparam", "")};processPost(client, post, TAOBAO_BASE_LOGIN, params, cookies, true, false);Header header=post.getResponseHeader("Location");String redirectUrl=header.getValue();responseString = processGet(client, null, redirectUrl, cookies, true, true);responseString=responseString.substring(0,responseString.indexOf("我的彩票"));System.out.println("main(String[]) - " + responseString); String caiPiaoUrl=responseString.substring(responseString.lastIndexOf("<a")+"<a href='".length(),responseString.lastIndexOf(">")-1);processGet(client, null, caiPiaoUrl, cookies, true, false);}public static String processGet(HttpClient client,GetMethod get,String url,Cookie[] cookies,boolean needAppendCookies,boolean needResponse) throws IOException{if(client==null || url==null || url=="") return "";if(get==null)get=new GetMethod();get = new  GetMethod(url);  if(cookies!=null)get.setRequestHeader("Cookie" , cookies.toString());client.executeMethod(get);if(needAppendCookies){cookies = client.getState().getCookies();   client.getState().addCookies(cookies);   }if(needResponse)return get.getResponseBodyAsString();   get.releaseConnection();return "";}public static String processPost(HttpClient client,PostMethod post,String url,NameValuePair[] params,Cookie[] cookies,boolean needAppendCookies,boolean needResponse) throws IOException{if(client==null || url==null || url=="") return "";if(post==null)post = new PostMethod(url);if(params!=null && params.length>0)post.setRequestBody(params);if(cookies!=null)post.setRequestHeader("Cookie" , cookies.toString());client.executeMethod(post);if(needAppendCookies){cookies = client.getState().getCookies();   client.getState().addCookies(cookies);   }if(needResponse)return post.getResponseBodyAsString();post.releaseConnection();return "";}public static String processDetail(String tempStr,String startFlag,String endFlag,int starts,int ends){if(tempStr==null || "".equals(tempStr)) return "";int start=tempStr.indexOf(startFlag);int end=tempStr.indexOf(endFlag);if(start==-1 || end==-1 || (end-ends)<(start+starts)) return "";try{tempStr=tempStr.substring(start+starts,end-ends);}catch(Exception e){System.out.println("processDetail(String, String, String, int, int) " + e.toString()); return "";}return tempStr;}}

登陆开心网的例子:

package com.spider.cron;import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;public class LoginKaixin {private static final String LOGON_SITE = "http://www.kaixin001.com";private static final int LOGON_PORT = 80;public static void main(String[] args) throws Exception {HttpClient client = new HttpClient();client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);// 登录页面PostMethod post = new PostMethod("http://www.kaixin001.com/login/login.php");NameValuePair ie = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");NameValuePair url = new NameValuePair("url", "/home/");NameValuePair username = new NameValuePair("email", "xxx@163.com");NameValuePair password = new NameValuePair("password", "xxxxxx");post.setRequestBody(new NameValuePair[] { ie, url, username, password });client.executeMethod(post);System.out.println("******************************登录******************************");Cookie[] cookies = client.getState().getCookies();client.getState().addCookies(cookies);post.releaseConnection();System.out.println("******************************页面转向******************************");String newUrl = "http://www.kaixin001.com/home/";System.out.println("==========Cookies============");int i = 0;for (Cookie c : cookies) {System.out.println(++i + ":   " + c);}client.getState().addCookies(cookies);post.releaseConnection();GetMethod get = new GetMethod(newUrl);get.setRequestHeader("Cookie", cookies.toString());client.executeMethod(get);String responseString = get.getResponseBodyAsString();// 登录后首页的内容System.out.println(responseString);get.releaseConnection();System.out.println("******************************组件功能******************************");// "http://www.kaixin001.com/!slave/index.php", "朋友买卖"// "http://www.kaixin001.com/!parking/index.php", "争车位"// "http://www.kaixin001.com/!house/index.php?_lgmode=pri", "买房子"// http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49// "http://www.kaixin001.com/!house/garden/index.php","花园"// (1)进入朋友买卖****************System.out.println("******************************(1)进入朋友买卖******************************");String slave = "http://www.kaixin001.com/!slave/index.php";get = new GetMethod(slave);get.setRequestHeader("Cookie", cookies.toString());client.executeMethod(get);responseString = get.getResponseBodyAsString();System.out.println(responseString);get.releaseConnection();// (2)进入争车位****************System.out.println("******************************(2)进入争车位******************************");String parking = "http://www.kaixin001.com/!parking/index.php";get = new GetMethod(parking);get.setRequestHeader("Cookie", cookies.toString());client.executeMethod(get);responseString = get.getResponseBodyAsString();System.out.println(responseString);get.releaseConnection();// (3)进入买房子****************System.out.println("******************************(3)进入买房子*******************************");String house = "http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49";get = new GetMethod(house);get.setRequestHeader("Cookie", cookies.toString());client.executeMethod(get);responseString = get.getResponseBodyAsString();System.out.println(responseString);get.releaseConnection();// (4)进入花园****************System.out.println("******************************(4)进入花园*******************************");String garden = "http://www.kaixin001.com/!house/garden/index.php";get = new GetMethod(garden);get.setRequestHeader("Cookie", cookies.toString());client.executeMethod(get);responseString = get.getResponseBodyAsString();System.out.println(responseString);get.releaseConnection();}}

httpclient3 自动登陆淘宝, 开心网相关推荐

  1. Python自动登陆淘宝并爬取商品数据

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. 基本开发环境 Python 3.6 Pycharm import time from sel ...

  2. yjh的实习生涯之自动化测试(2)---新建一个自动登录淘宝例子以及如何将java代码运行到手机上

    上一篇(http://blog.csdn.net/qq_33223761/article/details/53846914)我们了解了uiautomator的一些基本用法和方法,现在我们再来看看怎么创 ...

  3. python 登陆淘宝_如何用 Python 自动登录淘宝并保存登录信息?

    原标题:如何用 Python 自动登录淘宝并保存登录信息? 作者 | 猪哥 责编 | 伍杏玲 前段时间时间为大家讲解了如何使用requests库模拟登录淘宝,而今天我们将对该功能进行丰富.所以我们把之 ...

  4. Python《使用Selenium 和pyautogui 实现自动登录淘宝》

    有了上一博文学习即基础,这一节想来学习下自动登录淘宝. 直接整上测试代码: from selenium import webdriver # import logging import time fr ...

  5. Python3+Selenium 实现自动登录淘宝+清空购物车

    此博客的目的为分享自己用Python3和Selenium实现的自动登录淘宝和清空购物车的程序逻辑.经测试,此程序有时可以"秒杀"一些供给相对充足的限量商品,但无法秒杀疫情期间的任何 ...

  6. 利用Python模拟登陆淘宝,实现购物秒杀!

    模拟登录 学爬虫,总能听到模拟登录这四个字,究竟什么是模拟登录?通俗一点讲,模拟登录就是程序用账号和密码自动登录一个网站.然后,拿到只有登录后,才能下载的网站数据. 很多人学习python,不知道从何 ...

  7. 使用WebBrowser自动登录淘宝

    看到有人需要自动登录淘宝,觉得很好玩,仔细研究了一下. 1.淘宝登录密码分两种输入方式,一种是安全登录,是默认登陆方式,其密码输入框是ACTIVEX控件,另一种是非安全登录,即普通方式. 2.普通方式 ...

  8. uniapp、uniCloud实现微信公众号自动查询淘宝京东优惠券制作过程

    uniapp.uniCloud实现微信公众号自动查询淘宝京东优惠券制作过程 微信公众号自动查询淘宝京东优惠券机器人制作教程.服务器通过uniapp提供的uniCloud云服务搭建,建议使用阿里云,不要 ...

  9. 使用selenium模块自动打开淘宝并进行搜索

    Selenium是一个自动化测试工具,可以驱动浏览器器执行特定的动作,如点击,下拉等.可以用来进行模拟人工操作浏览器进行测试,爬虫等. 如果没有安装环境可以先pip install Selenium, ...

  10. 自动获取淘宝API数据访问的SessionKey

    原文地址为: 自动获取淘宝API数据访问的SessionKey 最近在忙与淘宝做对接的工作,总体感觉淘宝的api文档做的还不错,不仅有沙箱测试环境,而且对于每一个api都可以通过api测试工具生成想要 ...

最新文章

  1. [Google Guava] 6-字符串处理:分割,连接,填充
  2. Linux mount挂载umount卸载
  3. [网络安全自学篇] 十一.虚拟机VMware+Kali安装入门及Sqlmap普及
  4. 解决多进程模式下引起的“惊群”效应
  5. mysql alert table 日志_MySQL日志
  6. c语言 子进程,子Shell和子进程
  7. F. 张胖胖玩多米诺骨牌 (南阳理工oj—21新生第二场招新赛)
  8. redis的持久化相关操纵
  9. ch10_ex32nbsp;荷兰国旗问题
  10. 【MC-CNN论文翻译】Computing the Stereo Matching Cost with a Convolutional Neural Network
  11. python average函数怎么用,Python Numpy MaskedArray.average()用法及代码示例
  12. OpenFOAM 粘弹性流体求解器rheoTool
  13. 计算机怎样同时运行多个程序的?进程相关概念介绍
  14. java实现发送手机验证码功能
  15. 差分隐私若干基本知识点介绍(一)
  16. errorCode 1045,state 28000: Access denied for user 'mysql'@'localhost' (using password: YES)
  17. 威海北洋电气java面试题_面试题_76_to_81_Java 最佳实践的面试问题
  18. Unity3D-设置地形
  19. Caché 函数大全
  20. 一文看懂川土微电子隔离器核心技术

热门文章

  1. 阿里云OSS服务开通STS安全令牌
  2. 批量生成奖状的简单程序
  3. FISCO BCOS区块链搭建说明(第一篇)
  4. html文字溢出用省列号,关于文字内容溢出用点点点(...)省略号表示
  5. MiCT: Mixed 3D/2D Convolutional Tube for Human Action Recognition论文笔记
  6. stm32 IAP 程序编写心得
  7. PS抠图后有毛边怎么处理?
  8. 03系统服务器下安装WMP10实际经验分析
  9. java 首次适应算法_首次适应算法java代码
  10. java报错symbol_java 报错cannot resolve symbol问题