模拟登录完整示例
package crawl;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.DeflateDecompressingEntity;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.CharArrayBuffer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public abstract class LoginTianyan {
private static String encode = "utf-8";
protected static ApplicationContext loadSpringBean() {
final String[] paths = new String[] {
"resource/spring/action/action.xml",
"resource/struts/action.xml",
"resource/spring/common-beans.xml",
"resource/spring/reposity/mongoBean.xml" };
final ApplicationContext context = new FileSystemXmlApplicationContext(
paths); // ClassPathXmlApplicationContext
return context;
}
protected static Boolean loginTianyan(DefaultHttpClient httpPostClient)
throws UnsupportedEncodingException, IOException,
ClientProtocolException {
String parm = "username=joe%40163.com&password=123&redirect=&source_flow=&__source_node__=start&__context__=5ufhX2NDkiv0BqBT8aCbOZPPNjFLVRRGcuKVOAe-RxaZmPooKR5xIOOhqkdGM-7J-eWrCqmPh0pI2LjZ0h68b21up5kKMTkFjQuOvs_pmB5i2rn-2V2v60mfp9HeKJCp6_a-QV2cL4GD1H81gR7JRA%3D%3D&__form__=login&redirect=&button=Sign+In";
HttpPost httpRequst = new HttpPost(
"https://www.Tianyan.com/user/signin/");// 创建HttpPost对象
StringEntity entity = new StringEntity(parm);
httpRequst.setEntity(entity);
httpRequst.getParams().setParameter(
CoreProtocolPNames.HTTP_CONTENT_CHARSET, encode);
httpPostClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);// 连接时间20s
httpPostClient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 20000);// 数据传输时间60s
HttpResponse httpResponse = httpPostClient.execute(httpRequst);// 其中HttpGet是HttpUriRequst的子类
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity.getContentEncoding() != null) {
if ("gzip".equalsIgnoreCase(httpEntity.getContentEncoding()
.getValue())) {
httpEntity = new GzipDecompressingEntity(httpEntity);
} else if ("deflate".equalsIgnoreCase(httpEntity
.getContentEncoding().getValue())) {
httpEntity = new DeflateDecompressingEntity(httpEntity);
}
}
String result = enCodetoString(httpEntity, encode);// 取出应答字符串
System.out.println(result);
} else if (httpResponse.getStatusLine().getStatusCode() == 302) {
System.out.println("重定向了");
Header header = httpResponse.getFirstHeader("location");
if (header != null) {
String urlString = header.getValue();
System.out.println("header中的value " + urlString);
if (!urlString.contains("www")) {
urlString = "https://www.tianyan.com" + urlString;
}
if (urlString != null && !urlString.equals("")) {
String resultS = getUrlContent(httpPostClient, urlString);
if (!resultS.equals("")) {
return true;
}
}
}
}
return false;
}
private static String getUrlContent(DefaultHttpClient httpPostClient,
String urlString) throws IOException, ClientProtocolException {
HttpGet httpGet = new HttpGet(urlString);
HttpResponse httpGetResponse = httpPostClient.execute(httpGet);// 其中HttpGet是HttpUriRequst的子类
httpPostClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);// 连接时间20s
httpPostClient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 8000);// 数据传输时间60s
if (httpGetResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpGetResponse.getEntity();
if (httpEntity.getContentEncoding() != null) {
if ("gzip".equalsIgnoreCase(httpEntity.getContentEncoding()
.getValue())) {
httpEntity = new GzipDecompressingEntity(httpEntity);
} else if ("deflate".equalsIgnoreCase(httpEntity
.getContentEncoding().getValue())) {
httpEntity = new DeflateDecompressingEntity(httpEntity);
}
}
String result = enCodetoString(httpEntity, encode);// 取出应答字符串
// System.out.println(result);
return result;
}
return "";
}
public static String enCodetoString(final HttpEntity entity,
final String defaultCharset) throws IOException, ParseException {
return enCodetoStringDo(entity,
defaultCharset != null ? Charset.forName(defaultCharset) : null);
}
public static String enCodetoStringDo(final HttpEntity entity,
Charset defaultCharset) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return null;
}
try {
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
int i = (int) entity.getContentLength();
if (i < 0) {
i = 4096;
}
Charset charset = null;
try {
// ContentType contentType = ContentType.get(entity);
// if (contentType != null) {
// charset = contentType.getCharset();
// }
} catch (final UnsupportedCharsetException ex) {
throw new UnsupportedEncodingException(ex.getMessage());
}
if (charset == null) {
charset = defaultCharset;
}
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
Reader reader = new InputStreamReader(instream, charset);
CharArrayBuffer buffer = new CharArrayBuffer(i);
char[] tmp = new char[1024];
int l;
while ((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
return buffer.toString();
} finally {
instream.close();
}
} |