模拟登陆

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




}
模拟登陆
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值