模拟登陆

原创 2015年07月08日 16:03:48
package test;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.http.Header;
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.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.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class HttpClientUtil {


	private String userName = "";

	private String password = "";

	private String redirectURL = "";
	String tmpcookies = "";

//	
	private static String loginURL = "http://192.168.145.240:9080/admin/logon.do?name=admin&password=solr987";

	private HttpResponse response;

	private DefaultHttpClient httpclient = null;

	private String redirectLocation = null;

	private String defaultContent = null;

	public HttpClientUtil(String userName, String password,String redirectURL) {
		this.userName = userName;
		this.password = password;
		this.redirectURL=redirectURL;
	}
	

	@SuppressWarnings("deprecation")
	public boolean login() {
		if (httpclient != null) {
			return true;
		}
		httpclient = null;
		httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost(loginURL);
		// All the parameters post to the web site
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("origURL", redirectURL));
		nvps.add(new BasicNameValuePair("autoLogin", "true"));
		nvps.add(new BasicNameValuePair("TPL_username", userName));
		nvps.add(new BasicNameValuePair("TPL_password", password));
		try {
			httpost.setEntity((HttpEntity) new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			response = httpclient.execute(httpost);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			httpost.abort();
		}
		Header[] hs = response.getAllHeaders();
		for (Header h : hs) {
		}
		redirectLocation = getRedirectLocation();
		redirectLocation = redirectURL;//"http://192.168.145.240:9080/admin/processparamslog.do?processUrl=http://192.168.202.101:9080/emall/search.do?keyword=%E5%8D%B7%E7%BA%B8...&flag=log";
		if (getToken() == null) {
			return false;
		}
		List<org.apache.http.cookie.Cookie> cookies = httpclient
				.getCookieStore().getCookies();// 取出登陆成功后,服务器返回的cookies信息,里面保存了服务器端给的“临时证”
		tmpcookies = "";
		for (int i = 0; i < cookies.size(); i++) {
			org.apache.http.cookie.Cookie c = cookies.get(i);
			tmpcookies = tmpcookies + c.getName() + "=" + c.getValue() + ";";
		}
		return true;
	}

	private String getRedirectLocation() {
		Header locationHeader = response.getFirstHeader("Location");
		if (locationHeader == null) {
			return null;
		}
		return locationHeader.getValue();
	}

	public String getToken() {
		String retu = getText(redirectLocation);
		String requesttoken = null;
		int start = retu.indexOf("查看余额");// 这里要改一下,看一下登录成功后页面有什么内容,以区分是否成功登录,以前淘宝登录后可以使用"查看余额"区分,貌似现在不行了
		if (start != -1) {
			requesttoken = "success";
			defaultContent = retu;
		}
		return requesttoken;
	}

	private String getText(String redirectLocation) {
		HttpGet httpget = new HttpGet(redirectLocation);
		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (Exception e) {
			e.printStackTrace();
			responseBody = null;
		} finally {
			httpget.abort();
		}
		return responseBody;
	}

	public String getDefaultContent() {
		return defaultContent;
	}

	@SuppressWarnings("deprecation")
	public boolean logout() {
		if (login()) {
			String logoutUrl = "退出登录的url";
			HttpPost httpost = new HttpPost(logoutUrl);
			List<NameValuePair> nvps2 = new ArrayList<NameValuePair>();
			try {
				httpost.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8));
				response = httpclient.execute(httpost);
				httpost.abort();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} finally {
				httpost.abort();
				httpclient.getConnectionManager().shutdown();
			}
		}
		return true;
	}

	public String getBoughtList() {
		HttpGet httpget = new HttpGet(redirectURL);
		httpget.setHeader("cookie", tmpcookies);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = null;
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpget.abort();
		}
		return responseBody;
	}
	public static String getFile(String path,String Id){

        File file = new File(path);
        try {
            for (File shopFile : file.listFiles()) {
                List<String> shopList =FileUtils.readLines(shopFile, "UTF-8");
                for (String o:shopList) {
                	System.out.println(o);
                	if(o.contains(Id)){
                		System.out.println(shopFile.getName());
                		return shopFile.getName();
                	}
                }
            }
        } catch (UnsupportedEncodingException e) {
        	System.out.println("UnsupportedEncodingException");
        } catch (FileNotFoundException e) {
        	System.out.println("FileNotFoundException");
        } catch (IOException e) {
        	System.out.println("IOException");
		}
       return "";
	
	}
	public String getUrlContent(String url) {
		HttpGet httpget = new HttpGet(
				url);
		httpget.setHeader("cookie", tmpcookies);
		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = null;
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpget.abort();
		}
		return responseBody;
	}

	 public static void main(String[] args) throws IOException {
		 BufferedReader asareader = null; 
			String tempString = ""; 
			asareader=new BufferedReader(new FileReader("f:/allas.txt"));
				while ((tempString = asareader.readLine()) != null){
					 String redirectURL = "http://192.168.145.240:9080/admin/processparamslog.do?processUrl=http://"+tempString+":9080/emall/search.do?keyword="+URLDecoder.decode("4g4g")+"&cityId=9173&flag=log";
					 HttpClientUtil taobao = new HttpClientUtil("admin", "solr987",redirectURL);
				 taobao.login();
				 String string=taobao.getBoughtList();
				 if(string.contains("q=4g AND 4g")){
					 String[] strings=string.split("\n");
					 for(String str:strings){
						 if(str.contains("<td width=\"95%\"style=\"text-align:left;\" class=\"tr_keyword\" title=\"sort")){
							 String temString=str.split(">")[1].replace(" </td", "");
						 }
					 }
				 }else{

					 String[] strings=string.split("\n");
					 
					 for(String str:strings){
						 if(str.contains("<td width=\"95%\"style=\"text-align:left;\" class=\"tr_keyword\" title=\"sort")){
							 String temString=str.split(">")[1].replace(" </td", "");
//							 System.out.println(temString);
						 }
					 }
				 
					 System.out.println(tempString);
				 }
				}
		 
	
	 }




}
模拟登陆

