【教程】模拟登陆百度之Java代码版

时间:2022-09-21 15:51:58

【背景】

之前已经写了教程,分析模拟登陆百度的逻辑:

【教程】手把手教你如何利用工具(IE9的F12)去分析模拟登陆网站(百度首页)的内部逻辑过程

然后又去用不同的语言:

Python的:

【教程】模拟登陆网站 之 Python版(内含两种版本的完整的可运行的代码)

C#的:

【教程】模拟登陆网站 之 C#版(内含两种版本的完整的可运行的代码)

去实现对应逻辑。

此处,继续尝试,用Java代码,实现这套,模拟登陆百度,的逻辑。

【折腾过程】

1.之前已经整理了一些Java代码:

http://code.google.com/p/crifanlib/source/browse/trunk/java/crifanLib.java

现在是:

继续去丰富此套库。

然后把模拟登陆百度的逻辑模拟出来。

2.期间,想要实现Java的函数的默认参数,结果却发现不支持:

【已解决】Java中函数的默认参数

3.然后就是一点点去写代码,去调试了。

4.期间,出错了:

【基本解决】java中没法new:Cannot instantiate the type HttpParams

5.再去搞懂参数配置:

【整理】关于Java中的httpClient中可以传入的参数

6.再继续写代码,期间遇到各种问题,基本都解决了:

【已解决】Java代码中new List时出错:Cannot instantiate the type List<NameValuePair>

【已解决】实现Java中控制台中输入字符串

【无需解决】Java代码new BasicNameValuePair时出错:The constructor BasicNameValuePair(String, boolean) is undefined

【已解决】Java中实现{String, boolean}类型的字典Dict变量

【已解决】Eclipse中用java代码去new Date结果出错:The constructor Date(String) is deprecated

【暂未解决】Eclipse中调试Java代码期间如何修改值

【已解决】Java中的new Date所得的年份异常:传入2043年结果却是3943年

7.最终,完成了,主体代码为:

