模拟登陆

原创 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);
				 }
				}
		 
	
	 }




}
模拟登陆

使用httpclient模拟登录

import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; imp...
  • qq_36948778
  • qq_36948778
  • 2017年03月20日 08:50
  • 1623

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

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

Android(Java) 模拟登录知乎并抓取用户信息

前不久,看到一篇文章我用爬虫一天时间“偷了”知乎一百万用户,只为证明PHP是世界上最好的语言,该文章中使用的登录方式是直接复制cookie到代码中,这里呢,我不以爬信息为目的。只是简单的介绍使用jav...
  • sbsujjbcy
  • sbsujjbcy
  • 2015年08月10日 11:49
  • 8833

C# 模拟QQ登录器并获取登陆密码

  • 2015年01月17日 15:04
  • 1.95MB
  • 下载

最新java模拟登陆QQ空间【2017-12-22】有效

  • 2017年12月22日 15:41
  • 1.26MB
  • 下载

winform模拟登陆,带网站

  • 2015年09月18日 09:51
  • 154KB
  • 下载

C#模拟登陆代码片段

  • 2016年01月12日 14:32
  • 45KB
  • 下载

httpclient模拟登陆

  • 2012年07月13日 14:44
  • 85KB
  • 下载

Jmeter参数化实例-模拟用户登陆

  • 2016年05月20日 16:15
  • 1.19MB
  • 下载

HttpClient模拟登陆方正系统

  • 2015年07月06日 22:34
  • 616KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:模拟登陆
举报原因:
原因补充:

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