相关文章推荐

Linux 文件IO编程-模拟登陆系统

  • 2017年08月26日 14:48
  • 8KB
  • 下载

QQ注册登陆SWING界面模拟

  • 2016年03月24日 08:57
  • 13KB
  • 下载

使用Python的Requests包模拟登陆

前段时间喜欢用python去抓一些页面玩,但都基本上都是用get请求一些页面,再通过正则去过滤。 今天试了一下,模拟登陆个人网站。发现也比较简单。读懂本文需要对http协议和http会话有一定的理解...

jsoup step to step 模拟登陆

  • 2017年06月22日 10:15
  • 151KB
  • 下载

android mvp 模拟登陆

  • 2017年03月10日 10:43
  • 301KB
  • 下载

网络爬虫中的模拟登陆获取数据(实例教学)

目录模拟登陆的原因 如何模拟登陆 实战demo模拟登陆的原因很多网站,我们是无法直接获得服务器返回的数据,需要输入用户名及密码才能看到数据。如我们登陆人人网时,网站网址http://www.renre...

python模拟登陆新浪微博

  • 2015年09月25日 15:01
  • 5KB
  • 下载

模拟登陆CSDN——就是这么简单

工具介绍 本篇文章主要是讲解如何模拟登陆CSDN,使用的工具是HttpClient+Jsoup 其中HttpClient主要是负责发送请求,而Jsoup主要是解析HTML ...
  • njchenyi
  • njchenyi
  • 2014年08月24日 22:25
  • 30773

CMCT-PT 模拟登陆及请求调用

  • 2015年01月08日 09:10
  • 4KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:模拟登陆
举报原因:
原因补充:

(最多只允许输入30个字)