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);
}
}
}
}
模拟登陆
模拟登陆
最新推荐文章于 2021-02-16 04:31:13 发布