/**
* [File]
* EmulateLoginBaidu.java
*
* [Function]
* Use Java code to emulate login baidu
*
* 【教程】模拟登陆百度之Java代码版
* http://www.crifan.com/emulate_login_baidu_use_java_code
*
* [Version]
* v1.0, 2013-09-17
*
* [Note]
* 1. need add apache http lib:
* 【已解决】Eclipse的java代码出错:The import org.apache cannot be resolved
* http://www.crifan.com/java_eclipse_the_import_org_apache_cannot_be_resolved/
* 2.need crifanLib.java
* http://code.google.com/p/crifanlib/source/browse/trunk/java/crifanLib.java
*
* [History]
* [v1.0]
* 1. initial version, finally successfully emulate login baidu using java code.
*/
package com.login4baidu;
//import java.io.IOException;
import java.util.ArrayList;
//import java.util.Calendar;
//import java.util.Date;
//import java.util.GregorianCalendar;
import java.util.HashMap;
//import java.util.Hashtable;
import java.util.List;
//import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.cookie.Cookie;
//import org.apache.http.impl.cookie.BasicClientCookie;
//import org.apache.http.impl.cookie.BasicClientCookie2;
import org.apache.http.message.BasicNameValuePair; //import crifanLib; /**
* @author CLi
*
*/
public class EmulateLoginBaidu {
static crifanLib crl; /**
* @param args
*/
public static void main(String[] args) {
crl = new crifanLib(); // TODO Auto-generated method stub
EmulateLoginBaiduUsingJava();
} // emulate login baidu using java code
public static void EmulateLoginBaiduUsingJava()
{
System.out.println("============ 程序说明 ============");
System.out.println("功能:本程序是用来演示使用Java代码去实现模拟登陆百度");
System.out.println("注意事项:部分百度账户,在登陆时会出现:");
System.out.println("1.部分百度账户,在登陆时会出现:");
System.out.println("系统检测到您的帐号疑似被盗,存在安全风险。请尽快修改密码。");
System.out.println("此时,本程序,无法成功模拟登陆,请自行按照提示去修改密码后,就可以了。"); boolean bLoginBaiduOk = false;
List<Cookie> curCookieList; //step1: login baidu, got cookie BAIDUID
System.out.println("====== 步骤1:获得BAIDUID的Cookie ======");
String strTokenValue = "";
boolean bGotCookieBaiduid = false;
String strBaiduUrl = "http://www.baidu.com/";
HttpResponse baiduResp = crl.getUrlResponse(strBaiduUrl); curCookieList =crl.getCurCookieStore().getCookies();
crl.dbgPrintCookies(curCookieList, strBaiduUrl);
for(Cookie ck : curCookieList)
{
String cookieName = ck.getName();
if(cookieName.equals("BAIDUID"))
{
bGotCookieBaiduid = true;
}
}
if (bGotCookieBaiduid)
{
System.out.println("正确:已找到cookie BAIDUID");
}
else
{
System.out.println("错误:没有找到cookie BAIDUID !");
} //step2: login, pass paras, extract resp cookie
System.out.println("====== 步骤2:提取login_token ======");
boolean bExtractTokenValueOK = false;
if(bGotCookieBaiduid)
{
//https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true
String getapiUrl = "https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true";
String getApiRespHtml = crl.getUrlRespHtml(getapiUrl); curCookieList =crl.getCurCookieStore().getCookies();
crl.dbgPrintCookies(curCookieList, getapiUrl); //bdPass.api.params.login_token='3cf421493884e0fe9080593d05f4744f';
Pattern tokenValP = Pattern.compile("bdPass\\.api\\.params\\.login_token='(?<tokenVal>\\w+)';");
Matcher tokenValMatcher = tokenValP.matcher(getApiRespHtml);
//boolean foundTokenValue = tokenValMatcher.matches(); // will not match, but can search to find it
boolean foundTokenValue = tokenValMatcher.find();
if(foundTokenValue)
{
strTokenValue = tokenValMatcher.group("tokenVal"); //3cf421493884e0fe9080593d05f4744f
System.out.println("正确:找到 bdPass.api.params.login_token=" + strTokenValue); bExtractTokenValueOK = true;
}
else
{
System.out.println("错误:没找到bdPass.api.params.login_token !");
}
} //step3: verify returned cookies
if (bGotCookieBaiduid && bExtractTokenValueOK)
{
System.out.println("======步骤3:登陆百度并检验返回的Cookie ======"); /*
//Note:
//here, has verify, not manually update some cookie's domain and expiry
//also can emulate baidu successfully //do some workaround to makesure here cookie H_PS_PSSID not expire
//[version: 0][name: H_PS_PSSID][value: 3359_3341_2776_1424_2981][domain: .baidu.com][path: /][expiry: null] //Thu Sep 17 14:22:08 CST 2043
//Date newExpiryDate = new Date(2043, 9, 17);
Date newExpiryDate = new Date(143, 9, 17);
//Calendar newExpiryCalendar = new GregorianCalendar(2043, 9, 17, 14, 22, 8); BasicClientCookie hPsPssidCookie = null;
BasicClientCookie dbsvrtmCookie = null;
//int hPsPssidCookieIdx = 0; curCookieList = crl.getCurCookieList();
for(Cookie ck : curCookieList)
{
if(ck.getName().equalsIgnoreCase("H_PS_PSSID"))
{
//hPsPssidCookieIdx = curCookieList.indexOf(ck);
hPsPssidCookie = (BasicClientCookie) ck;
hPsPssidCookie.setExpiryDate(newExpiryDate);
ck = hPsPssidCookie;
//break;
} if(ck.getName().equalsIgnoreCase("BDSVRTM"))
{
dbsvrtmCookie = (BasicClientCookie) ck;
dbsvrtmCookie.setDomain(".baidu.com");
dbsvrtmCookie.setExpiryDate(newExpiryDate);
ck = dbsvrtmCookie;
//break;
}
}
crl.setCurCookieList(curCookieList);
*/ String staticPageUrl = "http://www.baidu.com/cache/user/html/jump.html"; List<NameValuePair> postDict = new ArrayList<NameValuePair>();
//ArrayList<NameValuePair> headerDict = new ArrayList<NameValuePair>();
//postDict.add(new BasicNameValuePair("ppui_logintime", ""));
postDict.add(new BasicNameValuePair("charset", "utf-8"));
//postDict.add(new BasicNameValuePair("codestring", ""));
postDict.add(new BasicNameValuePair("token", strTokenValue));
postDict.add(new BasicNameValuePair("isPhone", "false"));
postDict.add(new BasicNameValuePair("index", "0"));
//postDict.add(new BasicNameValuePair("u", ""));
//postDict.add(new BasicNameValuePair("safeflg", "0"));
postDict.add(new BasicNameValuePair("staticpage", staticPageUrl));
postDict.add(new BasicNameValuePair("loginType", "1"));
postDict.add(new BasicNameValuePair("tpl", "mn"));
postDict.add(new BasicNameValuePair("callback", "parent.bdPass.api.login._postCallback")); //get input baidu username and password
String strBaiduUsername = "";
String strBaiduPassword = "";
Scanner inputReader = new Scanner(System.in);
System.out.println("Please Enter Your:" );
System.out.println("Baidu Username:" );
strBaiduUsername = inputReader.nextLine();
//System.out.println("You Entered Username=" + strBaiduUsername);
System.out.println("Baidu Password:" );
strBaiduPassword = inputReader.nextLine();
//System.out.println("You Entered Password=" + strBaiduPassword);
inputReader.close(); postDict.add(new BasicNameValuePair("username", strBaiduUsername));
postDict.add(new BasicNameValuePair("password", strBaiduPassword)); postDict.add(new BasicNameValuePair("verifycode", ""));
postDict.add(new BasicNameValuePair("mem_pass", "on")); String baiduMainLoginUrl = "https://passport.baidu.com/v2/api/?login";
String loginBaiduRespHtml = crl.getUrlRespHtml(baiduMainLoginUrl, null, postDict); //Map cookieNameDict = new Map();
//Map cookieNameDict = new Hashtable();
HashMap<Object, Boolean> cookieNameDict = new HashMap<Object, Boolean>();
cookieNameDict.put("BDUSS", false);
cookieNameDict.put("PTOKEN", false);
cookieNameDict.put("STOKEN", false);
//Set-Cookie: SAVEUSERID=deleted; expires=Mon, 17-Sep-2012 09:45:03 GMT; path=/; domain=passport.baidu.com; httponly,
//cookieNameDict.put("SAVEUSERID", false); curCookieList = crl.getCurCookieList();
for(Object objCookieName : cookieNameDict.keySet().toArray())
{
String strCookieName = objCookieName.toString();
for(Cookie ck: curCookieList)
{
if(strCookieName.equalsIgnoreCase(ck.getName()))
{
cookieNameDict.put(strCookieName, true);
}
}
} boolean bAllCookiesFound = true;
for (Object objFoundCurCookie : cookieNameDict.values())
{
bAllCookiesFound = bAllCookiesFound && Boolean.parseBoolean(objFoundCurCookie.toString());
} bLoginBaiduOk = bAllCookiesFound; if (bLoginBaiduOk)
{
System.out.println("成功模拟登陆百度首页!" );
}
else
{
System.out.println("模拟登陆百度首页 失败!");
System.out.println("所返回的HTML源码为:" + loginBaiduRespHtml);
}
} return;
}
}
/**
* [File]
* crifanLib.java
*
* [Function]
* 1. implement crifan's common functions
* https://code.google.com/p/crifanlib/source/browse/trunk/java/crifanLib.java
*
* [Version]
* v2.0
*
* [Contact]
* http://www.crifan.com/about/me/
*
* [Note]
* 1. need add apache http lib:
* 【已解决】Eclipse的java代码出错:The import org.apache cannot be resolved
* http://www.crifan.com/java_eclipse_the_import_org_apache_cannot_be_resolved/
*
* [History]
* [v2.0, 2013-09-17]
* 1. update getUrlResponse and getUrlRespHtml
* 2. add getCurCookieList, getCurCookieStore, setCurCookieStore, setCurCookieList
*
* [v1.4, 2013-07-17]
* 1. add calcTimeStart, calcTimeEnd
* 2. add dateToString, outputStringToFile
*
* [v1.0]
* 1. add http related func and regex related func
*/ package com.login4baidu; import java.io.File;
//import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
//import java.net.CookieManager;
//import java.net.CookiePolicy;
//import java.net.HttpCookie;
import java.text.SimpleDateFormat;
//import java.util.Calendar;
import java.util.Date;
//import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
//import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
//import org.apache.http.client.HttpClient;
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.client.methods.HttpUriRequest;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.protocol.ClientContext; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.impl.cookie.BasicClientCookie; import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
//import org.apache.http.params.HttpConnectionParams;
//import org.apache.http.params.HttpProtocolParams;
//import org.apache.http.params.HttpParams;
//import org.apache.http.params.DefaultedHttpParams;
import org.apache.http.params.CoreProtocolPNames; import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; //for android:
//import crifan.com.downloadsongtastemusic.R;
//import android.os.Environment;
//import android.widget.EditText;
//import android.app.Activity; public class crifanLib {
private CookieStore gCurCookieStore = null;
//private HashMap<Object, Object> calcTimeKeyDict;
private HashMap<String, Long> calcTimeKeyDict;
//private Map<String, Long> calcTimeKeyDict; //IE7
private static final String constUserAgent_IE7_x64 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)";
//IE8
private static final String constUserAgent_IE8_x64 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E";
//IE9
private static final String constUserAgent_IE9_x64 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; // x64
private static final String constUserAgent_IE9_x86 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; // x86
//Chrome
private static final String constUserAgent_Chrome = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4";
//Mozilla Firefox
private static final String constUserAgent_Firefox = "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6"; private static String gUserAgent = ""; public crifanLib()
{
gUserAgent = constUserAgent_IE8_x64;
gCurCookieStore = new BasicCookieStore(); calcTimeKeyDict = new HashMap<String, Long>();
} /** start calculate time */
public long calcTimeStart(String uniqueKey)
{
long startMilliSec = 0;
startMilliSec = System.currentTimeMillis(); //
calcTimeKeyDict.put(uniqueKey, startMilliSec); //{load_dd_file=1373525642597}
return startMilliSec;
} /** end calculate time */
public long calcTimeEnd(String uniqueKey)
{
long endMilliSec = System.currentTimeMillis(); // long elapsedMilliSec = 0;
if(calcTimeKeyDict.containsKey(uniqueKey))
{
long startMilliSec = calcTimeKeyDict.get(uniqueKey); //
elapsedMilliSec = endMilliSec - startMilliSec; //
} return elapsedMilliSec;
} /* format date value into string */
public String dateToString(Date date, String format)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
String datetimeStr =simpleDateFormat.format(date); //2013-07-08_033034
return datetimeStr;
} /* output string into file */
public boolean outputStringToFile(String strToOutput, String fullFilename)
{
boolean ouputOk = true; File newTextFile = new File(fullFilename);
FileWriter fw;
try {
fw = new FileWriter(newTextFile);
fw.write(strToOutput);
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace(); ouputOk = false;
} return ouputOk;
} public void dbgPrintCookies(List<Cookie> cookieList, String url)
{
if((null != url) && (!url.isEmpty()))
{
System.out.println("Cookies for " + url);
} for(Cookie ck : cookieList)
{
System.out.println(ck);
}
} public void dbgPrintCookies(CookieStore cookieStore)
{
dbgPrintCookies(cookieStore, null);
} public void dbgPrintCookies(CookieStore cookieStore, String url)
{
List<Cookie> cookieList = cookieStore.getCookies();
dbgPrintCookies(cookieList, url);
} public void dbgPrintCookies(List<Cookie> cookieList)
{
dbgPrintCookies(cookieList, null);
} public CookieStore getCurCookieStore()
{
return gCurCookieStore;
} public List<Cookie> getCurCookieList()
{
if(null != gCurCookieStore)
{
return gCurCookieStore.getCookies();
}
else
{
return null;
}
} public void setCurCookieStore(CookieStore newCookieStore)
{
gCurCookieStore = newCookieStore;
} public void setCurCookieList(List<Cookie> newCookieList)
{
gCurCookieStore.clear();
for(Cookie eachNewCk : newCookieList)
{
gCurCookieStore.addCookie(eachNewCk);
}
} /** Get response from url */
public HttpResponse getUrlResponse(
String url,
List<NameValuePair> headerDict,
List<NameValuePair> postDict,
int timeout
)
{
// init
HttpResponse response = null;
HttpUriRequest request = null;
DefaultHttpClient httpClient = new DefaultHttpClient(); //HttpParams headerParams = new HttpParams();
//HttpParams headerParams = new DefaultedHttpParams(headerParams, headerParams);
//HttpParams headerParams = new BasicHttpParams();
BasicHttpParams headerParams = new BasicHttpParams();
//HttpConnectionParams.
//default enable auto redirect
headerParams.setParameter(CoreProtocolPNames.USER_AGENT, gUserAgent);
headerParams.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.TRUE); headerParams.setParameter(CoreConnectionPNames.SO_KEEPALIVE, Boolean.TRUE); if(postDict != null)
{
HttpPost postReq = new HttpPost(url); try{
HttpEntity postBodyEnt = new UrlEncodedFormEntity(postDict);
postReq.setEntity(postBodyEnt);
}
catch(Exception e){
e.printStackTrace();
} request = postReq;
}
else
{
HttpGet getReq = new HttpGet(url); request = getReq;
} if(headerParams != null)
{
//HttpProtocolParams.setUserAgent(headerParams, gUserAgent);
//headerParams.setHeader(HttpMethodParams.USER_AGENT, gUserAgent);
request.setParams(headerParams);
} //request.setHeader("User-Agent", gUserAgent); try{
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, gCurCookieStore);
response = httpClient.execute(request, localContext); //response HeaderGroup value:
//[Via: 1.1 SC-SZ-06, Connection: Keep-Alive, Proxy-Connection: Keep-Alive, Content-Length: 11006, Expires: Tue, 17 Sep 2013 01:43:44 GMT, Date: Tue, 17 Sep 2013 01:43:44 GMT, Content-Type: text/html;charset=utf-8, Server: BWS/1.0, Cache-Control: private, BDPAGETYPE: 1, BDUSERID: 0, BDQID: 0xaaa869770d8d5dcd, Set-Cookie: BDSVRTM=2; path=/, Set-Cookie: H_PS_PSSID=3361_2777_1465_2975_3109; path=/; domain=.baidu.com, Set-Cookie: BAIDUID=C0C2EAA4B1805EF21EE097E2C6A3D448:FG=1; expires=Tue, 17-Sep-43 01:43:44 GMT; path=/; domain=.baidu.com, P3P: CP=" OTI DSP COR IVA OUR IND COM "] //gCurCookieStore (formatted ouput) value:
/*{
[version: 0][name: BAIDUID][value: C0C2EAA4B1805EF21EE097E2C6A3D448:FG=1][domain: .baidu.com][path: /][expiry: Thu Sep 17 09:43:44 CST 2043]=java.lang.Object@55ba1c2b,
[version: 0][name: BDSVRTM][value: 2][domain: www.baidu.com][path: /][expiry: null]=java.lang.Object@55ba1c2b,
[version: 0][name: H_PS_PSSID][value: 3361_2777_1465_2975_3109][domain: .baidu.com][path: /][expiry: null]=java.lang.Object@55ba1c2b
}*/
} catch (ClientProtocolException cpe) {
// TODO Auto-generated catch block
cpe.printStackTrace();
} catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
} return response;
} /** Get response from url */
public HttpResponse getUrlResponse(String url)
{
return getUrlResponse(url, null, null, 0);
} /** Get response html from url, headerDict, html charset, postDict */
public String getUrlRespHtml(
String url,
List<NameValuePair> headerDict,
List<NameValuePair> postDict,
int timeout,
String htmlCharset
)
{
// init
String respHtml = "";
String defaultCharset = "UTF-8";
if((null == htmlCharset) || htmlCharset.isEmpty())
{
htmlCharset = defaultCharset;
}
//init
//HttpClient httpClient = new DefaultHttpClient();
//DefaultHttpClient httpClient = new DefaultHttpClient();
//HttpUriRequest request; //headerParams.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, htmlCharset); try{ HttpResponse response = getUrlResponse(url, headerDict, postDict, timeout); if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
HttpEntity respEnt = response.getEntity(); respHtml = EntityUtils.toString(respEnt, htmlCharset);
} } catch (ClientProtocolException cpe) {
// TODO Auto-generated catch block
cpe.printStackTrace();
} catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
} return respHtml;
} public String getUrlRespHtml(String url, List<NameValuePair> headerDict, List<NameValuePair> postDict)
{
return getUrlRespHtml(url, headerDict, postDict, 0, "");
} public String getUrlRespHtml(String url, String htmlCharset)
{
return getUrlRespHtml(url, null, null, 0, htmlCharset);
} public String getUrlRespHtml(String url)
{
String defaulCharset = "UTF-8";
return getUrlRespHtml(url, defaulCharset);
} public interface UpdateProgressCallback
{
// This is just a regular method so it can return something or
// take arguments if you like.
public void updateProgress(long currentSize, long totalSize);
} /**
* download file from file url
* eg:
* http://m5.songtaste.com/201212211424/2e8a8a85d93f56370d7fd96b5dc6ff23/5/5c/5cf23a97cef6fad6a464eb506c409dbd.mp3
* with header: Referer=http://songtaste.com/
* */
public Boolean downlodFile(String url, File fullFilename, List<NameValuePair> headerDict, UpdateProgressCallback updateProgressCallbak)
{
Boolean downloadOk = Boolean.FALSE; HttpResponse response = getUrlResponse(url, headerDict, null, 0); if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ HttpEntity respEnt = response.getEntity(); System.out.println("isChunked" + respEnt.isChunked());
System.out.println("Streaming" + respEnt.isStreaming()); Boolean isStream = respEnt.isStreaming();
if(isStream){
try {
InputStream fileInStream = respEnt.getContent(); FileOutputStream fileOutStream = new FileOutputStream(fullFilename); long totalSize = respEnt.getContentLength();
byte[] tmpBuf = new byte[8192];
int bufLen = 0;
long downloadedSize = 0;
while( (bufLen = fileInStream.read(tmpBuf)) > 0 ) {
fileOutStream.write(tmpBuf,0, bufLen);
downloadedSize += bufLen; //System.out.println(Long.toString((downloadedSize/totalSize)*100)+"%");
//System.out.println(Long.toString((downloadedSize*100)/totalSize)+"%");
updateProgressCallbak.updateProgress(downloadedSize, totalSize);
}
fileOutStream.close();
downloadOk = Boolean.TRUE;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} return downloadOk;
} /**
* none header version of downlodFile
* */
public String downlodFile(String url, String fullFilename)
{
return downlodFile(url, fullFilename);
} /** Extract single string from input whole string
* Note:
* 1. input pattern should include one group, like 'xxx(xxx)xxx'
* 2. output is in extractedStr
* */
public Boolean extractSingleStr(String pattern, String extractFrom, int flags, StringBuilder extractedStr)
{
Pattern strP = Pattern.compile(pattern, flags);
Matcher foundStr = strP.matcher(extractFrom);
Boolean found = foundStr.find();
if(found)
{
extractedStr.append(foundStr.group(1));
}
return found;
} /**
* None pattern version of extractSingleStr
* */
public Boolean extractSingleStr(String pattern, String extractFrom, StringBuilder extractedStr)
{
return extractSingleStr(pattern, extractFrom, 0, extractedStr);
} }

正常,成功模拟登陆百度的输出为:

【教程】模拟登陆百度之Java代码版

8.完整的Eclipse下面的代码下载:

EmulateLoginBaidu_java_2013-09-17_crifanLibVersion.7z

注意:

1.使用此项目,需要导入org.apache.http的库。

详见:

【已解决】Eclipse的java代码出错:The import org.apache cannot be resolved

2.部分百度账户,在登陆时会出现:

系统检测到您的帐号疑似被盗,存在安全风险。请尽快修改密码。

此时,本程序,无法成功模拟登陆,请自行按照提示去修改密码后,就可以了。

【总结】

java在处理http方面的库,相对来说,还是很不方便使用。

只能算是基本够用吧。

(转自:http://www.crifan.com/emulate_login_baidu_use_java_code/)

【教程】模拟登陆百度之Java代码版的更多相关文章

  1. 模拟登陆百度以及Selenium 的基本用法

    模拟登陆百度,需要依赖于selenium 模块,调用浏览器,执行python命令 先来说一下这个selenium模块啦...... 本文参考内容来自 Selenium官网 SeleniumPython ...

  2. Selenium模拟登陆百度贴吧

    Selenium模拟登陆百度贴吧 from selenium import webdriver from time import sleep from selenium.webdriver.commo ...

  3. 使用python模拟登陆百度

    #!/usr/bin/python # -*- coding: utf- -*- """ Function: Used to demostrate how to use ...

  4. python selenium 模拟登陆百度账号

    代码: from selenium import webdriver url = 'https://passport.baidu.com/v2/?login' username = 'your_use ...

  5. 模拟登陆百度 python

    from time import sleep from selenium import webdriver driver = webdriver.Firefox() driver.get('http: ...

  6. 【转】详解抓取网站,模拟登陆,抓取动态网页的原理和实现(Python,C&num;等)

    转自:http://www.crifan.com/files/doc/docbook/web_scrape_emulate_login/release/html/web_scrape_emulate_ ...

  7. 【教程】手把手教你如何利用工具&lpar;IE9的F12&rpar;去分析模拟登陆网站&lpar;百度首页&rpar;的内部逻辑过程

    [前提] 想要实现使用某种语言,比如Python,C#等,去实现模拟登陆网站的话,首先要做的事情就是使用某种工具,去分析本身使用浏览器去登陆网页的时候,其内部的执行过程,内部逻辑. 此登陆的逻辑过程, ...

  8. selenium3&period;7&plus; python3 添加cookie模拟登陆

    一.背景介绍 最近做一个爬虫项目,用selenium调用浏览器去获取渲染后的源码,但是每次登陆都需要手机验证,这真的是头痛啊,这种验证方式不要想着去破解,还是老老实实用手机收验证码去吧!反正我是不知道 ...

  9. Java模拟实现百度文档在线浏览

    Java模拟实现百度文档在线浏览 这个思路是我参考网上而来,代码是我实现. 采用Apache下面的OpenOffice将资源文件转化为pdf文件,然后将pdf文件转化为swf文件,用FlexPaper ...

随机推荐

  1. 导向矢量(Steering Vector)

    导向矢量是阵列天线的所有阵元对具有单位能量窄带信源的响应. 由于阵列响应在不同方向上是不同的,导向矢量与信源的方向是相互关联的,这种关联的独特性依赖于阵列的几何结构.对于同一阵元阵列,导向矢量的每一个 ...

  2. vpsmate安装

    安装需求 操作系统:CentOS/Redhat 5.4 或 5.4 以上版本,32位或64位均可,推荐使用 CentOS 6.2 64位. 内存大小:运行时占用约 20MB 左右的服务器内存. 请使用 ...

  3. 动画--过渡所需时间 transition-duration

    transition-duration属性主要用来设置一个属性过渡到另一个属性所需的时间,也就是从旧属性过渡到新属性花费的时间长度,俗称持续时间. 案例演示: 在鼠标悬停(hover)状态下,让容器从 ...

  4. handler&period;post&lpar;r&rpar;同一个线程的疑惑

    handler.post(r);是把r加到消息队列,但并未开辟新线程.等到消息被取出时才执行. package com.lei.handlethread; import android.os.Bund ...

  5. U盘只剩下快捷方式

    原理: 其实,这个是一种叫1KB病毒(也称之为快捷方式病毒.风暴一号)惹的祸,它是一种恶意的蠕虫病毒,执行以下恶意操作:1.当你的U盘放到一个已经被感染 的主机上时,主机(我的电脑)上的病毒体进程首先 ...

  6. Binder机制,从Java到C (大纲)

    转载请标注:张小燕:http://www.cnblogs.com/zhangxinyan/p/3487381.html 前段时间一直在看有关Binder机制的内容,觉得受益匪浅,整理记录于此,大家请随 ...

  7. Hive基础(1)---Hive是什么

    1. Hive是什么 Hive是基于Hadoop的数据仓库解决方案.由于Hadoop本身在数据存储和计算方面有很好的可扩展性和高容错性,因此使用Hive构建的数据仓库也秉承了这些特性. 这是来自官方的 ...

  8. Excel 2010高级应用-雷达图(六)

    Excel 2010高级应用-雷达图(六) 基本操作如下: 1.新建空白Excel文档,并命名雷达图 2.单击"插入",并找到雷达图图样 3.单击雷达图图样,在空白文档上生成图框, ...

  9. 获取客户端的请求IP地址

    获取客户端的请求IP地址 package com.microClass.util; import javax.servlet.http.HttpServletRequest; import java. ...

  10. C&num; to IL 4 Keywords and Operators&lpar;关键字和操作符&rpar;

    Code that is placed after the return statement never gets executed. In the first programgiven below, ...